Store scripts outside of GlobalScript with Events

Started by saintnightlyy, Tue 08/03/2022 11:05:41

Previous topic - Next topic

saintnightlyy

So hello frens I had a quick question, whenever I want to add an event to whatever (GUI thing, inventory item thing etc) it tells me to write it in GlobalScript and I think that'd be a problem no? Like it could potentially get bloated in stuff or something. I'd rather divide everything in a bunch of little scripts or like, say, a script for GUI events, another for inventory item events, so on. How do I do this? Thank u all

eri0o

Write a function to handle these events in a new script as you say, but place it's import in the script header. Now on Global Script, call this function when said event happens.

There's no way currently to link a GUI event to a script that's not in global script.

Also note you can pass what called the event in first place (the GUI or GUI Control) so you can in the function in your script figure which caller is. This can help reuse a same event in global script for multiple GUIs and GUI Controls.

saintnightlyy

Quote from: eri0o on Tue 08/03/2022 11:14:18
Write a function to handle these events in a new script as you say, but place it's import in the script header. Now on Global Script, call this function when said event happens.

I'd appreciate some form of example. Sorry, I have never programmed anything, this is my first time being exposed to some sort of 'scripting language' I guess you could call it... Thanks for responding by the way

Khris

Here's the way to move a function out to a module, in the order of the script tree:

Newly created module
Code: ags
// module header, this ends up on top of scripts below itself so
// needs to contain the import line

// import line; same return type, parameters, no function body though
import function InvItemHandler(InventoryItem* item);

Code: ags
// module script, actual function here

function InvItemHandler(InventoryItem* item) {
  if (mouse.Mode == eModeLookat) {
    // ...
  }
}


Global script
Code: ags
// global script
function iAnyItem_AnyClick() {
  InvItemHandler(inventory[game.inv_activated]);
}


Now paste  iAnyItem_AnyClick  into every inventory item's "any click" event field and you can handle all interaction in the module's function.

SMF spam blocked by CleanTalk