ActiveInventory and Global Variables Issue [SOLVED]

Started by Myinah, Wed 12/06/2013 20:24:45

Previous topic - Next topic

Myinah

Hi Guys

I have a problem when using active inventory items with a global variable counter. I apologise if there is a really simple answer for this, but searching the manual didn't help and I couldn't find an answer in the forums. I have tried various code combinations but nothing works. If there is a thread or page in the manual answering this please feel free to point me in the right direction. I have created a successful global variable before, but there was no inventory involved.

Basically we have a puzzle that requires us to use four ingredients to create a spell. The cauldron is an object in the room and we need to add 4 inventory items (in no specific order) to the cauldron. I thought using a counter would be the best way to do this, but it doesn't like the additional "ifs", "()", and "{}" for the code. I have tried removing one or all of them in various combinations but nothing works. Am I barking up the totally wrong tree here?

Code: AGS
function oCauldron1_UseInv()
{
  CauldronCounter = 0;


if (CauldronCounter < 4) {
      (cWoo.ActiveInventory == iIngredient1) {
      cWoo.Walk(787, 623, eBlock, eWalkableAreas);
      cWoo.Say("In it goes! Black as night!");
      cWoo.LoseInventory(iIngredient1); }
      CauldronCounter++; 
}
   else {   
     cWoo.Walk(787, 623, eBlock, eWalkableAreas);
      cWoo.Say("In it goes! Black as night!");
      cWoo.LoseInventory(iIndredient1);
      cWoo.Say("That's it! That's all the ingredients!");
      cWoo.Say("Now to begin the spell!");}
   }

      
if (CauldronCounter < 4) { 
     (cWoo.ActiveInventory == iIngredient2) {
      cWoo.Walk(787, 623, eBlock, eWalkableAreas);
      cWoo.Say("Fresh, new, into the stew!");
      cWoo.LoseInventory(iIngredient2); }
      CauldronCounter++1;
}

else {
  (cWoo.ActiveInventory == iIngredient2) {
      cWoo.Walk(787, 623, eBlock, eWalkableAreas);
      cWoo.Say("Fresh, new, into the stew!");
      cWoo.LoseInventory(iIngredient2); 
      cWoo.Say("That's it! That's all the ingredients!");
      cWoo.Say("Now to begin the spell!");}
  }
  
 
if (CauldronCounter < 4) {
     (cWoo.ActiveInventory == iIngredient3) {
      cWoo.Walk(787, 623, eBlock, eWalkableAreas);
      cWoo.Say("This perfectly aged item is going to make our spell awesome!");
      cWoo.LoseInventory(iIngredient3); }
      CauldronCounter++1;
}

else {
     (cWoo.ActiveInventory == iIngredient3) {
      cWoo.Walk(787, 623, eBlock, eWalkableAreas);
      cWoo.Say("This perfectly aged item will make our spell awesome!");
      cWoo.LoseInventory(iIngredient3);
      cWoo.Say("That's it! That's all the ingredients!");
      cWoo.Say("Now to begin the spell!");}
}

     
if (CauldronCounter < 4) {
     (cWoo.ActiveInventory == iIngredient4) {
      cWoo.Walk(787, 623, eBlock, eWalkableAreas);
      cWoo.Say("Can't get enough of this stuff!");
      cWoo.Say("...");
      cWoo.Say("Oops.");
      cWoo.LoseInventory(iIngredient4);
      cWoo.AddInventory(iBrokenThing); }
      CauldronCounter++1;
}
  
else {
     (cWoo.ActiveInventory == iIngredient4) {
      cWoo.Walk(787, 623, eBlock, eWalkableAreas);
      cWoo.Say("Can't get enough of this stuff!");
      cWoo.Say("...");
      cWoo.Say("Oops.");
      cWoo.LoseInventory(iIngredient4);
      cWoo.AddInventory(iBrokenThing); 
      cWoo.Say("That's it! That's all the ingredients!");
      cWoo.Say("Now to begin the spell!");}


I'm really not sure what I'm doing wrong. I feel like I must have missed something really basic because multiple items in a recipe isn't exactly testing the boundaries of adventure puzzling!

If anyone can offer some advice I would be very grateful.

Thanks again.


san.daniele

Line 3. ;)

