How To Link Variables from different procedures

Hey I am doing a game and I am going to need to split up each level into procedures since an error of "statement part too large " is occuring and I cant put the whole levels into 1 procedures. However I have a problem, since I need to activate a level only is lives>0 (so levels won't be played if lives=0). Do you know how can I do this please?

PS. If you know how to fix the "statement part too large", please tell me since it will solve thousands of problems. 10x
«1

Comments

  • : Hey I am doing a game and I am going to need to split up each level
    : into procedures since an error of "statement part too large " is
    : occuring and I cant put the whole levels into 1 procedures. However
    : I have a problem, since I need to activate a level only is lives>0
    : (so levels won't be played if lives=0). Do you know how can I do
    : this please?
    :
    : PS. If you know how to fix the "statement part too large", please
    : tell me since it will solve thousands of problems. 10x
    :
    You should use procedure parameters for that. Here's an example:
    [code]
    procedure GetName(var name: string);
    { [b]var[/b] before the parameter is necessary to change the value
    in the main program }
    begin
    write('Enter name: ');
    readln(name);
    end;

    procedure ShowName(name: string);
    begin
    writeln('Your name is ', name);
    end;

    var
    UserName: string
    begin
    GetName(UserName);
    ShowName(UserName);
    end.
    [/code]
  • : Hey I am doing a game and I am going to need to split up each level
    : into procedures since an error of "statement part too large " is
    : occuring and I cant put the whole levels into 1 procedures. However
    : I have a problem, since I need to activate a level only is lives>0
    : (so levels won't be played if lives=0). Do you know how can I do
    : this please?
    :
    : PS. If you know how to fix the "statement part too large", please
    : tell me since it will solve thousands of problems. 10x
    :

    What style of game is it? How are you storing your levels? Depending on these and many other factors, there are probably more efficient ways to go about this.

    First off, if you are making each level as an array then that can take up alot of memory. You should post some more info and possibly code snippets. Then we can help you clean it up a bit.
  • The game that I am making consists of drawing some bricks (1 chr each) and then I shoot a missile that keeps going till y=1 and the missile erases the bricks on its way up. when the co-ordinates of the missile are equal to that of a brick (then a variable of a brick will be assigned to 1). I have an array of 40 of integer (maximum of 40 because the maximum of bricks in each level is 40). When all variable of the bricks are equal to 1, then the level ends. However it will also stop if lives<1 or quit=1 (when character escape is pressed quit is equal to 1). Then I will need to start the procedure of the other level if only lives>0 and quit<1. So I will need to assign variables of procedures to variables of different procedures.Think you got it right?
  • : The game that I am making consists of drawing some bricks (1 chr
    : each) and then I shoot a missile that keeps going till y=1 and the
    : missile erases the bricks on its way up. when the co-ordinates of
    : the missile are equal to that of a brick (then a variable of a brick
    : will be assigned to 1). I have an array of 40 of integer (maximum of
    : 40 because the maximum of bricks in each level is 40). When all
    : variable of the bricks are equal to 1, then the level ends. However
    : it will also stop if lives<1 or quit=1 (when character escape is
    : pressed quit is equal to 1). Then I will need to start the procedure
    : of the other level if only lives>0 and quit<1. So I will need to
    : assign variables of procedures to variables of different
    : procedures.Think you got it right?
    :
    I would use procedure parameters in this case. It takes the least changes to the code.
  • So if I have a variable of lives:integer in procedure level_1, and I need its value to be in procedure level_2. How can I do it? something like lives:=level_1(lives);
  • : So if I have a variable of lives:integer in procedure level_1, and I
    : need its value to be in procedure level_2. How can I do it?
    : something like lives:=level_1(lives);
    :

    You could also declare your procedure like this:
    [code]
    PROCEDURE Level_1([b]VAR[/b] Lives : Integer);
    [/code]

    Then any changes made to LIVES inside the procedure will be reflected back to the original variable passes to LEVEL_1()

    ex:
    [code]
    VAR Life : Integer;
    Begin
    Life := 3;
    Level_1(Life); {Say they died twice}
    WriteLn(Life); { Life would be = 1 }
    End.
    [/code]
  • Wouldn't it be easier to just declare the "lives" variable as a global variable? That would allow all procedures and functions access to it without having to declare it for each procedure and function.
  • : The game that I am making consists of drawing some bricks (1 chr
    : each) and then I shoot a missile that keeps going till y=1 and the
    : missile erases the bricks on its way up. when the co-ordinates of
    : the missile are equal to that of a brick (then a variable of a brick
    : will be assigned to 1). I have an array of 40 of integer (maximum of
    : 40 because the maximum of bricks in each level is 40). When all
    : variable of the bricks are equal to 1, then the level ends. However
    : it will also stop if lives<1 or quit=1 (when character escape is
    : pressed quit is equal to 1). Then I will need to start the procedure
    : of the other level if only lives>0 and quit<1. So I will need to
    : assign variables of procedures to variables of different
    : procedures.Think you got it right?
    :

    If all your levels are handled the same, but just have different positions for the blocks, I would use one procedure that "loads" the correct level at the start and then all the rest of the code is the same. Saves having to retype things out multiple times and lengthening your code. Of course by "LOAD" you could be loading from a file or just copying one array to another. Then you can add as many levels as you want by just adding the level blocks without writing a whole new procedure.

    Example using a preset array:
    [code]
    USES Crt, Dos;
    TYPE
    BlockInfo = record
    Visible : Boolean; { True if block hasn't been hit, otherwise false }
    XPos : Byte; { The X Coordinate of our block }
    YPos : Byte; { The Y Coordinate of our block }
    End;
    BlockSet = Array[1..40] Of BlockInfo;

    CONST
    { You can expand this array to hold as many levels as
    you wish, memory allowing. Or load each level from a file. }
    LevelInfo : Array[1..1] Of BlockSet =
    (((Visible:True; XPos:1; YPos:1),(Visible:True; XPos:5; YPos:1),
    (Visible:True; XPos:10; YPos:1),(Visible:True; XPos:15; YPos:1),
    (Visible:True; XPos:20; YPos:1),(Visible:True; XPos:25; YPos:1),
    (Visible:True; XPos:30; YPos:1),(Visible:True; XPos:35; YPos:1),
    (Visible:True; XPos:40; YPos:1),(Visible:True; XPos:45; YPos:1),
    (Visible:True; XPos:50; YPos:1),(Visible:True; XPos:55; YPos:1),
    (Visible:True; XPos:60; YPos:1),(Visible:True; XPos:65; YPos:1),
    (Visible:True; XPos:70; YPos:1),(Visible:True; XPos:75; YPos:1),
    (Visible:True; XPos:1; YPos:3),(Visible:True; XPos:5; YPos:3),
    (Visible:True; XPos:10; YPos:3),(Visible:True; XPos:15; YPos:3),
    (Visible:True; XPos:20; YPos:3),(Visible:True; XPos:25; YPos:3),
    (Visible:True; XPos:30; YPos:3),(Visible:True; XPos:35; YPos:3),
    (Visible:True; XPos:40; YPos:3),(Visible:True; XPos:45; YPos:3),
    (Visible:True; XPos:50; YPos:3),(Visible:True; XPos:55; YPos:3),
    (Visible:True; XPos:60; YPos:3),(Visible:True; XPos:65; YPos:3),
    (Visible:True; XPos:70; YPos:3),(Visible:True; XPos:75; YPos:3),
    (Visible:True; XPos:7; YPos:2),(Visible:True; XPos:11; YPos:2),
    (Visible:True; XPos:14; YPos:2),(Visible:True; XPos:18; YPos:2),
    (Visible:True; XPos:21; YPos:2),(Visible:True; XPos:26; YPos:2),
    (Visible:True; XPos:28; YPos:2),(Visible:True; XPos:33; YPos:2)));

    VAR
    CurrentLevel : Byte; { The level the user is currently on }
    Lives : Byte; { How many lives are left }
    Quit : Boolean; { True if Quit, otherwise False }

    PROCEDURE Level(LevelNum : Word);
    VAR
    BlockData : BlockSet;
    X : Byte;
    Begin
    Move(LevelInfo[LevelNum],BlockData,SizeOf(BlockData));
    For X := 1 to 40 Do
    Begin
    GotoXY(BlockData[X].XPos,BlockData[X].YPos);
    If BlockData[X].Visible Then
    Write('#')
    ELSE
    Write(' ');
    End;
    Repeat
    { All the game code here... }
    If Keypressed Then Quit := True;
    Until (Lives = 0) or (Quit);
    End;

    Begin
    ClrScr;
    Lives := 3;
    Quit := False;
    CurrentLevel := 0;
    Repeat
    Inc(CurrentLevel);
    Level(CurrentLevel);
    Until (Lives = 0) or (Quit);
    End.
    [/code]
  • : Wouldn't it be easier to just declare the "lives" variable as a
    : global variable? That would allow all procedures and functions
    : access to it without having to declare it for each procedure and
    : function.

    I'm with you, but alot of people consider that a bad programming habit. I dunno, Makes more sense to me and that's what I usually do in my programs ;)
  • I am still finding it difficult because I need to assign a variable in procedure level_1 to the variable in procedure level_2. can't it be something simple?
  • Isn't there anyway to assign variables to answers which are stored in different procedures pls?
  • : I'm with you, but alot of people consider that a bad programming
    : habit. I dunno, Makes more sense to me and that's what I usually do
    : in my programs ;)

    Yeah, I've also gotten the impression that a lot of people consider it "bad" programming. Don't know why, as in this day and age with gigabytes of memory, having a lot of global variables shouldn't be a problem.
  • : Isn't there anyway to assign variables to answers which are stored
    : in different procedures pls?
    :
    As I've previously told you: procedure parameters!

    [code]
    procedure Level_1(var quit: integer; var lives: integer);
    begin
    lives := lives -1;
    end;

    procedure Level_2(var quit: integer; var lives: integer);
    begin
    lives := lives -1;
    end;

    procedure Level_3(var quit: integer; var lives: integer);
    begin
    lives := lives -1;
    end;

    var
    quit: integer;
    Lives: integer;
    begin
    Lives := 3;
    Level_1(quit, Lives);
    writeln('Lives after level 1:', Lives);
    Level_2(quit, Lives);
    writeln('Lives after level 2:', Lives);
    Level_3(quit, Lives);
    writeln('Lives after level 3:', Lives);
    end.
    [/code]
    Run this code above and you'll see the lives decrease. This is the only way, unless you want to store some things in a global variable.
    Example of a global variable:

    [code]
    var
    Lives, quit: integer;

    procedure Level_1;
    begin
    lives := lives -1;
    end;

    procedure Level_2
    begin
    lives := lives -1;
    end;

    procedure Level_3;
    begin
    lives := lives -1;
    end;

    begin
    Lives := 3;
    Level_1;
    writeln('Lives after level 1:', Lives);
    Level_2;
    writeln('Lives after level 2:', Lives);
    Level_3;
    writeln('Lives after level 3:', Lives);
    end.
    [/code]
  • : Isn't there anyway to assign variables to answers which are stored
    : in different procedures pls?
    :

    Also, it is different for every program. If you want more specific help, post some/all of your code. We can only generalize since we don't know how you're using your variables and what your requirements are.

    Phat Nat
  • : Yeah, I've also gotten the impression that a lot of people consider
    : it "bad" programming. Don't know why, as in this day and age with
    : gigabytes of memory, having a lot of global variables shouldn't be a
    : problem.

    The reason it's considered bad programming is because of linkage between modules (in this case a "module" means a procedure and function). It has nothing to do with memory.

    No module should have access to any variable that it does not need to have access to. That way if a programmer changes a variable in one module it has no effect on a similarly named variable in another module. A global variable can be changed by any procedure/function. It's not hidden.

    Read ELEMENTS OF PROGRAMMING STYLE and SOFTWARE TOOLS IN PASCAL, both by Brian Kernighan and P.J. Plauger.

Sign In or Register to comment.

Howdy, Stranger!

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

Categories