Redefining an integer

Started by imperialdr, Fri 21/07/2023 03:50:29

Previous topic - Next topic

imperialdr

Hi again,
I'm making a game with a loop mechanic, and so I want to create a variable to track how many loops have occurred. I've defined 'Loop' as an integer in the global variables tab, with a default of 1.

In a dialog script, I've written: Loop = Loop + 1. I'm trying to increase this integer by 1.
Essentially, I want "Loop" to be at a value of 2. But stating "Loop = 2" didn't seem to work either.

After the variable change, the player is sent back to the beginning of the game. I've written this in the room script:

function cEcho_AnyClick()
{
  if (Loop == 1)
  {
dDialogL1.Start();
  }
  else if (Loop == 2)
  {
dDialogL2.Start();
  }
}

AGS lets me run it without error, but it doesn't do what I want. It only runs the first loop option and never accounts for the variable change. I'm not sure where my problem is. Any help would be greatly appreciated! :smiley:

Crimson Wizard

Quote from: imperialdr on Fri 21/07/2023 03:50:29After the variable change, the player is sent back to the beginning of the game.

How do you send them to the beginning of the game exactly?

Just in case you use RestartGame -- that essentially resets all the game data to the initial state. If you are using that, then the only way to save the loop value is to write a file and then read it back (using File script commands).

imperialdr

I just use player.ChangeRoom(1):

Crimson Wizard

Where do you set Loop to a new value, can you show your script?

imperialdr

#4
Currently, the Loop variable is supposed to change at the end of a dialog script.

Code: ags
// Dialog script file
@S  // Dialog startup entry point
  Display ("Some dialog...");
  Display ("Some dialog...");
  Display ("Some dialog...");
  Train.Play(); //sfx
  player.ChangeRoom(1); //player back to initial room
  Loop = Loop + 1; //change Loop to 2

As you can see I execute a few commands after the displays. The SFX, and room change work. The variable doesn't seem to. Then, in the script for Room 1, I try to check for the Loop value.

Code: ags
function cEcho_AnyClick()
{
  if (Loop ==1 ) //dialog L1
  {
dDialog1.Start();
  }
  else if (Loop == 2) //dialog L2
  {
cEcho.Say ("Test!");
  }
}


Snarky

Where is "Loop" declared? A common mistake is to declare it in a header: because of how headers work in AGS (they are copied into each script below), this actually creates multiple separate copies of the variable (one for each script, including the dialog script), and so changing one of them (e.g. the one in the dialog script) will have no effect on the others (e.g. the one in the room script).

The right way to do it is to either declare it in a script (not a header), export it in that script, and put the import statement in a header, or simply use the Global Variables pane.

imperialdr

I have only declared it in the global variables pane. I haven't used export, import, or anything like that. Should I?

imperialdr

Ah OKAY. Actually, I just did something dumb. The variable WAS trying to work, but I had written a different script earlier that was messing everything up. I forgot about it. I got rid of it, and everything's working now.
Thanks for all of your replies.

Crimson Wizard

Hmm, I suspected something else was affecting this.
But I'd like to add following for the reference:
Code: ags
player.ChangeRoom(1); //player back to initial room
Loop = Loop + 1; //change Loop to 2
In AGS room change is performed only after the script is finished, so ChangeRoom call is scheduled. This means that "Loop = Loop + 1" will be performed before the room is changed. Normally it's advised to call ChangeRoom last, to avoid misinterpretations.

imperialdr

Appreciate the advice. Thanks!

SMF spam blocked by CleanTalk