Using other scripts for events other than "GlobalScript"

Started by EnoRed, Tue 10/08/2010 18:42:30

Previous topic - Next topic

EnoRed

Wow, been a while since I've posted here...

I just wanted to ask how to make events refer to a different script other than the "GlobalScript" for interactions and such. Basically I'm afraid the GlobalScript will get pretty cluttered, having to put in interactions for every single character and item not confined by one room script, so I wanted to check if there was a way I could make seperate scripts (like one for all characters or inventory items etc.) and how to make the events refer to them intead of the Global Script.

GarageGothic

#1
Not really, I'm afraid. I also think it's weird that you can't just put your own function call in place of the link to the auto-generated interaction script, but if there is at least I never got it to work. You can of course use the auto-generated script just to call your own function from a module, but it's still a bit of a mess.

The cleanest solution would probably be to handle all global interactions inside the on_mouse_click function of a module. It would mean a longish list of if-else statements, but at least it cuts down on the global script length and allows an easier-to-read code structure.

Khris

I tested this and got a puzzling result:

Putting your own function call into a character's event field is very much possible. E.g. one could put "cha_event" into every character's "Any click on" field, then handle the specific action by checking the mouse.Mode and Character.GetAtScreenXY().

But: when I moved the function "cha_event" to a module script and imported it in the header to make it global, AGS wouldn't execute the function any longer, didn't crash or hang though.
The solution is simple enough: put a second function in the global script and call the first one from there.

Thus it is definitely not necessary to have a function in the global script for each interaction, even if all it does is call another function.

Expanding on the idea of putting all character interactions in a module, I guess the best way to further split stuff is to have a function for each verb. The main opportunity to save code is using generic responses.
Using eModeInteract on people e.g. is rarely every used, and provided the characters have organized IDs, it could look like this:

Code: ags
void cha_grope(Character*c) {
  if (c.ID >= 12 && c.ID <= 15) {
    // handle character groping a male bar patron
  }
  else if (c.ID >= 16 && c.ID <=18) {
    // handle character groping a female bar patron
  }
  else if (c == cTrixie) {
    // handle character groping his best friend, Trixie the stripper
  }
  ...
}


In the global script, use something like

Code: ags
void cha_event() {
  Character*c == Character.GetAtScreenXY(mouse.x, mouse.y);
  if (mouse.Mode == eModeInteract) cha_grope(c);
  if (mouse.Mode == eModeLookat) cha_describe(c);
  ...
}





On that note, global interactions that are tidier and more comfortable to code in a room script could be done using enums and CallRoomScript/on_call().

Say the player has a tricorder, and depending on which objects are present in a room it gives a different analysis:

Code: ags
// header

enum global_event {
  CheckTricorder,
  BlowHorn,
  UseCellphone
};

// main module script

void inv_interact(InventoryItem*i) {
  if (i == iTricorder) {
    if (player.Room >= 7 && player.Room <=10 && !tricorder_upgraded)
      Display("This planet's atmosphere interferes with the readings. An update might help.");
    else
      CallRoomScript(CheckTricorder);
  }
}

// room script

void on_call(int p) {
  if (p == CheckTricorder) {
    Display("There's a high amount of lethal radiation here.");
    if (oRock.Visible) Display("It seems to originate from the pile of rocks.");
    else Display("It originates from the leaking barrel of atomic waste.");
  }
  ...
}

GarageGothic

Quote from: Khris on Wed 11/08/2010 01:41:15Putting your own function call into a character's event field is very much possible. [...] But: when I moved the function "cha_event" to a module script and imported it in the header to make it global, AGS wouldn't execute the function any longer, didn't crash or hang though.

How weird, I guess I must have been referring to a module then. I was under the firm impression that messing in any way with the generated functions would break the link. There was one situation I remember in particular, where I changed the script name of a character and updated all function names to reflect that, then modified the RunScript properties accordingly only to find them unresponsive in-game. Had to recreate all the RunScript interactions and paste in the code from the old functions. That was in 2.72 or possibly even earlier, though.

Thanks for the info, I will try it out when I get the time. I *really* didn't like the clutter of individual functions for each interaction, so I'd totally ditched the built-in Events properties since redirecting the script calls didn't seem to work. Not sure I'd gain much by resuming use of them now, but it's nice to know the option is available for future projects. Cheers.

SMF spam blocked by CleanTalk