Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Fabiano on Mon 19/12/2005 20:40:41

Title: Struct-Int problems when using in a String Format (SOLVED)
Post by: Fabiano on Mon 19/12/2005 20:40:41
Ok, this is problably a very dumb question.

My code is like this:

#sectionstart game_start  // DO NOT EDIT OR REMOVE THIS LINE
function game_start() {

struct Charas {
int str;
int dex;
int lvl;
};

Charas Stiletto;
Stiletto.str = 10;
Stiletto.dex = 8;
Stiletto.lvl = 1;

#sectionstart repeatedly_execute  // DO NOT EDIT OR REMOVE THIS LINE
function repeatedly_execute() {

int str;
int dex;
int lvl;

StrFormat(str, "d%", Stiletto.str);
SetLabelText(2, 5, str);



It return a "Stiletto - Undefined Token". What I'm doing wrong?





Title: Re: Struct-Int problems when using in a String Format
Post by: DoorKnobHandle on Mon 19/12/2005 20:43:10
(1) Move the struct definition outside of the "game_start" function and to the top of your script.

(2) The function StrFormat expects variables of string-type and not integers as you define them. With the newest version you can just change Stilette.str to a (const) string and int str to string str.
Title: Re: Struct-Int problems when using in a String Format
Post by: Fabiano on Mon 19/12/2005 20:58:45
Thanks Dkh for the fast reply. I didnt know about putting the structs in the top of the script, thanks, but I wrote wrong in the above code. the right is


#sectionstart repeatedly_execute  // DO NOT EDIT OR REMOVE THIS LINE
function repeatedly_execute() {

String str;
String dex;
String lvl;

StrFormat(str, "d%", Stiletto.str);
SetLabelText(2, 5, str);
Title: Re: Struct-Int problems when using in a String Format
Post by: Ashen on Mon 19/12/2005 21:12:26
One thing I missed to start with - it's %d, not d% (don't know if that's how you've got it in the script, or just a typo here).

It looks like you've mixed up about 3 'generations' of code in there ....

SetLabelText is from pre 2.7 (it works in 2.7, but isn't the 'right' way), String (as opposed to string) is 2.71, and StrFormat doesn't work on Strings (AFAIK). Which version are you actually using?

For 2.7, it should be:

string str;
string dex;
string lvl;

StrFormat(str, "d%", Stiletto.str);
lblStr.SetText(str); // Replace 'lblStr' with the script name of the label


(If you don't have a script name for the Label, you can use gui[2].Controls[5].AsLabel)

For 2.71, I think it's:

String str = String.Format("%d", Stiletto.str);
String dex;
String lvl;

lblStr.Text = str; // Replace 'lblStr' with the script name of the label
Title: Re: Struct-Int problems when using in a String Format
Post by: Fabiano on Mon 19/12/2005 21:43:21
Oi.

Well, about the %d e the %d I wrote here wrong (again). I'm using the 2.71 version, but I guess my mind dont understand OO script very well, so I still use some "old script".

I just tried the 2.71 code and it become like this:


// main global script file
struct Charas{
  int str;
  };

#sectionstart game_start  // DO NOT EDIT OR REMOVE THIS LINE
function game_start() {
  // called when the game starts, before the first room is loaded

Charas Stiletto;
Stiletto.str = 10;

#sectionstart repeatedly_execute  // DO NOT EDIT OR REMOVE THIS LINE
function repeatedly_execute() {
  // put anything you want to happen every game cycle here

String str = String.Format("%d", Stiletto.str);
strlabel.Text = str;


Returned "undefined Symbol - Stiletto"

I tried the 2.7 code and returned "undefined Symbol - Stiletto" too

:'( I guess structs dont mix with String format.



Title: Re: Struct-Int problems when using in a String Format
Post by: Ashen on Mon 19/12/2005 22:25:53
Variables (strings, Strings and ints) declared inside a function can only be accessed in that function - so, like the struct, Stiletto needs to be declared outside of game_start. Move the Charas Stiletto; line to just after the struct declaration. You should still set your variables in game_start, however (leave Stiletto.str = 10; where it is).

So:

// Main global script file
struct Charas{
  int str;
};
Charas Stiletto;

#sectionstart game_start  // DO NOT EDIT OR REMOVE THIS LINE
function game_start() {
  // called when the game starts, before the first room is loaded
  Stiletto.str = 10;

#sectionstart repeatedly_execute  // DO NOT EDIT OR REMOVE THIS LINE
function repeatedly_execute() {
  // put anything you want to happen every game cycle here

  String str = String.Format("%d", Stiletto.str);
  strlabel.Text = str;


Title: Re: Struct-Int problems when using in a String Format
Post by: Fabiano on Mon 19/12/2005 22:34:47
Worked like a charm!  :o Thanks Ashen and Dkh.

(mind note: MUST LEARN OO Script)