Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

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

Hey,



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!


Comments

  • 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



    interface

    variablename:integer;



    implementation

    end.





    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

    Later!


Sign In or Register to comment.