Stopping an action after it happens once (SOLVED)

Started by Spookster, Wed 15/05/2013 21:34:11

Previous topic - Next topic

Spookster

Hello!

I'm trying to make something happen just once, or for it to stop after some other thing happenning, but no success so far...

The thing is: My character walks over a bridge (Puente). That makes an object appear (oAbigail) and a ghostly sound play (aFantasma). Then, what I would like to happen is that, even if the character is over the Bridge, the sound stopped, and when my character onteracts with the objetc, it dissapears and just appears in my inventory, not reappearing again even if the character walks over the bridge again.

My code:

function oAbigail_Interact()
{
aFantasma.Stop();
oAbigail.Visible = false;
player.AddInventory(iAbigail);
}

function hPuente_WalkOn()
{
aFantasma.Play();
oAbigail.Visible = true;
}

I have tried the "do once only" command, but I keep getting errors... and the first time I interact iwth the object, the sound stops and the item is in the inventory... but it reappears again when I walk over the bridge.

Help, please?

Thanks in advance!!


Khris

This should do it:

Code: ags
function hPuente_WalkOn()
{
  if (Game.DoOnceOnly("fantasma abigail")) {
    aFantasma.Play();
    oAbigail.Visible = true;
  }
}


What errors did you get? (Just curious, maybe the manual entry isn't clear enough on how to use the command.)

Also, if you want to check for the player walking somewhere, you shouldn't use hotspots (the "player stands on" event is included for legacy reasons, using them that way is obsolete). That's what regions are for. In addition to a continuously firing event, they also have events for stepping on and off of them.

Retro Wolf

#2
EDITED: Sorry....I'm an idiot lol

Spookster

That worked perfectly, thank you so much!!

I didn't copy the error messages, sorry, so I can't tell you exactly what they were, but I can see now that the problem was that I was not writing correctly what was between the brackets (ie ("fantasma abigail"), because I wrote it as if it was an object (ie ("oAbigail") ).

Oldschool_Wolf, what do you mean it will happen again? The object should be in my inventory permanently, unless I use it, so.. if I use your code, as long as I have the item in my inventory, the sound would play, right?


Khris

Not sure what exactly the problem was, but it doesn't matter what you use as text for Game.DoOnceOnly, as long as the String is unique. I could've used Game.DoOnceOnly("Yabba Dabba Doooh!"); in my code and it would still work just fine.
Of course, something like Game.DoOnceOnly(oAbigail); will not work; it needs to be text. This on the other hand should work fine: Game.DoOnceOnly(oAbigail.Name);

All the function does is keep a list of used texts. At the beginning, it is empty. When Game.DoOnceOnly is called, AGS looks up whether the text is already in the list. If it isn't, the function returns true, otherwise false. Then the text is added to the list.
The cool thing about this is that you can use the command in a situation where the player can do either one of two things but not both. Imagine two levers, and one will open the door, while the other will also do that but hurt the player first. If both levers use the same String in their Game.DoOnceOnly(), the player can't pull the other lever after having pulled the first one, all without any additional variables and checks.

frenzykitty

Game.DoOnceOnly is fantastic - learnt about it in one of the demo games for one of the VerbCoin templates. Such a useful function.

SMF spam blocked by CleanTalk