Data Types in AGS

Started by dikla, Thu 06/05/2004 20:20:37

Previous topic - Next topic

dikla

 ???
i have 2 problems which i cannot salve (and believe me i tried):
1) i want to display a massage "you cant have it" when the player enter the room and touch the hotspot. after he use an inventory item, i wantÃ,  a new massage to appear "now you can take it". but whatever i try i keep getting one message after another on clicking the hand twice. (even after i used the item)the first click i get the 1st massage and the 2cd click gives the 2nd massage. can i just remove a massage?
2) i have a puzzle which the player has to click on several colours to get to the right color and than click on it and get some item. only if he will do it in the right order he will get the item. what script i should run?
thank you -
dikla

strazer

#1
2) For example:

Room script (Button {} in Room Editor/Settings)

int sequence=0;

Then in "Interact with object" or "Interact with hotspot" (depending on whether your colors are objects or hotspots):

Color 1

if (sequence == 0) sequence++;

Color 2

if (sequence == 1) sequence++;

Color 3 (last color of this combination)

if (sequence == 2) {
   // player gets item here
}
else sequence = 0;

EDIT: dikla asked me to clarify things:

You have to use scripting for this. If your colors are hotspots:

- Go to "Room Editor" / "Areas" and select "Hotspots" from the drop-down menu
- Select the hotspot for color 1 for example
- Press button "Interaction..."
- Double-click on "Interact with hotspot"
- Select "Run script" from the drop-down menu and press the "Edit script..." button
- Put the above code in here

"// player gets item here" is just a comment, you have to replace it with what you want to happen if the player gets the combination right.
You can add an inventory item with the AddInventory command. Look it up in the manual.

dikla

can you tell me how i end scripts? ( i once learn java script). and thankl you for your help again. i notice you script-man

strazer

You're welcome. :)

What do you mean, "end scripts"?
If you want to abort the execution of the rest of a script, afaik, you can write
return;

dikla

#4
no. i mean the syntax. dont we use a word or a sign to understand that this is the end of the script? in the end of dialon we write "stop". that is what i meant.

strazer

No.
If you take a look a the room script itself, any interaction looks basically like this:

#sectionstart object1_a  // DO NOT EDIT OR REMOVE THIS LINE
function object1_a() {
  // script for object1: Any click on object
  DisplaySpeech(EGO, "What a nice object!");
}
#sectionend object1_a  // DO NOT EDIT OR REMOVE THIS LINE

In the Interaction Editor, you only see the part between the braces { }
So the script functions already have a beginning and an end, so to speak, and you just don't see them in the Interaction Editor.

TerranRich

Yes, basically the ending brace } ends the script.
Status: Trying to come up with some ideas...

Ginny

But it's added automatically when you use the run script command from the interactions editor. Just making a note. :)

One thing I was wondering myself, if you write a custom function for example, and use return; at the end what does it return?

function Action1 (int param) {
param = GetPlayerCharacter(); //or something..
return;
//I know using return param; would return the player character, but sometimes return; is used alone..
}

Also, it's probably not a good idea to use: return GetPlayerCharacter();, right?
Try Not to Breathe - coming sooner or later!

We may have years, we may have hours, but sooner or later, we push up flowers. - Membrillo, Grim Fandango coroner

Scorpiorus

#8
Quote from: Ginny on Fri 07/05/2004 12:41:03One thing I was wondering myself, if you write a custom function for example, and use return; at the end what does it return?
If you don't specify a value it returns some garbage (i.e. some random values that were written into the register and then discarded). So, basically, you should always return something because there is a chance you call your function in a form of int value = MyFunction(); Returning a zero could be a good idea then. :)

QuoteAlso, it's probably not a good idea to use: return GetPlayerCharacter();, right?
It's alright in case the GetPlayerCharacter() function returns something (and it does).

You can also have:

function MyFunc(int a, int b) {
   return 3*a + b;
}

Thus, at first it evaluates 3*a + b expression and then returns the result value.

Ginny

