Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: imperialdr on Fri 21/07/2023 03:50:29

Title: Redefining an integer
Post by: imperialdr on Fri 21/07/2023 03:50:29
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:
Title: Re: Redefining an integer
Post by: Crimson Wizard on Fri 21/07/2023 04:14:54
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).
Title: Re: Redefining an integer
Post by: imperialdr on Fri 21/07/2023 04:58:47
I just use player.ChangeRoom(1):
Title: Re: Redefining an integer
Post by: Crimson Wizard on Fri 21/07/2023 05:49:12
Where do you set Loop to a new value, can you show your script?
Title: Re: Redefining an integer
Post by: imperialdr on Fri 21/07/2023 14:27:08
Currently, the Loop variable is supposed to change at the end of a dialog script.

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

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

Title: Re: Redefining an integer
Post by: Snarky on Fri 21/07/2023 15:36:40
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.
Title: Re: Redefining an integer
Post by: imperialdr on Fri 21/07/2023 15:59:21
I have only declared it in the global variables pane. I haven't used export, import, or anything like that. Should I?
Title: Re: Redefining an integer
Post by: imperialdr on Fri 21/07/2023 16:39:28
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.
Title: Re: Redefining an integer
Post by: Crimson Wizard on Fri 21/07/2023 16:46:08
Hmm, I suspected something else was affecting this.
But I'd like to add following for the reference:
Code (ags) Select
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.
Title: Re: Redefining an integer
Post by: imperialdr on Fri 21/07/2023 18:08:15
Appreciate the advice. Thanks!