Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: JonCausith on Thu 07/08/2014 16:51:45

Title: Room-specific Inventory-to-Character Interaction [SOLVED]
Post by: JonCausith on Thu 07/08/2014 16:51:45
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) Select

import function cEgo_UseInv();

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


But with no luck. Can someone help me?
Title: Re: Room-specific Inventory-to-Character Interaction
Post by: Khris on Thu 07/08/2014 18:00:47
The import line is completely unnecessary, why do you have that?

You need:
Code (ags) Select
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) Select
function cEgo_UseInv()
{
  if (player.ActiveInventory == iRadio) {
    CallRoomScript(1);
  }
}


In the room script, add this:
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.
Title: Re: Room-specific Inventory-to-Character Interaction
Post by: Crimson Wizard on Thu 07/08/2014 18:25:10
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) Select

enum MyRoomEvent
{
  ePlayerUsedInventoryOnSelf,
  eSomethingElseHappened
};


In global script:
Code (ags) Select

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


In room script:

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
    }
  }
}
Title: Re: Room-specific Inventory-to-Character Interaction
Post by: JonCausith on Thu 07/08/2014 18:41:03
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:

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!