Heh, thanks for the info :).
However, I don't think that if my funtion isn't meant to return something, like many others, there's no point telling it to return at all.. What happens if a function doesn't use return, and then accidentally is used this way:
int value = MyFunction();
Does the editor notice the bug or will it occur in the test?

Also, can any commands be used after return;? For example:

GetPlayerMood() {
player_char_mood = player_char_happy - player_char_sad

return player_char_mood;
player_char_mood = 0; // or "player_char_mood;", is this allowed?

}

Actually there isn't a point to use it in this case, but I couldn't think of anything better, and this function is a pretty silly example too.. :P

P.s. Soory for the hi-jack, I'm just curious. Trying to up my level of understanding of scripting.. ;)
Try Not to Breathe - coming sooner or later!

We may have years, we may have hours, but sooner or later, we push up flowers. - Membrillo, Grim Fandango coroner

Scorpiorus

#10
Quote from: Ginny on Fri 07/05/2004 13:46:40However, I don't think that if my funtion isn't meant to return something, like many others, there's no point telling it to return at all.. What happens if a function doesn't use return, and then accidentally is used this way:
int value = MyFunction();
Does the editor notice the bug or will it occur in the test?
That's a good point. If you are certainly sure you won't use its return value there is seemed to be no need to return anything. The problem is in such accidents you have mentioned. If you don't return a value then it works like if you put "return;" at the end of a function, i.e. returns a random value. While you are assuming the function to return something sensible it may return some invalid as well as a valid value. The returned value could be then passed as a parameter for some other function. Therefore, sometimes it may crash the game and sometimes it not. These kind of bugs might be difficult to replicate.
Currently, the script engine doesn't support declaration of functions which don't return a value, so the editor won't notice that fact.

QuoteAlso, can any commands be used after return;? For example:

GetPlayerMood() {
player_char_mood = player_char_happy - player_char_sad

return player_char_mood;
player_char_mood = 0; // or "player_char_mood;", is this allowed?

}
Yep, you can add a command after "return" but it won't be executed since on reaching the "return" function returns the control to the place it was called from (i.e. ends its execution). You can even have more than one "return". Although, generally it's considered to be a bad programming practice there are occasions here and there when putting several "return"s makes the life much easier. :)

QuoteP.s. Soory for the hi-jack, I'm just curious. Trying to up my level of understanding of scripting.. ;)
That's ok. Besides, this kind of stuff isn't mentioned in the manual. I only judge because the AGS script language is similar to "C". :)

~Cheers

Ginny

#11
Ok, I'll make sure to use return 0; in the future. :)

So, what's the point of writing something if it won't be executed? The only occasion I can see where this would be useful is for if statements, such as:

function OneTwoThree(int num) {

if (num == 1) {
return 1;
}
else if (num == 2) {
return 2;
}
else if (num == 3) {
return 3;
}
else {
return 0;
}

}

A useless function of course, but this is the only way I can spot to use return more than once and to have something after return too. I guess this isn't really a question, but I did wonder if there was another reason and way to do this (multiple returns I mean).

Thanks for all your enriching information! Is there anything else AGS supports or "doesn't support"? I.e. things not in the manual. I saw a struct declaration in a script, and I love #define already. I'll learn C# in 3 years in school, but it never hurts to learn a bit of introduction first. Though I understand this is probably very different, since it's object oriented (not quite sure what that means).
To the best of my knowledge, structs are like arrays of variables, looking like this:

struct object {
int size;
string name;
//etc..
}
and then reffered to as:
object_struct.size=7;
I'm not quite sure about it.
Can it be sued similarly to properties in VB? i.e.
object.property = value;

I don't quite get the char and short variables, and what they are good for.

edit:
Oh, I noticed there is an unanswered question here:
Quote from: dikla on Thu 06/05/2004 20:20:371) i want to display a massage "you cant have it" when the player enter the room and touch the hotspot. after he use an inventory item, i wantÃ,  a new massage to appear "now you can take it". but whatever i try i keep getting one message after another on clicking the hand twice. (even after i used the item)the first click i get the 1st massage and the 2cd click gives the 2nd massage. can i just remove a massage?