Every time you use an item on the cauldron the counter is set to 0.

Slasher

#2
Brief solution example:

I would create a variable Int (in the global Pane) and  name it ingredients and set it to 0.

When you use an appropriate active inventory ingredient (any of 4) on the cauldron simply add 1 to the variable and lose that inventory item from your inventory.

Repeat for each inventory item used on object.

When you have used all 4 inventory items on the cauldron then ingredients int will equal 4.

In Rep Exec check if 'ingredients' is equal to 4 and if it does then run events.

You could of course have the player say something each time an ingredient item is used or do other things.

Any probs. just ask for more.



Crimson Wizard

#3
Your lines 7, 23, 31 etc make no sense at all.

Code: ags

if (CauldronCounter < 4) {
      (cWoo.ActiveInventory == iIngredient1) {


E: Actually, you may just be missing another "if" there, in which case ignore the following.

//-------------------------------------------------------
I can make a guess that you wanted to combine two conditions here?
This is how it is done:
Code: ags

if (CauldronCounter < 4 && cWoo.ActiveInventory == iIngredient1) {

&& sign means AND operator, that is: both conditions must be correct to make this part execute.

If you need "any of the" conditions to trigger your code, use OR operator:
Code: ags

if (CauldronCounter < 4 || cWoo.ActiveInventory == iIngredient1) {



More on this:
http://www.adventuregamestudio.co.uk/wiki/Scripting_tutorial_part_2#Multiple_conditions

san.daniele

Quote from: Crimson Wizard on Wed 12/06/2013 21:06:36
Your lines 7, 23, 31 etc make no sense at all.

I'm actually happy to read that. I was already cursing my lack of knowledge and wondered if in nested functions it may be possible to skip all following "if's" :D

Khris

It's either variable = variable + 1; or variable += 1; or variable++;
The former two can be used to add any value, the last will increment by one.

You have also some misplaced brackets and lots of duplicate code:

Code: ags
function oCauldron1_UseInv()
{

  if (CauldronCounter == 4) {
    cWoo.Say("All ingredients are in there already.");
    return;
  }

  if (cWoo.ActiveInventory == iIngredient1) {
    cWoo.Walk(787, 623, eBlock, eWalkableAreas);
    cWoo.Say("In it goes! Black as night!");
    cWoo.LoseInventory(iIngredient1);
    CauldronCounter++;
  }
  else if (cWoo.ActiveInventory == iIngredient2) {
    cWoo.Walk(787, 623, eBlock, eWalkableAreas);
    cWoo.Say("Fresh, new, into the stew!");
    cWoo.LoseInventory(iIngredient2); 
    CauldronCounter++;
  }
  else if (cWoo.ActiveInventory == iIngredient3) {
    cWoo.Walk(787, 623, eBlock, eWalkableAreas);
    cWoo.Say("This perfectly aged item is going to make our spell awesome!");
    cWoo.LoseInventory(iIngredient3);
    CauldronCounter++;
  }
  else if (cWoo.ActiveInventory == iIngredient4) {
    cWoo.Walk(787, 623, eBlock, eWalkableAreas);
    cWoo.Say("Can't get enough of this stuff!");
    cWoo.Say("...");
    cWoo.Say("Oops.");
    cWoo.LoseInventory(iIngredient4);
    cWoo.AddInventory(iBrokenThing);
    CauldronCounter++;
  }
  else cWoo.Say("That's not supposed to go in there.");   // other inv item

  if (CauldronCounter == 4) {
    cWoo.Say("That's it! That's all the ingredients!");
    cWoo.Say("Now to begin the spell!");
  }
}

Stupot

I wonder if making the cauldron into a character, rather than an object, would make this puzzle any easier to implement?
Then the player just 'gives' each item to it, and the system already knows which items it has without any awkward variables.  Then once cCauldron has all of the items in its "inventory" you can start the process of beginning the spell.

This doesn't solve your actual question, but just thought I'd throw it out there.
MAGGIES 2024
Voting is over  |  Play the games

Myinah

Thank you all so much for your input. I really appreciate the responses. It was fixable by simply adding the && to combine the two conditions. I had tried to do it with the additional ifs and such, but none of it worked. The rest of the code was actually okay. So thank you Crimson for helping out with that. I won't forget that in the future! (Touch wood...)

Also san.daniele you were right about the counter too. We took out that bit of code. Thanks for that!

There was one thing my coding partner soxbrooker wanted to ask, and that was is there a reason "else" doesn't work in the code. We were following one of the video tutorials regarding these variables and the command else worked for them, but it wouldn't accept it for us. We have just used "if" for every alternative. Is else a redundant term now?

edit: spelling

Khris

As long as your syntax is messed up, AGS will complain about else. It is not redundant at all.
Code: ags
// example with single command:

  if (a < 0) Display("a is negative");
  else if (a == 0) Display("a equals zero");
  else Display("a is positive");

// code blocks

  if (walk_there_first) {
    player.Walk(123, 45, eBlock);
    player.FaceLocation(123, 44, eBlock);
  }
  else {
    player.FaceLocation(mouse.x, mouse.y);
  }


Btw, did you look at what I posted? No comment at all?

san.daniele

your question about the "else" is a little vague (actually very vague).
if you referr to your initial code posted here: the "else"'s there will never come to play as they're taking the "if (CauldronCounter < 4)" as reference.

if (CauldronCounter < 4) { code here }
else { code here }

i suggest you go through Khris' code line by line and try to make sense of everything that's happening and how it is connected.

edit: Khris beat me to it.
I was referring to the code he posted earlier.

Myinah

Ok firstly, sorry if I seem to have missed your code Khris! My coding partner and I actually intended to respond to Crimson and san.daniele before you posted. We have just been really busy trying to work on our game so its ready for the MAGS deadline we kept forgetting to respond. I didn't want to seem ungrateful and rude so I quickly posted the thank you for what had specifically helped us. The stuff they said helped us make the puzzle work in our own higgledy piggledy way.

That said, looking over your code it does seem much better (cutting out lots of our excess stuff) and my coding partner will take a closer look later to see how we can implement it. I am not as great at understanding code right off the bat as I am completely new to all this. Really we've taken on a huge task to make our first ever game in a month! I'm doing my best, but clearly I need to be more thoughtful in my responses. Sorry if I offended you!

sans.daniele, the question was kind of vague, but I couldn't think of a better way to describe it. Sorry! My coding partner will make a post later on and probably do a far better job!  :-D

Khris

No worries, I wasn't offended, just curious. Learning the shorter ways of doing things will save much time, especially when it comes to debugging, but for a first - and MAGS - game, I completely understand the "I just want it to work" approach :)

monkey0506

Quote from: Khris on Fri 14/06/2013 13:47:45Learning the shorter ways of doing things will save much time

Additionally, while you probably would never see a difference in actual performance rising out of it, it is logically more efficient to avoid having to process the same condition multiple times. I cannot tell you the number of times I've see new programmers do something like:

Code: ags
if (myint < 4) { ...blah... }
else if (myint >= 4) { ... }


If the term myint is not less than 4 here, then by the time you reach the else-statement you already know that the second condition (in this example) is true. It is completely redundant to do this. If you want to make a note for yourself, that's what comments are for. Don't force the engine into doing extra work just as a form of self-documentation. In the example case here, the correct syntax would be similar to:

Code: ags
if (myint < 4) { ... }
else { ... } // myint >= 4


The only time you should ever use else if is if you are testing a different condition (not just the inverse condition).

I'm not strictly saying that you were doing this, but I felt like it needed to be said (especially since there was some confusion about how else should be used).

Crimson Wizard

Quote from: monkey_05_06 on Fri 14/06/2013 13:58:04
Additionally, while you probably would never see a difference in actual performance rising out of it
This really depends on what code is put into condition. Testing a >= 4 few extra times is no big deal, but if person gets this habit, he eventually starts to call same function with same set of parameters dozens of times when checking various conditions, and that eventually gets you into trouble. :)

Dropped Monocle Games

Khris I read your coding and think I understand where I was going wrong and was misusing the 'Else' its working amazing now with half the coding it was before! Thank you and everyone else for your help! hope you will all get to try the game when its finished :)

SMF spam blocked by CleanTalk