[SOLVED] Need help combining items using bool variable

Started by delacey89, Fri 09/06/2023 09:15:26

Previous topic - Next topic

delacey89

Hello everyone. I'm new here, and to coding in general, though I've been toying with the idea of making a game in AGS since about 2007 or so. Forgive me if my question seems obvious.

Okay, in my scenario the character (cDummy) is supposed to pick up a bit of bamboo and an empty Jetpack. Nearby he finds a Crater with radioactive waste inside. The puzzle involves combining the inventory item iBamboo with iJetpackEmpty to make the new combined item iBambooJetpack, which can then be used on the Crater to siphon the waste to use as jetpack fuel. Once this is done the inventory item iBambooJetpack will be replaced with iJetpackFull.
The catch is that I don't want the player to be able to combine any of the items until after they have tried to use either the bamboo (iBamboo) or the empty Jetpack (iJetpackEmpty) on the Crater (hCarter1).

At the moment, I have this in my global script:

Code: ags
  // Declare global variables

bool iBambooOrJetpackUsedOnCrater = false;

  //Rest of Script
function iBamboo_UseInv()
{
  if (cDummy.ActiveInventory == iJetpackEmpty)
  {
    if (iBambooOrJetpackUsedOnCrater)
    {
      cDummy.Say("Now You're thinking with your noodle!");
      cDummy.LoseInventory(iBamboo);
      cDummy.LoseInventory(iJetpackEmpty);
      cDummy.AddInventory(iBambooJetpack);
      cDummy.Say("The bamboo will make the perfect siphon.");
    }
  }
  else
  {
    cDummy.Say("Why would I want to do that?");
  }
}

function iJetpackEmpty_UseInv()
{
  if (cDummy.ActiveInventory == iBamboo)
  {
    if (iBambooOrJetpackUsedOnCrater)
    {
      cDummy.Say("Now You're thinking with your noodle!");
      cDummy.LoseInventory(iBamboo);
      cDummy.LoseInventory(iJetpackEmpty);
      cDummy.AddInventory(iBambooJetpack);
      cDummy.Say("The bamboo will make the perfect siphon.");
    }
    else
    {
      cDummy.Say("Why would I want to do that?");
    }
  }
}

And this in my room script:

Code: ags
  // Declare global variables

bool iStonesUsedOnCrater = false;

bool iBambooOrJetpackUsedOnCrater = false;


function hCrater1_UseInv()
{
  if (cDummy.ActiveInventory == iStones)
  {
    if (Game.DoOnceOnly("StonesInCrater") == true)
    {
      cDummy.Walk(hCrater1.WalkToX, hCrater1.WalkToY, eBlock, eWalkableAreas);
      iStonesUsedOnCrater = true;
      cDummy.LoseInventory(iStones);
      cDummy.Say("There goes my back.");
      cDummy.Say("Whoa! Some kind of fluorescent green liquid splashed up at me!");
      GiveScore(10);
    }
  }
  else if (cDummy.ActiveInventory == iBamboo)
  {
    iBambooOrJetpackUsedOnCrater = true;
    cDummy.Say("It's glowing! It's probably radioactive.");
    cDummy.Say("If you want me to suck that up into my mouth you'd better have a damn good reason.");
  }
  else if (cDummy.ActiveInventory == iJetpackEmpty)
  {
    iBambooOrJetpackUsedOnCrater = true;
    cDummy.Say("Not a bad idea. Whatever that green liquid is, it sure looks radioactive; there's a good chance it'll work as fuel.");
    cDummy.Say("...but it's too far to reach. I'll need to fetch it with something else.");
  }
}

The game launches and everything works up to a point. The inventory items can be used on the crater and cDummy will say what he is supposed to, but if I try to combine the bamboo with the Jetpack AFTER using the items on the crater, I simply get the response "Why would I want to do that?". My assumption is the problem has something to do with "iBambooOrJetpackUsedOnCrater" and the fact it's written in both the global and the room script, but that's just my gut feeling. I really don't know. I thought maybe if I put all the code surrounding the crater in the global script?

Any help is greatly appreciated.



EDIT---
Sorry everyone. Figured it out as soon as I posted.
I got it working by removing "bool iBambooOrJetpackUsedOnCrater == false;" from room script, before putting this into the global script:

Code: ags
bool iBambooOrJetpackUsedOnCrater;
export iBambooOrJetpackUsedOnCrater;

...and finally I put this in the Global HeaderScript:

Code: ags
import bool iBambooOrJetpackUsedOnCrater;

Now it works flawlessly.


PS I've never really used a forum before and don't know the proper etiquette. Guess I'll leave this here for others with a similar problem to see?

Snarky

Welcome, and good luck with your game! Glad you figured it out so soon yourself. That's often the way when you post a question.

Yeah, you did the right thing: post the solution you found and leave the question up in case others have a similar problem, or in case someone else has anything to add. The only other thing is to also edit the title to mark the problem as solved, which I have done for you.

Khris

For reference:

Your initial code was using two identically named but separate variables, changing the room one didn't affect the one local to the global script.
As a rule in general, a variable is only ever declared once.

Also note that depending on your circumstances it might be preferable to use the Global Variables node in the project tree.

delacey89

Every time I've searched an old thread with a problem, you guys have already supplied the solution. Thanks a lot, both of you. Glad to know there are helpful experts out there.

I did wonder if one variable was conflicting with the other. The problem I was stuck on was how to make the variable work across both scripts while declaring it only once, and it was the import export bool feature that saved me in the end.

I really need to open my eyes. Completely missed the Global Variables node. I'm going to dig into that now and see if I can tidy things up a bit.

Thanks again.

SMF spam blocked by CleanTalk