GUI code in its own Script

Started by EliasFrost, Thu 21/11/2013 14:32:24

Previous topic - Next topic

EliasFrost

Hey! I'm having a bit of a problem right now, the thing is: I don't want to clog my global script too much with GUI functions and similar but I'm not sure if you can call GUI events through custom script files (I've tried to just copy the functions over, but that doesn't work), do I need to place GUI events in the global script or is there a comprehensible way to split my code into several scripts?

Ghost

GUI event handlers need to be in the global script- but nothing stops you from putting the actual functions that are called into a separate one.

Crimson Wizard

..this will look like

Code: ags

function btnMyButton1_Click(GUIControl *control, MouseButton button)
{
    GuiScript_OnMyButtonClick();
}


Or even
Code: ags

function btnMyButton1_Click(GUIControl *control, MouseButton button)
{
    GuiSystem.OnMyButtonClick();
}

(if you prefer writing in full OO-style)

EliasFrost

Yeah that's what I figured when I played around a bit, it would be much more intuitive to be able to call events from custom scripts though, thanks for the quick answer. :)

Khris

#4
Just in case it isn't clear, you can in theory use a single global script function for almost all GUI events.
The function that is created by AGS is only a suggestion, you don't have to stick to it. All you need to do is enter your function's name into the field next to the event.
Code: ags
// module header

import Buttons(Button *b);

// module script

void Buttons(Button *b) {

  if (b == btnSave) {
    // handle left click on save button
  }
  ...
}


And in the global script:
Code: ags
void HandleGUIClicks(GUIControl *gc, MouseButton button) {
  if (button != eMouseLeft) return;
  if (gc.AsButton != null) Buttons(gc.AsButton);
  ...
}


Now enter "HandleGUIClicks" into every button's on click event field.

EliasFrost

That helped me tremendously, thanks! My keypad gui just got about 10 times smaller in code. Thanks a lot. :)

SMF spam blocked by CleanTalk