Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: Akumayo on Mon 06/06/2005 00:02:33

Title: Sub-scripts / "Labels" / GoTo (SOLVED)
Post by: Akumayo on Mon 06/06/2005 00:02:33
     Over the course of the school year my need for AGS was lessened by one thing, a TI-83 Calculator.  I took the time to learn the programming language used to make programs that can do the pythagorean therym, find missing angles, etc.  And I turned my calculator into a text based game centre.  I have both a basic RPG and a casino with roulette, card draw, dice, slot machines, and a "luck wheel".  About now you are asking my point, right?

     The TI allows a script function called a "Label"  It is more like a sub-script.  You type in "Goto A" and it searches your script for a line with "Lbl A"  On it.  It starts running script from the "Lbl A" line.  You can type a command, "Return" anytime after "Lbl A" and the script runner jumps to the line directly under "Goto A"

     My Point?  This allows for an all out game.  Particularly, it allows for RPG's.  Using the Labels (sub-scripts) you can quickly loop scripts for attacking, checking for HP below 0, and rewarding the player for a win.  My point is this, does AGS have any sort of sub-script system like this?  If so, I need to know how to access said system.

     All comments are appreciated.
Title: Re: Sub Scripts?
Post by: Redwall on Mon 06/06/2005 01:18:04
Er. . . you mean a function?
Title: Re: Sub Scripts?
Post by: Akumayo on Mon 06/06/2005 01:22:10
I'm not sure, just a place in the script that is completely unaccessable unless called upon, and can immediatly transport you back to the script that called it.
Title: Re: Sub Scripts?
Post by: Rui 'Trovatore' Pires on Mon 06/06/2005 07:57:36
You do have "CallRoomScript", I guess... and clever use of variables and repeteadly execute might emulate that quite nicely, at least from the player's POV - from the designer's it might be a bit messy.

But, it doesn't seem to me that is really an issue with AGS - if you're using AGS, it's very likely you'll find other ways to do stuff like that. Each program has its own way of working - while some programs work with such labels (like ZZT-OOP and MEGAZEUX and qBASIC, which I remember from my school days) AGS simply doesn't need them due to the way it... works... processes... thingies.
Title: Re: Sub Scripts?
Post by: ilSilente on Mon 06/06/2005 09:20:17
In modern languages GoTo/Label statements are deprecated (and anyway they are not well-see by programmers due to maintenance difficulties) and replaced by loops, functions etc.

That's why AGS don't support them (or, at least, I think)
Title: Re: Sub Scripts?
Post by: scotch on Mon 06/06/2005 09:25:17
It's true goto statements are rarely used in modern languages, they do exist but are often considered bad programming.

Does sound like you do just want a function, they are like gotos but a lot more useful.
Say you wanted to make the character walk 100 pixels to the right and say something, but you wanted to use this all over the place in the script, in different functions - at the top of your room script you'd make one like:


function walkEastAndTalk() {
// inside the curly braces goes your code:
cEGO.Walk(cEGO.x + 100, cEGO.y, 1, 0);
cEGO.Say("I'm here.");
}


then you can call it from other places in the room script just by using the command walkEastAndTalk(); Ã, it will enter that function, excecute the commands and return to the place where it was called.
Ã,  Ã, You can also make your function take parameters, to change what it does. Ã, For example if we wanted to be able to tell that function how far to walk, instead of it always walking 100 pixels then you could change it to this:


function walkEastAndTalk(int howfar) {
// inside the curly braces goes your code:
cEGO.Walk(cEGO.x + howfar, cEGO.y, 1, 0);
cEGO.Say("I'm here.");
}


now you have to specify a distance when you call it like walkEastAndTalk(250); would make him walk 250 units instead.
Ã,  Ã, Another thing you can do with functions is to return a value. Ã, For example, if you want a function to check if the character was standing in a particular rectangle on the screen you could do this:


function characterIsInRectangle(int x1, int y1, int x2, int y2) {
// check if they are inside these coordinates
if(cEgo.x>x1 && cEgo.y>y1 && cEgo.x<x2 && xEgo.y<y2) return 1; // return 1 if they are
else return 0; // otherwise return 0
}


then you could use the returned value to make decisions when you call it, like


if(characterIsInRectangle(200,200,400,300) == 1) {
doStuffBecauseTheyAreInRectangle();
} else {
doOtherStuff;
}


Now, if you want a function to be callable in more than one room, you need to write it in the global script, and make sure that the room scripts are aware of it by adding an import command to the Script Header (press ctrl+H to edit the header). Ã, For example, for the previous function to be usable in room scripts you'd write the following in the script header:


import function characterIsInRectangle(int, int, int, int);


this tells all the scripts name of the function, and the TYPES of the parameters that it takes, so you can call it anywhere in the game.
Title: Re: Sub Scripts?
Post by: Pumaman on Mon 06/06/2005 18:48:38
Indeed, goto/label is the old fashioned way of doing functions.

I think scotch has neatly summed up how to do it in AGS :)
Title: Re: Sub Scripts?
Post by: Akumayo on Mon 06/06/2005 20:21:48
oh goody, thanks scotch!  That helps alot, I had considered that function before, but I wasn't sure if it returned to the place it was called.  Thanks alot!  I can do that!