(SOLVED) I'm sorry... I need help again with if/else

Started by BlueAngel, Sat 05/03/2011 13:18:19

Previous topic - Next topic

BlueAngel

Sorry to bother you all again but I cant get this to work.
It works fine for the two thing but the I want him to say that hes got enough bambo but it never gets to that part?  ???

Code: ags

function hBambo_AnyClick()
{
  //LOOK AT
  if(UsedAction(eGA_LookAt)) {
    player.Say("Some bamboo is growing over there");  
  }
  
  // Push
  else if(UsedAction(eGA_Push)) {
    player.Say("it just bends");
  }
  // Pull
  else if(UsedAction(eGA_Pull)) {
    //player.Say("Oh hey");
    if(player.InventoryQuantity[iBamboo.ID]==0&&player.InventoryQuantity[iSpindle.ID]==0)
    {
      player.AddInventory(iBamboo);
      }
      else if(player.InventoryQuantity[iBamboo.ID]==1||player.InventoryQuantity[iSpindle.ID]==1)
      {
        player.AddInventory(iBamboo2);
      }
      else if (player.InventoryQuantity[iBamboo.ID]==1&&player.InventoryQuantity[iBamboo2.ID]==1)
      {
        player.Say("I got enough right now");
      }
  }  
  // PICKUP
  else if(UsedAction(eGA_PickUp)) {
    player.Say("Its not laying around on the ground");  
  }
  
  // don't forget this
  else Unhandled();
}


mode7

#1
In the previous  two conditions your checking for iSpindle, shouldnt you check for iBamboo2 there? You also have an 'or' operator on the condition before the player says, that he does have enough bamboo, which will aleays stay true if the player has ispindle or ibamboo1. So the last condtion never gets triggered here.

Code: ags

if(player.InventoryQuantity[iBamboo.ID]==0&&player.InventoryQuantity[iSpindle.ID]==0)
    {
      player.AddInventory(iBamboo);
      }
      else if(player.InventoryQuantity[iBamboo.ID]==1||player.InventoryQuantity[iSpindle.ID]==1)//as long as the player has ispindle this will be true
      {
        player.AddInventory(iBamboo2); //maybe you should loose ispindle here for this to return false
      }
      else if (player.InventoryQuantity[iBamboo.ID]==1&&player.InventoryQuantity[iBamboo2.ID]==1)
      {
        player.Say("I got enough right now");
      }


BlueAngel

I think I wasn’t clear enough. :(
The player can pick up two things: bamboo1 and bamboo2.
If he got spindle he doesn’t need bamboo1, so one and two works.

So If I understand you correctly I need to make last sentence false for it to work.

What I really want it to mean is: if player got bambo1 (or spindle) AND bambo2 he don’t need any more.

Sorry for my poor English, hope you understand anyway.

Khris

I think you want:
Code: ags
  if ((player.HasInventory(iBamboo) || player.HasInventory(iSpindle)) && player.HasInventory(iBamboo2))


Note that this will only prevent the player from getting the bamboo again if they never loose it or never get back to the room it is in.

If you want to keep the player from getting the bamboo again permanently, you have to use room variables.

Here's what I would do:

Code: ags
bool got_bamboo1, got_bamboo2;

function hBambo_AnyClick() {

  ...

  // Pull
  else if (UsedAction(eGA_Pull)) {

    if (!got_bamboo1) {
      player.AddInventory(iBamboo);
      got_bamboo1 = true;
    }
    else if (!got_bamboo2) {
      player.AddInventory(iBamboo2);
      got_bamboo2 = true;
    }
    else player.Say("I got enough right now.");
  }

  ...
}

BlueAngel

 :D Worked like a charm!

Just for learning experience can you tell me why the last line wasnt triggered in my script?

monkey0506

#5
Well for that you should consider the truth table of what you were doing:
A  |  B  |  A && B
------------------
T  |  T  |  T
T  |  F  |  F
F  |  T  |  F
F  |  F  |  F

------------------

A  |  B  |  A || B
------------------
T  |  T  |  T
T  |  F  |  T
F  |  T  |  T
F  |  F  |  F


So then you were doing:

Code: ags
if (A && B) {
  // A and B are both true
}
else if (C || D) {
  // by here A or B, or both, are false
  // C or D, or both, are true
}
else if (C && E) {
  // here both C *and* D *must* be false, otherwise we'd never enter this block
  // since C must be false, C && E will never be true, so we'd still never enter this block
}


So basically your logic was flawed and the final condition could never be true.

BlueAngel

Thanks Monkey for looking at this after it been closed.

I must first start with confessing that I have never seen or worked with a truth table, had to look it up.
But if I understand this (probably not ;) ) correctly :

(if) A and B are true in row 1
Then A and B are false in row 2
So since C is true in row 2 then it can’t be true in row 3
And since it can’t be true in row 3, a sentence that states that “(if) C and E are both true do X “ can never  work.

I’m still not sure why C can’t be true in both row 2 and 3 but that I put down to bad mathematic knowledge on my part.
So I just have to accept that as a fact right now. :)

Khris

Disregard A and B for a moment, lets assume at least one of them is false (otherwise row 1 is executed and that's it).

C is either true or not, and each time for both row 2 and 3; the problem here is the if - else if - else if.

If C is true, row 2 is executed. This in turn means that regardless of row 3's condition, it is not executed.
If C is false and D is true, again, row 2 is executed.
If both are false, which is a necessary prerequisite to row 3 being executed, C && E must also be false, regardless of E.

Due to the if - else if - else if structure, the condition for row 3 is essentially:

if ( (A is false or B is false)  and  (C is false and D is false)  and  (C is true and E is true) )

Leaving A and B out of the picture we can shorten this to

if ( C is false and D is false and C is true and E is true )

Obviously, this can never be the case.

BlueAngel

Thanks Kris! :)
I really think I got it now.
Since C are true in row 2, row 3 vill not run. And for row 3 to run C must be true. But it will not for if C is true, run row2!
So of course row 3 will never run.  ::)

Thanks so much for taking the time to show me this!

SMF spam blocked by CleanTalk