Changing Global Variables

Started by ESGmaster, Tue 24/02/2009 03:00:47

Previous topic - Next topic

ESGmaster

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.

Gilbert

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

ESGmaster

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")
}

Gilbert

You are missing the semi-colons which are essential to signify the ends of script lines:
Code: ags

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


ESGmaster

#4
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");
}

Creator

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:

Code: ags

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?

Khris

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

ESGmaster

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");
  }
}

Matti

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 "==".

ESGmaster

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");
  }
}

Mazoliin

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

if (player.ActiveInventory == i9mmclip10) {


or use the inventory items ID, like this:
Code: ags

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

Khris


ESGmaster

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");
  }
}

Gilbert

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.

ESGmaster

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

IndieBoy

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..
Quote from: Calin Elephantsittingonface on Tue 08/02/2011 09:00:55
The only person in favour of the mobs seems to be IndieBoy.. but he's scottish so we dont listen to him anyway.

Khris

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?

SMF spam blocked by CleanTalk