Problem with items - unhandled events

Started by Chomba, Wed 17/03/2021 02:21:44

Previous topic - Next topic

Chomba

Hello again!

I am having a problem with the handling of some commands referring to Items in my game.

The problem happens when I want to call the unhandled_event on an item with several interactions that are already defined (with other inventory items).

Maybe it will be better understood when you see the code.
My intention is that if I have not defined any reaction with another inventory item, apart from those specified, I use the unhandled_event.

Code: ags

function iMachete_UseInv()
{
if (player.ActiveInventory == iPistola) {
player.Say("Mejor no, podría arruinar la pistola");
}
if (player.ActiveInventory == iCraneo) {
player.Say("No quiero cortar la flecha");
}
if (player.ActiveInventory == iLiana) {
player.Say("No me interesa atar eso");
}
if (player.ActiveInventory == iFlecha) {
player.Say("No quiero recortar la flecha");
}
if (player.ActiveInventory == iIdolo) {
player.Say("La doctora lo prefiere en una sola pieza");
}
if (player.ActiveInventory == iPistolaDeArponCargada) {
player.Say("Mejor no, podría arruinar la pistola");
}
if (player.ActiveInventory == iLanzadorDeLianas) {
player.Say("Mejor no, podría arruinar la pistola");
}
if (player.ActiveInventory == iFlechaAtada) {
player.Say("No quiero acortar ni la flecha, ni la soga");
}
if (player.ActiveInventory == iPiedra) {
player.Say("Prefiero mantenerlo en buenas condiciones");
}
else {unhandled_event(2, 3);
}
}


What happens is that:

* If the object I try to use (this time with iMachete) had no dialog established, I have no problem.

* If the object I try to use with iMachete already had some dialogue established, the character says that dialogue and then also some of the ones assigned to the unhandled_event.
EXCEPT WITH iPiedra, with which it works as it should and only says the specified dialog.

I know the solution should be to find some way to encapsulate all the "if" above and apply the "else" as an alternative to any of them. And that the problem is that the program interpretation (or rather, that what I am telling the program) is that the "else" should be applied to the last "if" only ... but I can' t figure out how to do it and could not find something I can understand in the manual or in other topics.

Crimson Wizard

#1
This is a very simple mistake. A correct list of conditions with only one possible choice is constructed using "if/else if/else" form:

Code: ags

if (condition 1)
else if (condition 2)
else if (condition 3)
...
else


This binds all conditions in one "switch", where only one condition can be passed at a time.

In your code, however, you begin every condition with just "if", which marks a start of a new switch. So, regardless of what was decided in the first condition, engine will continue testing next "if", then third "if", and so on, until it reaches last switch with "else", where it will choose "else" unless ActiveInventory == iPiedra.

Chomba

Thanks Crimson Wizard!

I had tried that solution, but I was typing the command backwards.
instead of putting:

"else if"

I put:

"if else"

and I was getting an error! I wouldn't have noticed if it wasn't for you.

morganw

It might be easier to write it as a switch statement:
https://adventuregamestudio.github.io/ags-manual/ScriptKeywords.html#switch-case-statements
...it would also allow you to remove duplication of dialogue within the script because you can specify more than one case for the same result.

Untested example:
Code: ags
function iMachete_UseInv()
{
  switch(player.ActiveInventory)
  {
    case iPistola:
    case iPistolaDeArponCargada:
    case iLanzadorDeLianas:
      player.Say("Mejor no, podría arruinar la pistola");
      break;
    case iCraneo:
      player.Say("No quiero cortar la flecha");
      break;
    case iLiana:
      player.Say("No me interesa atar eso");
      break;
    case iFlecha:
      player.Say("No quiero recortar la flecha");
      break;
    case iIdolo:
      player.Say("La doctora lo prefiere en una sola pieza");
      break;
    case iFlechaAtada:
      player.Say("No quiero acortar ni la flecha, ni la soga");
      break;
    case iPiedra:
      player.Say("Prefiero mantenerlo en buenas condiciones");
      break;
    default:
      // do something here if nothing matched
      break;
  }
}

Chomba

QuoteIt might be easier to write it as a switch statement:
https://adventuregamestudio.github.io/ags-manual/ScriptKeywords.html#switch-case-statements
...it would also allow you to remove duplication of dialogue within the script because you can specify more than one case for the same result.

Intresting... that would be useful to avoid so many monotonous answers or for special cases... I will try it in the future! Thank you!

SMF spam blocked by CleanTalk