Room-specific Inventory-to-Character Interaction [SOLVED]

Started by JonCausith, Thu 07/08/2014 16:51:45

Previous topic - Next topic

JonCausith

EDIT: Many thanks to Crimson Wizard and Khris for helping me out. The problem has been solved.

Hello everyone! I am trying to create a mystery/vigilante sort of adventure game with AGS.

Unfortunately, the specific sort of scripting I'm trying to go for is not in the manual (or at least from what I can read.) What I would like to do is have the playable character USE their radio on themselves, and trigger a conversation that would only happen in that specific room.

Basically if they used the radio at a crime scene, they would ask for details to look for, and if they were at a criminal hideout, they would ask to record conversations among thugs, and so forth.

I tried using:
Code: ags

import function cEgo_UseInv();

function cEgo_UseInv()
{
 if (player.ActiveInventory == iRadio) {
      Display("The radio clicks on.");
}
}


But with no luck. Can someone help me?

Khris

The import line is completely unnecessary, why do you have that?

You need:
Code: ags
function cEgo_UseInv()
{
  if (player.ActiveInventory == iRadio) {
    if (player.Room == 3) {
      // room 3 code here
    }
    else if (player.Room == 4) {
      // room 4 code here
    }
  }
}


There's also another possibility, if you'd rather move the code into the respective room script:

Code: ags
function cEgo_UseInv()
{
  if (player.ActiveInventory == iRadio) {
    CallRoomScript(1);
  }
}


In the room script, add this:
Code: ags
function on_call(int p) {
  // radio
  if (p == 1) {
    // player used radio on themselves
  }
}


The 1 is arbitrary, you can use any other number instead. It's just to distinguish CallRoomScript() calls in case you have more than one.

Crimson Wizard

I would like to add that if you are going to use the second solution (with CallRoomScript) you may greatly benefit from introducing your own enumeration type for all possible kinds of events, and use it to distinguish events, instead of plain numbers.

For example, in GlobalScript.ash:
Code: ags

enum MyRoomEvent
{
  ePlayerUsedInventoryOnSelf, 
  eSomethingElseHappened
};


In global script:
Code: ags

function cEgo_UseInv()
{
  if (player.ActiveInventory == iRadio) {
    CallRoomScript(ePlayerUsedInventoryOnSelf); // notice, using event "name", instead of number
  }
}


In room script:
Code: ags

function on_call(MyRoomEvent event) { // yes, this actually works! because "enum" is technically identical to "int" :)
  if (event == ePlayerUsedInventoryOnSelf) {
    if (player.ActiveInventory == iRadio) {
      // player used radio on themselves
    }
  }
}

JonCausith

Thank you very much, Khris and Crimson! :-D You've both helped me out very well!

Quote from: Crimson Wizard on Thu 07/08/2014 18:25:10
In room script:
Code: ags

function on_call(MyRoomEvent event) { // yes, this actually works! because "enum" is technically identical to "int" :)
  if (event == ePlayerUsedInventoryOnSelf) {
    if (player.ActiveInventory == iRadio) {
      // player used radio on themselves
    }
  }
}


That is interesting! I really didn't know too much about the enum until now, so this will definitely help making things easier in searching for code in case the character calls the radio guy in a different room!

Either way, the second solution worked wonders in the test, so all I have to do is add in the dialog code and I'm all set! Thank you again!

Quote from: Khris on Thu 07/08/2014 18:00:47
The import line is completely unnecessary, why do you have that?

Oh, yes. I used that code because I was running out of options, and I misread one of the manual lessons by accident. Sometimes the brain does strange things when trying to get things to work, haha!

SMF spam blocked by CleanTalk