I don't know what script you used, but the way I would do it:

[If, after the player has used the inventory item on the hotspot, you want the normal hotspot interaction to display the 2nd message, add what I put in red also. Otherwise, ignore the red bits, it's jus that I didn't quite understand what you wanted to achieve. :)]


//at the top of the room script
int useditem;


//run script for intercation with hotspot

if (useditem == 1) {

Display("Now you can have it.");
}

else {

Display("You can't have it.");
}

//run script for use inventory on hotspot
if (character[GetPlayerCharacter()].activeinv == 4) {
//supposing 4 is the inventory item you need to use
Display("Now you can have it.");
useditem = 1;
}
Try Not to Breathe - coming sooner or later!

We may have years, we may have hours, but sooner or later, we push up flowers. - Membrillo, Grim Fandango coroner

Scorpiorus

#12
Quote from: Ginny on Fri 07/05/2004 16:54:42So, what's the point of writing something if it won't be executed? The only occasion I can see where this would be useful is for if statements, such as... I guess this isn't really a question, but I did wonder if there was another reason and way to do this (multiple returns I mean).
Extra returns can be used if, for example, there is an emergency event occured (like potential division by zero), so instead of immediately quiting with an error message it could return -1; or something so that the value can be checked in a way of:
int error = MyFunction();
if (error) {
// handle error
} else {
//proceed normally
}


Another situation if you want a function to be executed as fast as possible, for example:

An abs() function that returns an absolute value, i.e. abs(3)=3; abs(-5)=5; abs(-7)=7; abs(0)=0 etc.

You could implement it like:
function abs(int value) {
Ã,  Ã,  int result;
Ã,  Ã,  if (value < 0) result = -value;
Ã,  Ã,  else result = value;
Ã,  Ã,  return result;
}


However, a bit faster way is:

function abs(int value) {
Ã,  Ã,  if (value < 0) return -value;
Ã,  Ã,  else return value;
}


It's executed faster because we don't assign a value but return it instantly. :)


QuoteThanks for all your enriching information! Is there anything else AGS supports or "doesn't support"?
You are welcome :)
As for struct, yes, its a user-custom type that is defined by the programmer (equivelent of Basic's TYPE and Pascal's record):
//defining our own type SKILLS:
struct SKILLS {

Ã,  Ã,  int agility;
Ã,  Ã,  int magic;
Ã,  Ã,  int fighting;
};

//declaring a variable of type SKILLS

SKILLS hero_skills;

//next somewhere in script:

if (hero won the fight) hero_skills.fighting++;
...
if (hero_skills.magic < 30) Display("Your magic skill is too low.");

But bear in mind, you must not declare a string within the struct. Although, the compiler doesn't say anything it will lead to some serious errors, because strings are not supported within the struct!

QuoteI don't quite get the char and short variables, and what they are good for.
Well, they just take less memory. But it also defines how large value each of them is able to store, for instance:

int -2147483648 through 2147483647 //really large values
short -32768....32767
char 0....255

Though, you should usually use the int type. :)

p.s. I think we are little off compared to the original topic. Is there a way to somehow reorganize it into a separate topic, or is that too much hassle? Terran? Scummy? :)

SSH

#13
Quote from: Scorpiorus on Fri 07/05/2004 18:37:18
function abs(int value) {
    if (value < 0) return = -value;
    else return value;
}


Don't you mean:

function abs(int value) {
    if (value < 0) return -value; // no "=" here please!
    else return value;
}


Quote
But bear in mind, you must not declare a string within the struct. Although, the compiler doesn't say anything it will lead to some serious errors, because strings are not supported within the struct!

Although, strings are actually just the same as char[200], which CAN be used inside structs.

Quote
QuoteI don't quite get the char and short variables, and what they are good for.
Well, they just take less memory. But it also defines how large value each of them is able to store, for instance:

