Jump to content


Photo

Changing a string inside a variable?


  • Please log in to reply
3 replies to this topic

#1 Spiritwolf

Spiritwolf

    GMC Member

  • GMC Member
  • 31 posts

Posted 04 March 2012 - 07:59 PM

Hopefully this makes sense.

Here are 3 variables:
global.iteminventory0[2]
global.iteminventory1[2]
global.iteminventory2[2]

Here is the code:
if global.iteminventory0[1] = "Least Potion"
{global.iteminventory0[2] -= 1
global.life += 50}

I pretty much have the code set as a script, and I want to use arguments to specify the number after inventory (something like global.iteminventory(argument0) so that I don't have to make several copies of the script for global.iteminventory0, global.iteminventory1 etc.

Any ideas?
  • 0

#2 GameGeisha

GameGeisha

    GameGeisha

  • GMC Member
  • 2743 posts
  • Version:GM:Studio

Posted 04 March 2012 - 08:06 PM

In your case, use a 2D array instead of multiple 1D arrays to store your inventories.

To set it up:
var i;
for (i = 0; i < 3; i += 1) 
  global.iteminventory[i, 0] = ""; //What goes here?
  global.iteminventory[i, 1] = ""; //Name?
  global.iteminventory[i, 2] = 0; //Amount?
}

Then to reference it:
if (global.iteminventory[0, 1] == "Least Potion") {
  global.iteminventory[0, 2] -= 1;
  global.life += 50;
}

GameGeisha

PS:
- Remember that array indices in GM start with 0, not 1.
- Simple coding questions like this belongs in Novice Q&A.
  • 0

#3 Spiritwolf

Spiritwolf

    GMC Member

  • GMC Member
  • 31 posts

Posted 04 March 2012 - 09:05 PM

PS:
- Remember that array indices in GM start with 0, not 1.
- Simple coding questions like this belongs in Novice Q&A.


Yes, I am aware of this. The [0] array is used elsewhere in the game for a different interface.

Shoot, are 2d arrays really that easy?
  • 0

#4 GameGeisha

GameGeisha

    GameGeisha

  • GMC Member
  • 2743 posts
  • Version:GM:Studio

Posted 04 March 2012 - 09:08 PM

Shoot, are 2d arrays really that easy?

If you look up "array" in the manual, you'll see from the example given that it really is this easy.

GameGeisha
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users