Major trouble scripting custom GUI

Started by MuppleWill, Thu 09/09/2021 15:11:44

Previous topic - Next topic

MuppleWill

Hi,

So I've been working on a new game and I've run into some real trouble when it comes to scripting my own custom GUIs. I've checked all of the resources I can find, watched a bunch of YouTube tutorials, and searched the forum but I can't seem to find an answer.

I made a menu button, created another GUI for it to point to and everything worked great. However, in the menu, none of the buttons worked at first. I had all the settings changed correctly (visible, clickable, enabled, RunScript, etc) on both the buttons and the GUI itself, but despite this, the buttons don't actually seem to run the script. I can tell that they're registering the click because the image changes to the 'pressed' image.

After a bit of pratting about, one of the in-menu buttons started to work, but I have no idea why. The scripting and properties are the same as the rest of my buttons. I've included all of the code relevant to my GUIs below to show you what I mean.

Code: ags

function Button1_OnClick(GUIControl *control, MouseButton button)
{
gInventory.Visible = true;
}

function Button6_OnClick(GUIControl *control, MouseButton button)
{
gInventory.Visible = false;
}

function Button2_OnClick(GUIControl *control, MouseButton button)
{
gRestoreGame.Visible = true;
}

function Button5_OnClick(GUIControl *control, MouseButton button)
{
gInventory.Visible = false;
gInGameMenu.Visible = true;
}

function Button3_OnClick(GUIControl *control, MouseButton button)
{

gSaveGame.Visible = true;
}

function Button7_OnClick (GUIControl *control,  MouseButton button)
{
  gInGameMenu.Visible = false;
  gInventory.Visible = true; 
}


I've tried having it both ways, i.e. leaving the old GUI still visible and just putting the new one over the top, and actually closing out the old menu first, in case it was getting in the way. Doesn't seem to make a difference.

I also did think it might be something to do with the zOrder after fishing around the FAQ, but as far as I can tell there's nothing wrong with my ordering. When I was using the method of making the old GUI invisible that should have stopped it being clickable completely, presumably.

Any help on this would be much appreciated, even if it's just telling me where I can find the answers.

Khris

#1
The most important thing for a button to work is that its "on click" event is actually linked to a function. A simple shortcut to achieve this is to double-click the button in the GUI editor; this will create a function based on the button's name at the end of the GlobalScript and send you there you put some code inside.
Did you do this?

Or did you manually type / copy-paste these function names and params into the script?

You can check if a button is linked to a function by selecting it, then clicking the âš¡ icon above its properties.
The name of the function can be any valid name so you can fix this simply by pasting the respective functions name into the blank field next to "OnClick" in the events table.

MuppleWill

Yeah, I always use the events shortcuts when I'm scripting unless I'm messing around with something where that's not an option.

As far as I can tell they're all pointed to the right buttons and stuff.

In the code button1 and button5 work, none of the others do.

Really baffled by all of this if I'm being completely honest. If I can't figure out what's going on I might just ditch all of the menu GUIs I've made and start from scratch.

Khris

Wait, can you zip your source files and upload them somewhere so we can take a look? I'm sure we can resolve this.

MuppleWill

sure, I actually keep all my project files on GDrive anyway so I can work between my desktop and laptop.

I can just DM/email you a link to the folder if that'll work for you?

Khris


MuppleWill

#6
DM sent. Apologies in advance that you have to look at this thing haha.

Oh, on that note there are also a few typos that I haven't fixed yet because I'm still very early on. So sorry about that too.

Khris

I was looking at Button7 of the InGameMenu GUI, and its click event was linked to close_GUI_OnClick, a function that doesn't exist. I put Button7_OnClick in the field instead and the button started to work fine.

Next I looked at the Load Game button; it displays the RestoreGame GUI, however the menu GUI has a zOrder of 1 and the restore GUI has the default zOrder of 0, so it "appears" behind your main GUI, i.e. remains invisible.

To debug this I recommend adding lines like  Display("button 7 clicked");  to your functions; that way you can first make sure that the function linkage is OK, then go from there.

MuppleWill

Well, I feel like an idiot now haha.

I  forgot that I was getting this GUI stuff done at like 4 AM last night and came across some old functions from a previous version of AGS and tried those before I found out they were obsolete.

Thanks very much for your help!

Khris

You're welcome :)

It's easy to miss the event function link stuff; you'll quickly get the hang of it.

SMF spam blocked by CleanTalk