int -2147483648 through 2147483647 //really large values
short -32768....32767
char 0....255

Though, you should usually use the int type. :)

Unless faking strings in structs, of course...

I foudn out some interesting stuff my loading ROOMEDIT.CLB into a test editor. It was very handy for making the quick reference guide, but there were some other things too. For example:

#define function int

which means you can actually write things much more like C:

int abs (int value) {
   if (value < 0) return -value; // no "=" here please!
    else return value;
}

oh look... you don't need [ pre ] since the forum upgrade!
12

Ginny

#14
Yeah, reorganising should be nice, this topic has gotten carried away...
I guess I kinda made it more difficult by adding a reply for dikla (I always see this hebrew sounding names here! :P) in the off-topic post.
Maybe the off-topic part could be seperated (I could repost my reply to dikla). If others think they'd fins this useful, maybe it could even be moved to the tech archive.
------------------------------
Ah, so creating a struct creates a new variable type, in a way? Thus declaring a variable of this type means it can have the properties in the struct?
Is this the same as declaring each variable seperately, only faster? Such as:

int hero_fighting;
int hero_magic;
int villain_fighting;
int villain_magic;

Does the engine react negatively to an int called hero_skills.magic when it's not part of the struct? just wondering.

So, if strings cannot be declared in structs, is there a way to include one anyway?

For example:

struct char {

int alive;
//can a value such as 1 be assigned by default here?

//string name; is this usable?
//if not, is the only way to implement it declaring it outside the struct?

}

Structs, when reffered to in the script, are similar to text script global variables in the manual, right?

I also noticed char can store 1 letter character or a number 0-256, so it's usually used for pallette
what about this: char num[100]; does this limit characters to be 0-99/ 0-100? can letters still be used?

:)

One more thing (for now ;)):
Can int be 3.45 for example? or is it limited to "complete" numbers? same for char.

------------------------------
edit: SSH-
what does '#define function int' do? I

Odd, I also noticed the "=", and was going tomention it but then saw it had been edited ;).

I was going to ask you this is PM, but as long as you're here:
what do you mean char[200] is a fake string? Does the [200] specify that there can be 200 characters, thus identical to a string, excpet that numbers 0-256 can also be used?

I feel a lot clearer on ags scripting now. I guess it's my recent drive for scripting challnges that made me want to know every single detail and way things work in ags. :D
Try Not to Breathe - coming sooner or later!

We may have years, we may have hours, but sooner or later, we push up flowers. - Membrillo, Grim Fandango coroner

SSH

#15
Quote from: Ginny on Fri 07/05/2004 19:16:23
Yeah, reorganising should be nice, this topic has gotten carried away...
I guess I kinda made it more difficult by adding a reply for dikla (I always see this hebrew sounding names here! :P) in the off-topic post.

Indeed. Maybe this is best in the Technical Archive, renamed to "Data types in AGS" or something!

Quote
Ah, so creating a struct creates a new variable type, in a way? Thus declaring a variable of this type means it can have the properties in the struct?
Is this the same as declaring each variable seperately, only faster? Such as:

int hero_fighting;
int hero_magic;
int villain_fighting;
int villain_magic;

faster in the sense of less typing for you maybe, if the struct is quite big. Structs are also the onyl way to get mutli-dimensional arrays in AGS.

You have probably already used a struct.... character is one. In fact character is an array of structs, which is why you say:

character[arrayindex].room  or character[arrayindex].x

Quote
Does the engine react negatively to an int called hero_skills.magic when it's not part of the struct? just wondering.

I think so, although I can't actually recall seeing an error message myself. But then, I am perfect ;)

Quote
So, if strings cannot be declared in structs, is there a way to include one anyway?

You missed that bit in my previous post: yes, you have to say char[200] instead of string.

Quote
For example:

struct char {

but you can't call a struct type the same as an existing type, so you would have to say struct mychar {

Quote
int alive;
//can a value such as 1 be assigned by default here?

When you start getting into advanced stuff like this, don't count on anything being initialised. Set it manually in the game start function.

Quote
Structs, when reffered to in the script, are similar to text script global variables in the manual, right?

No.
First, once you have declared a struct type, you haven't yet created any variables, so to take Scorp's example:
//defining our own type SKILLS:
struct SKILLS {

    int agility;
    int magic;
    int fighting;
};

//declaring a variable of type SKILLS

SKILLS hero_skills;

// declare anothe one

SKILLS badguy_skills;

//or even an array of them

SKILLS char_skills[6];


Quote

I also noticed char can store 1 letter character or a number 0-256, so it's usually used for pallette
what about this: char num[100]; does this limit characters to be 0-99/ 0-100? can letters still be used?


No, this means you're declaring an array of 100 chars, from num[0] (which can hold 0-255) to num[99] which can also hold 0-255.

With structs you can have arrays of arrays:

struct mystruct {
   char sub[200];
}

mystruct twod[10];

You can then refer to:

twod[0].sub[0]  // first row, first column
twod[9].sub[199] // last row, last column

or anythign in between. Also, as I said before, an array fo 200 chars can be treated like a string.

Finally, you can have arrays of ints or shorts as well.



EDIT FOR GINNY'S EDIT:

OK, don't worry about the #define function int thing. But if you really want to know, it means in syntactical terms that functions are functions because they have brackets afetr their declaration, NOT because there is a function keyword there. It's a bit esoteric and is maybe interesting to COmputer Science students only (and maybe not even then :) )

as for char[200] being equivalent to strings, then thsi reall comes down to what a string is. A string is simply one character after another in memory. C , and AGS, tell where a string ends by the last character being '0'. However this behaviour is nearly entirely transparent in AGS. In fact, it doesn't actually work liek this, I believe, but CJ could have implemented AGS strings by putting

#define string char[200]

in the script header.
12

Scorpiorus

#16
Quote
Don't you mean:
function abs(int value) {

Ã,  Ã,  if (value < 0) return -value; // no "=" here please!

Ã,  Ã,  else return value;

}
Yeah, of course. I just copy&pasted the previous function and accidentally forgot to change that thing. Thanks for correction. :)

QuoteAlthough, strings are actually just the same as char[200], which CAN be used inside structs.
That's true. I just wanted to point out that the "string" keyword mustn't be used in struct. As I mentioned the compiler doesn't see that fault (because it's syntactically correct).


QuoteI foudn out some interesting stuff my loading ROOMEDIT.CLB into a test editor. It was very handy for making the quick reference guide, but there were some other things too. For example:

#define function int
Yep, that is it. I suspect some other "types" like void is actually defined as int.


Quoteoh look... you don't need [ pre ] since the forum upgrade!
hehe, I'd better stay with [ pre ], in case CJ will change that behaviour. ;D


Quotefaster in the sense of less typing for you maybe, if the struct is quite big. Structs are also the onyl way to get mutli-dimensional arrays in AGS.
To be more precise you can not access multi-dimensional arrays in a form of operator, i.e.
int array[10][10];

array[3][5] = 5;

but a function can be written that evaluates the target address. This way you can have anydimensional array, whereas with structs that's limited to 2d ones.


Quote
I believe, but CJ could have implemented AGS strings by putting

#define string char[200]
I think the string is defined as char* otherwise there would be no problems declaring them in structs. :P


[EDIT]:
QuoteCan int be 3.45 for example? or is it limited to "complete" numbers? same for char.
Nope, you can only have integer numbers. But that's easy to simulate:

3.45 = 345/100

thus, you can store 345 in the int variable and then divide it by 100 when needed, say:

int fixedA = 345; //  A = 3.45
int fixedB = 884; //  B = 8.84

//now let's add them and convert to integer:

int Integer = (fixedA + fixedB)/100;

so we get (345 + 884)/100 = 12;

whereas 3.45 + 8.84 = 12.29 (would be 12 if truncated)

that is it :)


SMF spam blocked by CleanTalk