Author Topic: Different ways to use an item only once? [SOLVED]  (Read 248 times)

Stranga

  • I love GameBoy Games!
    • I can help with making music
    •  
    • I can help with story design
    •  
Hello everyone, I want to use an item on a hotspot only once to get another item without losing the initial item. So far I've been using
Code: Adventure Game Studio
  1. if (Game.DoOnceOnly("GainItem"))

But I'm not sure if this is the best method/way to do this. Anyone have any ideas or experience on how to do this or is this way fine?

P.S wasn't sure If this sort of question should be posted here but any moderators are more than welcome to move it :)
« Last Edit: 10 Feb 2018, 07:21 by Stranga »
Yo.

Danvzare

  • The Man with No Name
    • I can help with AGS tutoring
    •  
    • I can help with play testing
    •  
    • I can help with proof reading
    •  
    • I can help with story design
    •  
    • I can help with voice acting
    •  
Re: Different ways to use an item only once?
« Reply #1 on: 09 Feb 2018, 12:29 »
Create a variable in the room script and set it to false, then check if that variable is false when you use the item, and if it is, set the variable to true and gain the other item. That way if you use the item again, the check will fail and you'll get a different response, such as "I've got enough wood, I don't need to chop off any more."

I think something like this code should work:
Code: Adventure Game Studio
  1. bool usedAxe = false;
  2.  
  3. if (!usedAxe)
  4. {
  5.     player.AddInventory(iWood);
  6.     usedAxe = true;
  7. }
  8. else
  9. {
  10.     player.Say("I've got enough wood, I don't need to chop off any more.");
  11. }
  12.  
« Last Edit: 09 Feb 2018, 12:32 by Danvzare »

dayowlron

  • been programming computers since 1981
    • I can help with AGS tutoring
    •  
    • I can help with proof reading
    •  
    • I can help with scripting
    •  
    • I can help with story design
    •  
Re: Different ways to use an item only once?
« Reply #2 on: 09 Feb 2018, 13:05 »
the whole purpose for the Game.DoOnceOnly is for this purpose. Even though Danvzare's method will work, the way you had it works also and you don't need to set up the additional variable.
Pro is the opposite of Con
This fact can clearly be seen,
If progress means to move forward
Then what does congress mean?  --Nipsey Russell

Stranga

  • I love GameBoy Games!
    • I can help with making music
    •  
    • I can help with story design
    •  
Re: Different ways to use an item only once?
« Reply #3 on: 09 Feb 2018, 13:28 »
I thought it was the best method at the time didn't think it would be the actual way to do it. The bool method would be good if I weren't using the RestartGame() function. One other thing that I wanted to ask about is which way is better or they both do the same thing:

Code: Adventure Game Studio
  1. if (player.ActiveInventory==iAxe && Game.DoOnceOnly("CutTree"))
  2.  
  3. //or
  4.  
  5. if (Game.DoOnceOnly("CutTree")){
  6.     if (player.ActiveInventory==iAxe){
  7.  
  8.     }
  9. }
  10.  

I have used both ways and they both "seem" fine to me but I am unsure if they would conflict with each other some how with the first example.
Yo.

Crimson Wizard

  • AGS Project Tracker Admins
    • Best Innovation Award Winner 2013, for spearheading the AGS 3.3.0 project
    •  
    • Lifetime Achievement Award Winner
    •  
    • Crimson Wizard worked on a game that was nominated for an AGS Award!
      Crimson Wizard worked on a game that won an AGS Award!
Re: Different ways to use an item only once?
« Reply #4 on: 09 Feb 2018, 13:54 »
I thought it was the best method at the time didn't think it would be the actual way to do it. The bool method would be good if I weren't using the RestartGame() function.

Both boolean variables and DoOnceOnly are reset with the RestartGame.
Actually, what RestartGame does is simply loading a save that was made when game is started, or you call SetRestartPoint. So, this is like loading a savegame.


One other thing that I wanted to ask about is which way is better or they both do the same thing:

