Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: ESGmaster on Tue 24/02/2009 03:00:47

Title: Changing Global Variables
Post by: ESGmaster on Tue 24/02/2009 03:00:47
How do I change A global variable?
The game I'm working on requires the item to use the global variables to reload the guns.
Everytime I try to change it by:
Ruger_P94_Ammo + 10
And it says it is already set.
Title: Re: Changing Global Variables
Post by: Gilbert on Tue 24/02/2009 03:26:23
To change a global variable, you need to write it like this:
Ruger_P94_Ammo = Ruger_P94_Ammo + 10;

(Or, the shorten form Ruger_P94_Ammo += 10 which I personally don't recommend people to write unless they're familiar with scripting.)
Title: Re: Changing Global Variables
Post by: ESGmaster on Tue 24/02/2009 03:45:33
That worked unfortunely I have another problem.
It saids there's A parse error on line 550
I don't know what's wrong here's the script:
function iRUGERP94_UseInv()
{
if (player.ActiveInventory.ID == Ruger_P94_Ammo)
{
Ruger_P94_Ammo = Ruger_P94_Ammo + 10;
player.LoseInventory (i9mmclip10)
}
else Display ("No")
}
Title: Re: Changing Global Variables
Post by: Gilbert on Tue 24/02/2009 03:51:35
You are missing the semi-colons which are essential to signify the ends of script lines:

function iRUGERP94_UseInv()
{
  if (player.ActiveInventory.ID == Ruger_P94_Ammo)
  {
     Ruger_P94_Ammo = Ruger_P94_Ammo + 10;
     player.LoseInventory (i9mmclip10);
  }
   else Display ("No");
}

Title: Re: Changing Global Variables
Post by: ESGmaster on Tue 24/02/2009 04:02:31
Thanks, as you can tell I am bad at scripting.
EDIT:Sorry it has A error. (and I made A error before with the item)
function iRUGERP94_UseInv()
{
  if (player.ActiveInventory.Name=i9mmclip10);
  {
     Ruger_P94_Ammo = Ruger_P94_Ammo + 10;
     player.LoseInventory (i9mmclip10);
  }
   else Display ("No");
}
Title: Re: Changing Global Variables
Post by: Creator on Tue 24/02/2009 05:50:09
Quote from: ESGmaster on Tue 24/02/2009 04:02:31
function iRUGERP94_UseInv()
{
  if (player.ActiveInventory.Name=i9mmclip10);
  {
     Ruger_P94_Ammo = Ruger_P94_Ammo + 10;
     player.LoseInventory (i9mmclip10);
  }
   else Display ("No");
}

It should be:


function iRUGERP94_UseInv() {
  if (player.ActiveInventory.Name == i9mmclip10) {
    Ruger_P94_Ammo = Ruger_P94_Ammo + 10;
    player.LoseInventory(i9mmclip10);
  }
  else {
    Display("No");
  }
}


(Braces are moved around so you can understand the last part of the message below)

You need 2 equals signs when checking a variable and 1 equals sign when setting a variable. You also don't need a semicolon ( ; ) when you are writing functions. Basically, when writing a function (in this case it's ActiveInventory) you need a brace (both opening and closing) instead of a semicolon.

It's hard for me to explain. Do you understand what I mean?
Title: Re: Changing Global Variables
Post by: Khris on Tue 24/02/2009 08:38:29
function iRUGERP94_UseInv()
{
  if (player.ActiveInventory == i9mmclip10);
  {
     Ruger_P94_Ammo = Ruger_P94_Ammo + 10;
     player.LoseInventory (i9mmclip10);
  }
  else Display ("No");
}

You need player.ActiveInventory. It's a pointer of type InventoryItem, so you can compare it directly to i9mmclip10.
player.ActiveInventory.Name is a String containing the item's description.
player.ActiveInventory.ID is an integer variable containing the item's number from the editor list.
Title: Re: Changing Global Variables
Post by: ESGmaster on Tue 24/02/2009 18:20:21
That worked but it says there's A parse error on line 550, which is the if line.
I don't know that much about coding.
function iRUGERP94_UseInv() {
  if (player.ActiveInventory.ID = i9mmclip10) {
    Ruger_P94_Ammo = Ruger_P94_Ammo + 10;
    player.LoseInventory(i9mmclip10);
  }
  else {
    Display("No");
  }
}
Title: Re: Changing Global Variables
Post by: Matti on Tue 24/02/2009 18:26:16
As creator already said: "You need 2 equals signs when checking a variable and 1 equals sign when setting a variable."

So just change "=" in "==".
Title: Re: Changing Global Variables
Post by: ESGmaster on Tue 24/02/2009 18:31:48
I tried it before, but I set it back. (I am not good at coding.)
Now it says it cannot convert 'int' to InventoryItem.

function iRUGERP94_UseInv() {
  if (player.ActiveInventory.ID == i9mmclip10) {
    Ruger_P94_Ammo = Ruger_P94_Ammo + 10;
    player.LoseInventory(i9mmclip10);
  }
  else {
    Display("No");
  }
}
Title: Re: Changing Global Variables
Post by: Mazoliin on Tue 24/02/2009 19:04:57
It's because you have the "ID" after ActiveInventory. Then the function want's the inventory items ID, it's number, to check. To fix it just remove the ID, like this:

if (player.ActiveInventory == i9mmclip10) {


or use the inventory items ID, like this:

if (player.ActiveInventory.ID == i9mmclip10.ID) {


but the first one is preferable since the second one's just a harder way to achive the same thing
Title: Re: Changing Global Variables
Post by: Khris on Tue 24/02/2009 19:48:37
Woooo!

(http://www.2dadventure.com/ags/_equal_signs.png)
Title: Re: Changing Global Variables
Post by: ESGmaster on Wed 25/02/2009 06:25:48
Sorry, I don't know anything about coding.
It says that there's an undefined token 'Ruger_P94_Ammo' (sorry I am horrid at coding.)

function iRUGERP94_UseInv() {
if (player.ActiveInventory == i9mmclip10) {
    Ruger_P94_Ammo = Ruger_P94_Ammo + 10;
    player.LoseInventory(i9mmclip10);
  }
  else {
    Display("No");
  }
}
Title: Re: Changing Global Variables
Post by: Gilbert on Wed 25/02/2009 06:35:02
Hmmm Did you define the variable Ruger_P94_Ammo at all?

It's odd that you have used this variable from the beginning but did get a similar error before.
Title: Re: Changing Global Variables
Post by: ESGmaster on Wed 25/02/2009 06:44:19
I made a mistake in the variable I said it was Ruger_P94_Ammo it was RUGER_P94_Ammo.
Sorry, I'm an idiot.
Thanks, I was looking for something hard and it was in plain sight. (as in The Purloined Letter.)
Title: Re: Changing Global Variables
Post by: IndieBoy on Wed 25/02/2009 16:25:46
Sorry for being off topic, but just to say I really like KhrisMUC's drawing, as I had this same problem mixing up equal signs when I just started to learn how to script. However I think the single equal sign looks a bit like a natzi..
Title: Re: Changing Global Variables
Post by: Khris on Wed 25/02/2009 16:43:27
Heh, didn't see the signs as moustaches until now :)
I wanted to draw the eyes and mouths closer to the signs originally, partly covering them, but chose not to.

Btw, this drawing can be freely used, I hereby declare it public domain ;)
Does anybody still have the RTFM-Roger-Smiley?