(SOLVED)Accessing objects within custom objects

Hi!

A simple question:
I have a custom object consisting of a sprite and a bbtext slapped on it.
How can I access text of the bbtext object so I can change it?

Say, just simple button functionality - I click on the instance of composite object “Bumper”, and Bumper’s “BumperValue” variable increases by one. Now how do I get the text of “BumperDisplayValue” bbtext object to update?

You can add a function to your custom object to change the text of its child object.
https://wiki.gdevelop.io/gdevelop5/events/functions/

Uff… I feared that… It seems really complicated. Not sure I’m on that level yet :frowning:

Phew! Managed to do it!
For future reference, if anyone needs it, here’s what I did:

  1. Create a custom object. (I call it “Bumper” it has a sprite and bbtext “BumperValueDisplay” on it.) Give it a variable “BumperValue”
  2. Add a function to it. A doStepPostEvents one in my case. (If you don’t know where it is, it is in the custom extension you created to store your custom object)
  3. You get an event sheet, and here it becomes tricky: The key is in the following action:
    “Change the BBCode text of BumperValueDisplay: set to ToString(Object.Variable(BumperValue))”
    “Object” is created (automatically I believe) as a parameter of the custom object which you can then use in custom functions belonging to it. It references the root object itself.
    So Object.Variable(BumperValue) accesses the “BumperValue” variable of the root object, as defined in the main game.
    The tricky part was that the expression builder didn’t offer me a list of root object’s variables so you have to remember and type in the name of the variable yourself.

Maybe there are more elegant ways to do it, and I’m sure I didn’t explain it as clearly as possible, but there it goes. Perhaps it can be of some help to a newbie like me. Cheers!

2 Likes

Have you tried with a property?
(It’s the doc for behaviors, but it works the same for objects)
https://wiki.gdevelop.io/gdevelop5/behaviors/events-based-behaviors/#use-the-behavior-on-an-object

1 Like