Problem with strings.

Started by joelphilippage, Fri 10/08/2007 04:43:08

Previous topic - Next topic

joelphilippage

Hello I am trying to write sort of a code gui so you can click buttons in a certain order and get something to happen.
This is in repeatedly Execute
Code: ags
  if (Game.GlobalStrings[1] == ".RYRO" {
    cEgo.Say("I just did a draft!");
    }

It keeps saying that the expression was met in the middle of the command.



monkey0506

  if (Game.GlobalStrings[1] == ".RYRO") {
    cEgo.Say("I just did a draft!");
    }


Check your parentheses! ;)

joelphilippage

#2
Whoops that was stupid. Anyway Ive got another problem. I am tring to ad to that string when you click a button.
I have this in game start.
Code: ags

Game.GlobalStrings[1] = ".");

how would I add a letter after that period when the player clicks a button?



Gilbert

Either one would work:

Game.GlobalStrings[1] = Game.GlobalStrings[1].AppendChar('a');
Game.GlobalStrings[1] = Game.GlobalStrings[1].Append("a");
Game.GlobalStrings[1] = Strings.Format("%sa", Game.GlobalStrings[1]);

joelphilippage

#4
It shows the error: "Error (line 80): Must have the instance of the struct to access a non-static member."
Line 80 is the line
Code: ags
Game.GlobalStrings[1] = Game.GlobalStrings[1].AppendChar('R');


Edit: Never mind. I finally got it to work now. I just had to use the last command you supplied.



Gilbert

Yeah, I just checked, since the Game.GlobalStrings[] are considered static, it seem that we can't use Append() or AppendChar() directly.

Anyway the last method using Format() would work, any problem to that?

(If you really want to use Append() or AppendChar(), just do something like:
String aa = Game.GlobalStrings[1];
Game.GlobalStrings[1] = aa.AppendChar('R');
But IMO there isn't much point as you can use Format(). )

joelphilippage

Sorry I'm having another problem. I'm trying to get it so It will check the length of the string before it checks the sequence. It is having the same "must have instance of struct" problem as before except this time the problem is the length command.
Code: ags
 if (Game.GlobalStrings[1].Length == 4){
if (Game.GlobalStrings[1] == ".RYRO") {
    cEgo.Say("I just did a draft!");
    Game.GlobalStrings[1] = ".";
}
else {
  cEgo.Say("I guess that isin't a draft.");
  Game.GlobalStrings[1] = ".";
}
}

What do I keep doing wrong?



Gilbert

The problem is, Game.GlobalStrings[] are declared as static by the engine (I'm not quite sure why it has to be done that way), so struct function that operates on specific (an instant) Strings won't work.

So the solution is either:
1. Use an intermediate instant of String for that, thus:
Code: ags

  String aa = Game.GlobalStrings[1];
  if (aa.Length == 4){
  ...

  or
2. Don't use the GlobalStrings, declare a proper String variable for that purpose, so:
   Top of global script:
Code: ags

  String ustring;
  export ustring;

Then in script header:
Code: ags
  import String ustring

  This way you can use ustring instead of the GlobalStrings, and all those functions that act on an instance will work.



joelphilippage

Thanks for the explanation. It's working great now.



monkey0506

#9
Well just because the property is static doesn't mean you shouldn't be allowed to access its properties normally. The GlobalStrings property being declared a static property of the Game struct simply means that you access it using the name of the struct (Game) directly instead of calling it on an instance of the struct. You're not actually accessing the String struct statically, you're accessing an instance of the String struct which means you should have access to the instance's properties/functions. This is a bug in the system...

[EDIT:]

Actually now that I think of it, what I said was the problem before wouldn't be producing a compile-time error anyway. CJ says he knows about the problem and could fix it, it's simply not high priority. ;)

SMF spam blocked by CleanTalk