Struct-Int problems when using in a String Format (SOLVED)

Started by Fabiano, Mon 19/12/2005 20:40:41

Previous topic - Next topic

Fabiano

Ok, this is problably a very dumb question.

My code is like this:
Code: ags

#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?





Yeah, yeah, It happens. A lot.

DoorKnobHandle

(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.

Fabiano

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

Code: ags

#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);
Yeah, yeah, It happens. A lot.

Ashen

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:
Code: ags

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:
Code: ags

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

lblStr.Text = str; // Replace 'lblStr' with the script name of the label
I know what you're thinking ... Don't think that.

Fabiano

#4
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:

Code: ags

// 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.



Yeah, yeah, It happens. A lot.

Ashen

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:
Code: ags

// 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;


I know what you're thinking ... Don't think that.

Fabiano

Worked like a charm!  :o Thanks Ashen and Dkh.

(mind note: MUST LEARN OO Script)


Yeah, yeah, It happens. A lot.

SMF spam blocked by CleanTalk