[SOLVED] Character interact event scope: is it possible to call it from room?

Started by Tomags, Sun 26/09/2021 12:35:03

Previous topic - Next topic

Tomags

Hi all,

Just a question about the scope of the events in scripts:
The [character_interact] event, checks whether the mouse interacts with a characters. My question is: is it possible to call it from a room instead of from the Global script?

It is not a major issue, but just want to keep the code tidy: I want to use this event only for one particular room in the game, and ideally would like to keep all the functions and code called on this event on the room script itself and not on the global script (to avoid a massive global script file).

I've tried copying the trigger code (ie, function cChar01_Interact(){...}) from the global script to the room script but it is pretty much ignored as far as I can tell.
If not possible, any advice on worarounds? For example, is there an easy way to detect the mouse interacting with your character from the room itself?

Thanks,
Tomags

Slasher

Could you not use for interaction on character in Room script ot even Global?

Code: ags

}
if(character (script name).Room==(room number)){
//Do this......
}
}

Tomags

Hi Slasher,

Many thanks for coming back. To be honest I didn't know that you could call scripts that way, that is interesting. I'm going to try to play around with it to see if I'm able to apply it on this situation.

Regards,
Tomags

Alan v.Drake

Another option is using CallRoomScript: https://adventuregamestudio.github.io/ags-manual/Globalfunctions_General.html#callroomscript
You could for example define a specific int value for each character and use this system to write the code directly inside the room script, while Globascript merely launches that call and does nothing else.

- Alan

Khris

Does the character walk around? Because if not, you can use an object instead (unless that will show in-game due to your game interface).

Or you can catch the click by adding an on_mouse_click function to your room (which will automatically be called before the global one if it exists):
Code: ags
function on_mouse_click(MouseButton button) {
  Character* c = Character.GetAtScreenXY(mouse.x, mouse.y);
  if (mouse.Mode == eModeTalkto && c == cSomeguy) {
    ClaimEvent();
    cChar01_Interact();
  }
}

Whether this is a sensible approach depends entirely on your template / interface though.

Tomags

Quote from: Alan v.Drake on Sun 26/09/2021 22:54:24
Another option is using CallRoomScript: https://adventuregamestudio.github.io/ags-manual/Globalfunctions_General.html#callroomscript
You could for example define a specific int value for each character and use this system to write the code directly inside the room script, while Globascript merely launches that call and does nothing else.

Thanks Alan, that is great. Again, I didn't know about the CallRoomScript, learning a lot from this problem.
I've tested it and it works very well. This solution doesn't completely avoid the code on the GlobalScript.asc (I still need the trigger to the "Interact" function in there) but allows me to have all the functions and code of the interaction in the room script, that was my goal, so that is great.

GlobalScript.asc:
Code: ags

function cHair01_Interact()
{
  CallRoomScript(1);
}


Room100.asc:
Code: ags

function on_call (int value) {
  if (value == 1) {
    fRemoveHair(cHair01);
    Display("Interaction!");    
  }
}


Btw, I saw your technical demo on cameras, great stuff.


Tomags




Quote from: Khris on Mon 27/09/2021 07:03:01
Does the character walk around? Because if not, you can use an object instead (unless that will show in-game due to your game interface).
To be honest, no. You are right I could have used an object.

Quote from: Khris on Mon 27/09/2021 07:03:01
Or you can catch the click by adding an on_mouse_click function to your room (which will automatically be called before the global one if it exists):
That is perfect. I have something like this on the on_mouse_click event on the room, I didn't think on to easily identifying the character being clicked with the Character.GetAtScreenXY. Great stuff. Thanks for this Khris.

Code: ags

function on_mouse_click(MouseButton button) {
  if (button == eMouseLeft) {
      mouse.Mode = eModeInteract;
      fCut();
      gTijeretazos += 1;
  }
  if (button == eMouseRight) {
    if (gBlockControlTime == 0) {
      mouse.Mode = eModePickup;
      fSpray();
    }
  }
  Character* c = Character.GetAtScreenXY(mouse.x, mouse.y);
  if (mouse.Mode == eModeInteract && c == cHair01) {
    ClaimEvent();
    fRemoveHair(cHair01);
    Display("Interaction!");    
  }  
  
}

SMF spam blocked by CleanTalk