Sub-scripts / "Labels" / GoTo (SOLVED)

Started by Akumayo, Mon 06/06/2005 00:02:33

Previous topic - Next topic

Akumayo

     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.
"Power is not a means - it is an end."

Redwall

Er. . . you mean a function?
aka Nur-ab-sal

"Fixed is not unbroken."

Akumayo

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.
"Power is not a means - it is an end."

Rui 'Trovatore' Pires

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.
Reach for the moon. Even if you miss, you'll land among the stars.

Kneel. Now.

Never throw chicken at a Leprechaun.

ilSilente

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)

scotch

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:

Code: ags

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:

Code: ags

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:

Code: ags

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

Code: ags

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:

Code: ags

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.

Pumaman

Indeed, goto/label is the old fashioned way of doing functions.

I think scotch has neatly summed up how to do it in AGS :)

Akumayo

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!
"Power is not a means - it is an end."

SMF spam blocked by CleanTalk