Is it possible to use a GUI template that has a script attached?

Hi guys, I’ve just started developing a small test project in Defold and I was having some trouble with GUI elements.

I’ve written a small script for a generic button that is going to handle things like animating when the mouse moves over it, etc.

If I place that GUI into a collection and run the game, it works.

If I place it as a template into a GUI and then place that GUI into a collection and run the game, it does not.

Do I need to write separate scripts to handle every piece of GUI? Or is what I’m trying to do possible. I could possibly do it by writing a static set of button functions for handling the animation, and then passing the desired gui nodes into that via a script but that seems odd.

I’m basically just trying to create a nice generic button that I can hook up to send whatever message I want to whatever game object I want.

Cheers
Adrian

edit: Ok I’ve discovered go.property, so that’s really useful! Is it possible to use this on gui_scripts? But I’m still wondering about running scripts on templates.

1 Like

There is one script associated with each gui component. Any templates you add will get its nodes brought in, not the script.

2 Likes

Could I then use the gui script to add a script to those template objects to handle that?

Or are you saying if I wanted Button 1 to be able to animate, change colour, etc. I’d need to duplicate that code for Button 2 and Button 3?

1 Like

No, there is no notion of scripts belonging to nodes in Defold. Each instance of the template will have nodes named according to the template structure so you can definitely write generic code for any number of button instances, for example.

1 Like

So the best way of doing this would be to write a module that handles all the animating/resizing/sound effects/etc for the buttons. And then in the GUI script just pass the nodes over to that?

Seems a bit odd but thanks for answering, I just wanted to make sure I hadn’t missed something obvious.

2 Likes

Either that of just keep the functions you need in the gui script. Depends on how you want to use/reuse them.

Nothing odd att all. It’s just functions operating on data.

3 Likes