(Solved)How do I make events only happen once

Started by TGames, Sat 07/04/2018 03:13:10

Previous topic - Next topic

TGames

I know there is probably an obvious answer that i am missing, but can anyone tell me how I make it so that an event is only able to happen one time? In my game the character is able to interact with a vein of coal in a cliff face to pick up a piece of coal, but this happens whenever you interact with the vein, even if you already have the coal. This creates duplicates and the coal ends up staying in your inventory even after it was supposed to disappear. What do I need to do in order to make it so that you can only pick up the coal once, and maybe every other time you interact with it it just tells you that you don't need anymore?
Thankyou

NicolaGs

you can use a global variable that changes from isCoalTaken(false) to isCoalTaken(true).
And a If statement that checks this variable.

Also you can take a look in the manual at the function DoOnceOnly().
My first game : I Want Out!

Vincent

There are a hundred ways in which you can do this, essentially you need a condition.
You can archive a condition with a boolean for example, declare a global bool and call it PlayerHasPieceOfCoal with the default value to false. Then when you interact with the vein:
Code: ags

if (!PlayerHasPieceOfCoal) // Player doesn't have the piece of coal yet
{
  PlayerHasPieceOfCoal = true;
  // add inventory
  // do stuff
}
else // Player has the piece of coal
{
  // there's nothing else here
}


You could also set a condition by using an integer at all. You can check Character.InventoryQuantity[] or Game.DoOnceOnly.

Edit: NicolaGs was fast!

HandsFree

The variable that tells if the player has a piece of coal already exists in AGS. There's no need to create another one.
if player.HasInventory(iCoal) {}

Cassiebsg

@HandsFree... note that is different to check if the player has the item in inventory or has a variable to check if "it has been taken". Cause as soon as the player loses the item from inventory, he will be able to pick it up again if he wants it (even though he might not need anymore), while the "it has been taken" will still be true and not allow picking it up again (though, since it's a variable) you can then turn it true again if at one point you want the player to be able to pick it again. Using GameDoOnceOnly will do just that, you take it once and never again during that game you'll be able to use that again.

So... pick the one that is best to the situation you want. ;)
There are those who believe that life here began out there...

HandsFree

True. But op's problem had to do with getting multipe pieces of coal. If player loses it you may want to be able to get another piece. If not, then indeed GameDoOnceOnly is the best option.
In both cases creating a new variable is not needed.

Cassiebsg

I think you are missing one point, there are not 2 options, but 3. ;)
The case where you want the player to pick it up again if it's not in the inventory, the case where you only want it to be picked once no matter what and the case where you might want to let the player pick it again, if it becomes relevant. Let's say: the player picked it up & can't pick up a new one. Then uses it, and thus it's removed from inventory but he still doesn't need to pick up a new one (and thus he'll just says he doesn't need it). And then he's meet with a new puzzle where he realizes that he needs the item again, and goes back to pick it again (and it's now allowed). Though most games don't use this formula, but it's an option. And in this 3rd option you would need a variable. ;)
There are those who believe that life here began out there...

TGames

Thank you all for your helpful replies, I got it fixed.

SMF spam blocked by CleanTalk