Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: on Fri 23/04/2004 13:01:02

Title: Scripting quest. concerning variables
Post by: on Fri 23/04/2004 13:01:02
Hi.

My game uses several player characters which can be chosen at will, sort of Maniac Mansion style.
As I like each character to behave differently when interacting with objects, it is necessary to check, who is the current player character, everytime an action is performed. To save me some typing I put:

int n = GetPlayerCharacter ();

at the head of every object or hotspot scripts, followed by if..then choices:

if (n == EGO) {
...
}
else {
...
}

etc.

My simple question is, is there way to declare n as a kind of global variable, so I don't have to repeat defining it at the beginning of every script?

Thanx in advance,
TB
Title: Re:Scripting quest. concerning variables
Post by: Ben on Fri 23/04/2004 17:02:02
Instead of a variable, you could try function with a shorter name.. In the global script, put this:

function n() {
 return GetPlayerCharacter()
}

and in the script header, put this, so you can use the function in all your scripts:

import n()


Another idea: You could define the variable in each mouse click. Just put "int n = GetPlayerCharacter();" in the on_mouse_click section of the global script.
Title: Re:Scripting quest. concerning variables
Post by: Proskrito on Fri 23/04/2004 17:14:59
to make that kind of 'global' variable, you can define it at the top of the global script, then export it at the bottom (i think this is necessary), and import it in the script header.
Or directly define it in the script header may work aswell.


Title: Re:Scripting quest. concerning variables
Post by: Hollister Man on Sat 24/04/2004 15:37:47
Is there a reason not to use:

if(getplayercharacter()==EGO){}    ???

really all it takes is typeing getp then scrolling to the right line in the popup box.  Pretty quick really, and it saves you from that fiddly variable stuff, that's why it works that way.  You can even say,

MoveCharacter(getplayercharacter(), 120, 203);  // if you want.  That way it never says:  CHARACTER NOT IN ROOM!!!  ARRGH I HATED THAT! :)
Title: Re:Scripting quest. concerning variables
Post by: on Sat 24/04/2004 22:16:46
Hi.

@Hollister Man

Quote:
"Is there a reason not to use:
if(getplayercharacter()==EGO){}"

Yes, there is, it kind of gets on one's nerves after typing GetPlayerCh... for the 10^100th time (keep in mind, its not only in the if...s, but also in all the Move-, Displayspeech- and what not else relevant character commands). ;-)


@Ben
The mouse-click thing sounded good first, but as n is also needed in PlayerEnters/Leaves-Screen-functions its not an overall solution. I tried defining n in the globalscript and then exporting it, but the programm doesn't accept "int n = GetPlayerCharacter ()"  if its not included in a function. I'm not very experienced with importing/exporting functions/variables and the script header, but I'm trying to figure things out. Thanx for your thoughts.


@Proskito
Defining variables in the script header doesn't seem to work. I'm still having problems when it comes to importing variables/functions. Must come to grips with the script header (never used it before). Thanx for your advice.


Greetings,
TB

Title: Re:Scripting quest. concerning variables
Post by: Hollister Man on Sun 25/04/2004 21:00:33
I think I might see the problem.

Quote
int n = GetPlayerCharacter ();
...
if (n == EGO)

you declared "n" as an int, then tried to check for if it was equal to a string, could that be it?  Have you tried using the character's actual number?  Dunno for sure, though.  You might put this in the Tech Forum with a different description, such as, "Shorter use of GetPlayerCharacter()?" or whatever.

Personally, if it gets annoying, I'd get one of those nifty "clipboard" programs that allows you to make a hotkey to paste that particular bit of code any time you want.  Works for other things too, but I can't remember the name.
Title: Re:Scripting quest. concerning variables
Post by: Alynn on Mon 26/04/2004 11:43:54
Quote from: Hollister Man on Sun 25/04/2004 21:00:33
I think I might see the problem.

Quote
int n = GetPlayerCharacter ();
...
if (n == EGO)

you declared "n" as an int, then tried to check for if it was equal to a string, could that be it?  Have you tried using the character's actual number?  Dunno for sure, though.  You might put this in the Tech Forum with a different description, such as, "Shorter use of GetPlayerCharacter()?" or whatever.

Personally, if it gets annoying, I'd get one of those nifty "clipboard" programs that allows you to make a hotkey to paste that particular bit of code any time you want.  Works for other things too, but I can't remember the name.

Hollister, just FYI, if you look on your character area, you will see that it has a text box for script name, this is basically the global variable for that characters number for instance in my game

ALYNN = 0
ROMAN = 1
FLAME = 2
KNIG = 3
and so on and so forth...


TB.... you don't really have to type GetPlayerCharacter() over and over, here is something simple I do... just copy and paste so you write

if (GetPlayerCharacter()==NAME){}

copy it and paste it as many times as you need, then you only need to change NAME to the name you need, and do the code for it. It saves you quite a bit of typing.
Title: Re:Scripting quest. concerning variables
Post by: Ginny on Mon 26/04/2004 15:07:49
Umm, one thing I was wondering was wether GetPlayerCharacter actually returned the name of the char or the number. If the latter, then you can do this in repeatedly_execute or repeatedly_execute_always (in the new beta version, but backup your game before using a beta):

SetGlobalInt(0, GetPlayerCharacter());

and then use GetGlobalInt(0);
Then again, this doesn't help much, as it's just as much hassle to write as the original option..

I don't see why it wouldn't let you set int n = GetPlayerCharacter() at the top of the script. But in this case, you could do: int n;

and in repeatedly_execute (or _always, as that lets it happen even during blocking commands):

n = GetPlayerCharacter;

as for exporting and importing this variable for use in rooms, I'm no expert on it, but as the manual says:

export n;
//at the end of the global script. I'd put it before the different inventory interactions you may have just for notmaking things messy, though of course it may not work then, so try both ways

and in the "Script header" (a seperate script file in the same menu as the global script) put:

import int n;

That should work so you can use it anywhere.
I don't think defining ints in the header it possible though.

Exporting a function is the same, except you don't use the export thing at all.

import function n();
in the script header. When importing functions with parameters you use just the types, btw.

I just noticed Ben actually wrote the same thing, anyway. using a function for this is Imo the cleanest solution. :)

edit: Whew, I am so lucky this posted before I accidentally closed the window. ;)