Code: Adventure Game Studio
  1. if (player.ActiveInventory==iAxe && Game.DoOnceOnly("CutTree"))
  2.  
  3. //or
  4.  
  5. if (Game.DoOnceOnly("CutTree")){
  6.     if (player.ActiveInventory==iAxe){
  7.  
  8.     }
  9. }
  10.  

I have used both ways and they both "seem" fine to me but I am unsure if they would conflict with each other some how with the first example.


There is a difference, and that difference may be important or not important depending on situation.

First of all, the first example will run the block of statements if both conditions are true only.
The second example has TWO blocks of statements, one under "if (Game.DoOnceOnly("CutTree"))" and the second under "if (player.ActiveInventory==iAxe)", which is a "sub-block" of the first one.
If you only have commands under second condition, then there is no difference, but in theory you could also add commands under Game.DoOnceOnly like this:
Code: Adventure Game Studio
  1. if (Game.DoOnceOnly("CutTree")){
  2.     /// some commands here
  3.     if (player.ActiveInventory==iAxe){
  4.  
  5.     }
  6.     /// more commands here
  7. }
  8.  
In such hypothetical case these extra commands would be run if Game.DoOnceOnly returns true, but they won't depend on the second condition.


If you only suppose to have game react to both conditions at once always, I'd recommend to use the first variant, because it makes intentions more clear.
But then again, such things are rather important when you work in team and want other people understand your code. If you are working alone it's all up to you.
« Last Edit: 09 Feb 2018, 14:08 by Crimson Wizard »

Khris

    • Lifetime Achievement Award Winner
    •  
    • I can help with play testing
    •  
    • I can help with scripting
    •  
    • I can help with translating
    •  
    • Khris worked on a game that was nominated for an AGS Award!
Re: Different ways to use an item only once?
« Reply #5 on: 09 Feb 2018, 16:02 »
I don't think that's correct, there is indeed a difference.

Code: Adventure Game Studio
  1.   if (player.ActiveInventory==iAxe && Game.DoOnceOnly("CutTree"))

Expressions are evaluated from left to right (unless set differently in General Settings -> Backwards Compatibility). Which means if the player has used some other InventoryItem, the left test will fail. At this point AGS doesn't bother evaluating the right side, since "false && x" is always "false" anyway.
The downside of this method is that a subsequent else will catch both "other item was used" and "axe was used a second time", which is usually undesirable.

Here however:
Code: Adventure Game Studio
  1. if (Game.DoOnceOnly("CutTree")){
  2.     if (player.ActiveInventory==iAxe){
  3.       //...
  4.     }
  5. }

AGS always runs Game.DoOnceOnly() first, which means the entire outer block is disabled after the first run, regardless of the item that was used.
Which means:
1. Use Spoon on Tree -> (wrong)
2. Use Axe on Tree -> (wrong)

My suggestion would be to always handle the item first (after potentially approaching the hotspot and the like, of course):
Code: Adventure Game Studio
  1.   if (player.ActiveInventory == iAxe) {
  2.     if (Game.DoOnceOnly("CutTree")) { ... }
  3.     else ...
  4.   }
  5.   else Unhandled();

In general, always nest from generic to specific. Cutting the tree is part of the axe interaction, and should go inside it.

Stranga

  • I love GameBoy Games!
    • I can help with making music
    •  
    • I can help with story design
    •  
Re: Different ways to use an item only once?
« Reply #6 on: 10 Feb 2018, 07:20 »
Thank you everyone and especially CW and Khris you both have gave great answers but also solved a few problems that I would of had to post here.
Yo.

Danvzare

  • The Man with No Name
    • I can help with AGS tutoring
    •  
    • I can help with play testing
    •  
    • I can help with proof reading
    •  
    • I can help with story design
    •  
    • I can help with voice acting
    •  
Re: Different ways to use an item only once? [SOLVED]
« Reply #7 on: 12 Feb 2018, 12:08 »
the whole purpose for the Game.DoOnceOnly is for this purpose. Even though Danvzare's method will work, the way you had it works also and you don't need to set up the additional variable.
Wow, can you believe that I've been using AGS for years, and I never knew that Game.DoOnceOnly worked like that.
I've learnt something new. :-D