removing an item form hotspot

Started by Gribbler, Sat 12/08/2006 12:17:30

Previous topic - Next topic

Gribbler

Hi all,

Im new to scripting and started to work on a game. Here's the thing. There's a hotspot in my room. When character is interacting with it he receives an item (then he uses it somewhere else and gets rid of it). When he gets back to the hotspot he can get the item again (and again and again). Now my question is how to do it so he would be able to get just once? I've used command "interact with hotspot" --> "add inv item".


hedgefield

#1
Because you can go back to the hotspot after you lose the item, I don't think you can avoid coding on this one. I would say using a global integer would do the trick.

Open up the Global Script and find the section:
Code: ags
#sectionstart game_start  // DO NOT EDIT OR REMOVE THIS LINE
function game_start() {
  // called when the game starts, before the first room is loaded

 
And add this below it:
Code: ags
SetGlobalInt (0, 0);  //if you already have global ints just change the first variable to an unused number.


Now under 'interact with hotspot' change "add inv item" to 'run script'. Click 'edit script' and add:
Code: ags

If (GetGlobalInt(0) == 1) {  //Player already used the hotspot
  Display("I don't need another one of those.");
}
If (GetGlobalInt(0) == 0) {  //Player hasn't used the hotspot yet
 cEgo.AddInventory(iKey);  //Change iKey to the script name of the inventory item you want to the player to receive here.
 SetGlobalInt(0, 1);
}


Now when you use the hotspot it will give the player an item, and the next time display a message saying you already got that item.

Gribbler

Thanks largopredator! It worked just fine!


Khris

There's no need to "init" the GI in game_start. And GIs are 0 from the beginning.

If you have many of these situations, keeping a list of GIs can get annoying, so in this case I'd suggest a room variable.

Open the room's script ({}-Button in Room Editor -> Settings), then add
Code: ags
bool got_thingie=false;
at the top outside any function.

In the interaction's script, use this;
Code: ags
Ã,  if (got_thingie) {
Ã,  Ã,  Display("I don't need another one of those.");
Ã,  }
Ã,  else {
Ã,  Ã,  cEgo.AddInventory(iThingie);
Ã,  Ã,  got_thingie=true;
Ã,  }

Since you can use any name you want for the variable, the code becomes more readable, too.

SMF spam blocked by CleanTalk