Need a really simple answer to a simple question!!! - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Need a really simple answer to a simple question!!!


I've been programming a game for quiet some time now and have come across a bit of a problem.

My code is too long for one pascal file and the error message says that I need to put some of the code into a unit.

I understand how to use a unit and how to make one except I would love to know, how can I put code in the unit which has variables which apply to my main program?

I hope I have asked this question clearly.

Help would be greatly appreciated!!!

Thanks in advance!


  • try declaring the variables in the "interface" section.

    if you wanted to centralize such varable declararations

    you can have a unit that merely has these declarations,

    rather like a "block common", jhc I haven't used that

    term for years.

  • Hey Steve,

    Please could you explain clearly how I can have variables in my main program and have them active in the unit. See the thing is, when I put them under the "interface" section it takes them as variables but they don't keep their value from the main program.

    See in the unit I want to put in code which draws images to the screen using co-ordinates that are defined and changed constantly in the main program as objects are moved around.

    So the unit has to use the exact variables declared in the main program.

    P.S. I'm not an expert programmer, maby because I'm actually still at school and everything I know about programming I taught myself, so please forgive me for seeming a bit "daaa". Soo maby an example would be great! if you know how.

    Thanks a lot!

  • Just do it like this

    Unit unitname





    and now REMOVE the variable declaration from your main program. If you main program now does a

    USES unitname

    you can just use the variables the same way as you would when they were in your main. You can also change them from your main and all. Just be sure to declare the variable ONLY in your unit, and not both in your unit and the main program.

    And oh, we could tell that you are not a cutting-edge programmer, for else you wouldn't have crammed over 64k of code in your main ;-)

  • Thanks alot MR smartie pants!

    No seriously, thanks!

    I'll be back for more questions.

    But for now I'm kewl!

    Oh, P.S. Does anyone know how to play mid files in the background while a program is running?

    I'm guessing it's complicated?

    So I wouldn't know how...but if anyone has an easy to use unit that they wanna share?

    Just give me a shout.

    Thanks all


Sign In or Register to comment.