How to generalize properties of similar objects?

Hello,

I am making a basic game with multiple buttons on the screen. I want to be able to control what happens when each button is pressed, but I want there to be the same code for each button in terms of the animations of the button.

Rather than redundantly put the same code over and over for each button, since they will all have almost the same properties, I wanted to be able to create a button class and have the same code apply to everything that I call a button. Does anybody know how I can do this?

This is the event I’m using to manipulate the animation for the button:

You can add multiple instances of the button and your events (with a small annoyance) will work for all of them already :slight_smile:

Now you need to differentiate the button actions. What you want to do can be achieved in two ways, through object variables and object groups.

Object variables:

  • You’ve a single “button” object and add multiple instances on the scene.
  • Edit each instance properties to add an “action” variable with a value like “new game”, “menu”, “exit”.
  • When you detect a click on a button (your first or second event), add sub-events to check the clicked button “action” variable, and launch the corresponding action:

[code]Conditions: Mouse left click pressed
Cursor is over button
Actions: No actions

    // Sub-event
    Conditions: Text variable "action" of button is = "new game"
    Actions: Go to scene "Level_1"

    // Sub-event
    Conditions: Text variable "action" of button is = "menu"
    Actions: Go to scene "Menu"

    // Sub-event
    Conditions: Text variable "action" of button is = "exit"
    Actions: Exit game[/code]

Object groups:

  • Almost the same, now you have multiple button objects (New_Game_Button, Menu_Button, Exit_Button) and add only one instance of each to the scene.
  • Add an object group “button” with all the button objects.
  • Run the conditions and actions over all the objects in the “button” group, and when a button is clicked check the object name through expressions to know the action to launch:

[code]Conditions: Mouse left click pressed
Cursor is over button
Actions: Do = button.ObjectName() to the text of variable “object_name”

    // Sub-event
    Conditions: Text of variable "object_name" is = "New_Game_Button"
    Actions: Go to scene "Level_1"

    // Sub-event
    Conditions: Text of variable "object_name" is = "Menu_Button"
    Actions: Go to scene "Menu"

    // Sub-event
    Conditions: Text of variable "object_name" is = "Exit_Button"
    Actions: Exit game[/code]

You have to filter the button when you’re going to laucha its action, because it’s different for each button, but the rest of the code should be the same (animation when click, when release, when hover) :wink:

Thank you,

I got the instance method to work. It is actually a reasonably efficient system! You can use the same technique to change the text on the button as well, so all you have to do is change the object variable to the button name and everything changes at once.

Another quick question:
Are there any ways to cross multiply 2 vector variables with each other?

IE: variable1.a, variable1.b, variable1.c x variable2.a, variable2.b, variable2.c such that you get variable3.a=a1xa2, variable3.b=b1xb2, etc?

Nope, you have to do it manually, multiplying each component as you do at the end. GD doesn’t know what a vector is :frowning: