Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: EliasFrost on Thu 21/11/2013 14:32:24

Title: GUI code in its own Script
Post by: EliasFrost on Thu 21/11/2013 14:32:24
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?
Title: Re: GUI code in its own Script
Post by: Ghost on Thu 21/11/2013 14:49:25
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.
Title: Re: GUI code in its own Script
Post by: Crimson Wizard on Thu 21/11/2013 14:50:18
..this will look like

Code (ags) Select

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


Or even
Code (ags) Select

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

(if you prefer writing in full OO-style)
Title: Re: GUI code in its own Script
Post by: EliasFrost on Thu 21/11/2013 15:12:53
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. :)
Title: Re: GUI code in its own Script
Post by: Khris on Thu 21/11/2013 15:37:24
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) Select
// 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) Select
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.
Title: Re: GUI code in its own Script
Post by: EliasFrost on Fri 22/11/2013 18:42:40
That helped me tremendously, thanks! My keypad gui just got about 10 times smaller in code. Thanks a lot. :)