Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Spread code over multiple files

  1. #1

    Spread code over multiple files

    System: WinXP Pro SP2
    Compiler: FPC
    Libraries: -


    Basically I want my experimental (for learning purposes) simple console text adventure's code that's in one file to be spread over more files, like some kind of modularity. (It's more for ease of reading, though I think it's fairly readable with the use of a search in my text editor, but even then it's useful to know.)

    As the straightforward hobbyist, I usually (from what I've learned anyway) start my main() and put my non-existent procedures/functions into it, like so....
    [pascal]///--->
    begin
    do_inits;
    tell_story01;

    choose_class;
    ask_showclass;

    tell_story02;
    idle_state;
    ask_usepotion_firsttime;

    tell_story03;
    idle_state;

    tell_story04;
    end.
    ///<---
    [/pascal]

    Then I declare the functions above the main(), and some of them are called by others. For instance, do_inits() has...

    [pascal]
    procedure do_inits; begin
    init_classvalues;
    welcome_screen;
    verify_userdata;
    set_textspeed; //goto main()
    end;
    [/pascal]

    Now, I got a whole bunch of procedures declared and described all above my main(), so you can imagine my source file is kinda big.

    By looking at the code snippets above, can you give me an example (optionally based on them of course) of how to apply modularity over separate files?

    I *THINK* I could somehow take out code here and there and put it into a separate file and in the main file use uses and the name of the file, but I'm not so sure on it. I just want to do it the right away and avoid disappointments :)

  2. #2
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    Spread code over multiple files

    I believe what you are looking for is units.

    Units are a great main-stay of the Pascal and Object Pascal languages. All you simply have to do to use them is

    [pascal]uses
    TheNameOfYourNewUnit;[/pascal]

    and you're ready to go.

    More than one unit...

    [pascal]uses
    FirstUnit, SecondUnit;[/pascal]

    Easy as pi.

    As far as making your game more 'modular' I'd recommend taking a look at OOP. (Object Pascal being of the best at this!)

    The idea of OOP is that everything in your game is made into an object. Each object in your game is of a specific class. An easy way to think about this concept is that all your game objects are like ships and each of them of a specific type of ship, which is your class.

    Each object has a way to be made with a constructor and unmade with a destructor. There are basically functions and procedures as you learn from Standard Pascal only they are for the purpose of creating and managing your objects.

    The coolest part of OOP is that each class can be a parent of a new class. ie.

    [pascal]type
    TSon = class(TDad)
    StuffInside: Integer;
    // etc...
    constructor BeMade(stuff: Boolean);
    destructor BeUnmade(stuff: Integer);
    end;[/pascal]

    See? Thats your basic object class definition. Easy stuff isn't it? You can have as mand constructors/destructors and all that fun gooie stuff inside as you like, it's your object!

    Cool thing is that TSon will have everything from TDad as well, so if there are constructors, etc in TDad TSon can use them. TObject is the most basic of object classes though. Think of it like a base class.

    Anyhow it can get a lot more complex than that, but I figured it would give you an idea of what OOP would do for you in making things more modular and flexible.

    If some of this went over your head don't worry, just grab a good book that probably explains it a lot better than I have.
    Jason McMillen
    Pascal Game Development
    Co-Founder





  3. #3

    Spread code over multiple files

    Thanks WILL :)

    I'm looking into the units thing and I understand that a self-made unit should be in a separate file, and in it there should be unit identifier_that_is_filename (though I haven't tried yet in the code, still learning its way). This in combination with OOP looks very useful and powerful.

    Thing about OOP is, though, I understand the concept of it when I was learning C++, but I'd never used it because it was a bit too confusing to apply code-wise. But the overall picture is simple.

    I'm kind of comfortable with minimal procedural stuff so I've been avoiding OOP. But isn't a (C(++)) struct or (Pascal) record "kind of like a class", but without constructors and methods and such?

    Do you think I should really hit OOP or do you think I should continue doing procedural until I have that perfectly under control? I ask this because OOP to me looks like it's just a step higher for more general natural control, even though it looks like I'd still rely on using procedures/methods, so it looks logical to perfectly know procedural programming first.

  4. #4

    Spread code over multiple files

    Quote Originally Posted by Bijo
    Do you think I should really hit OOP or do you think I should continue doing procedural until I have that perfectly under control? I ask this because OOP to me looks like it's just a step higher for more general natural control, even though it looks like I'd still rely on using procedures/methods, so it looks logical to perfectly know procedural programming first.
    You are correct. You should master the normal procedural programming first to some degree to benefit from OO programmming

    Your approach in the OP is called Top-Down programming which I also after a lot of years programming still use. The nice thing about it is that you'll have to write the underlying procedures so you can't slack in the initial stages
    Peregrinus, expectavi pedes meos in cymbalis
    Nullus norvegicorum sole urinat

  5. #5
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    Spread code over multiple files

    You probably should learn the basics of Pascal first as it will give you a solid base to build up your knowledge for Object Pascal. Everything is the same except for the extra OOP stuff.

    And you know what? I completely forgot to put up the code structure for a unit. Sorry, I was thinking of doing that, but I guess posting from work has it's pitfalls huh?

    [pascal]unit TheNameYouWant;

    interface

    implementation

    end.[/pascal]

    Thats it. And example of usage would be...

    [pascal]unit TheNameYouWant;

    interface

    // Global Stuff

    uses
    Math;

    var
    gText: String; // Can be accessed by this unit and other units, libraries and programs

    // Any optionally globally accessible functions/procedures go here

    procedure DoThings; {You only have to declare your local procedures/functions here to use them globally!}

    implementation

    // Local Stuff

    var
    lNumber: Integer; // these can only be accessed within this unit!

    procedure DoThings;
    begin
    // Whatever this thing does.
    end;

    // begin
    // If you include a code block at the bottom of your unit
    // all of the contents will auto execute. This is handy for
    // some unit specific auto initialization, etc...
    // Of course this is only optional.
    end.[/pascal]

    I think you should be fairly good to go with that example. Play around with it and try things. And of course ask if you get stuck.
    Jason McMillen
    Pascal Game Development
    Co-Founder





  6. #6

    Spread code over multiple files

    Thanks for the infos :)


    That with the unit thing is kind of confusing. Keywords like implemenation and interface are the worst. I expected self-made units to be exactly similar to a program (where the program keyword is replaced by unit) but just without the main(). I think I'll just keep doing my familiar stuff and take it slow and easy with this one :)

  7. #7
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    Spread code over multiple files

    Quote Originally Posted by Bijo
    Keywords like implemenation and interface are the worst.
    Really? I'd say that they are the least complex of everything I've showed you.

    All they do is tell the compiler when it's time to process global declarations and and when to do the local declarations and code. Thats all they do really.

    Do you know the difference between a global variable and a local variable? I think that might be the start of your confusion. Mind you the concept comes up, or should have, when you learned functions & procedures.
    Jason McMillen
    Pascal Game Development
    Co-Founder





  8. #8

    Spread code over multiple files

    Quote Originally Posted by WILL
    Quote Originally Posted by Bijo
    Keywords like implemenation and interface are the worst.
    Do you know the difference between a global variable and a local variable? I think that might be the start of your confusion. Mind you the concept comes up, or should have, when you learned functions & procedures.
    From what I know, a global variable is accessible throughout the whole program and a local one is accessible only by the function that has it.

  9. #9

    Spread code over multiple files

    Quote Originally Posted by Bijo
    That with the unit thing is kind of confusing. Keywords like implemenation and interface are the worst.
    Hi Bijo,

    you said you know C/C++.

    Consider the Interface part as your .h file while the implementation part is your .c/.cpp file.... thats all... the same thing, but in one file!

    Greetings.
    <a href="http://www.greatgamesexperiment.com/game/Valgard/?utm_source=gge&amp;utm_medium=badge_game"><img border="0" alt="GGE" title="GGE" src="http://static.greatgamesexperiment.com/badge/game/valgard/gge400x56.png"></a>

  10. #10

    Spread code over multiple files

    That's a bit difficult to imagine well for me, 'cause I terribly misused / abused header files when using C++ ops:

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •