Access object variable dynamically

I have a scene variable structured like this:

  • Dialog
    – type : value

and an object with these variables structure:
dialogBG (object)

  • info
    – xPos : value
  • another
    – xPos : value

I need to access the object xPos value based on the GlobalVariable Dialog.type
I’ve tryed different combinations but without success…
like this is not working: Variable(dialogBG[VariableString(Dialog.type)].xPos)

How has to be written?
Many thanks!

If you need to access an object variable, you need to state it:
dialogBG.Variable(…)
And if you need a value inside brackets for dynamic access, don’t forget to ToString() it.

Check this page for proper syntax.

@Gruk sorry but I’ve tried

dialogBG[ToString(Variable(Dialog.type))].xPos

and

dialogBG[ToString(Variable(Dialog.type))][“xPos”]

but nothing works, the editor tell me there’re errors…
Can you write it as it is correct?

also the syntax in the wiki PlayersStats[“Level”+ToString(Variable(CurrentLevel))]
give me: 1. no operator find… and 2. you must enter a valid number or expression call

(I’ve tried this inside a Change object variable value action)

So until now I know that:

dialogBG.Variable(info.xPos) – works
dialogBG.Variable(info[“xPos”]) – works

dialogBG.Variable([“info”][“xPos”]) – doesn’t work

and I need that “info” to be dynamic, is it possible?
It seems stupid to think that I have to do another level on the objects variable so that I can do
dialogBG.Variable(something[VariableString(Dialog.type)].xPos)

What you are asking doesn’t make any sense to me.

Dynamically accessing a variable requires something to define what the engine should be looking at.

How is it supposed to know whether it should be going to info or something else?

You have to have something to point the operators.

Let’s try to clear this up.
You have a scene variable Dialogue.sample that is worth 1.
You have a dialogBG object with a variable content.1 that is worth “that’s a sample”.

If you want dialogBG to show “that’s a sample”, you use:
dialogBG.VariableString(content.1) or
dialogBG.VariableString(content[ToString(Variable(Dialogue.sample))])

The square brackets allow dynamic access to children of structures, as stated on the wiki.

Sorry but what you mean?
I would like to have the value of a scene variable to tell him if it’s “info” or “something else”… But as @Gruk confirmed that’s not possible because dynamic access is valid only for children so I’ll add another level to make it work.

Anyway maybe I could have found out alone but thanks everyone for the help and the support.

The use case still doesn’t make sense to me. Why would your object store 2 different x positions on an object? Are you going to have unique dialog boxes objects for each dialog? That could be negatively impactful on performance.

You’d be better storing your data in scene and/or global variables, then creating the respective dialog box object with the settings you want based off those variables.

All of that said, you can also accommodate what you want by just having 2 separate events:

If the value of Scene variable "Dialog.Type" = "info" | Do whatever you need to variable "info.xPos" of object "dialogBG"
If the value of Scene variable "Dialog.Type" = "another"  |  Do whatever you need to variable "info.xPos" of object "dialogBG"

Yeah I could have done it with the if/else but if I’ll need another position that would require to code it again in another condition.
Yes, I have a dialog with pretty much the same elements that could slide in from right with a defined width/height/position… and slide in from bottom with another width/height/position.
I’ve think I was less heavy using one object and just moving it to different positions cause I create it at the beginning of the scene a hidden layer and than change text/position and make it appear/disappear.
I also use the same image (it’s a tile sprite) in white and change its color by code.

I tought it was the best way in terms of performance but if I was wrong please tell me, I’m glad to learn!

Thanks again

Hmmmm…

I honestly think you’d be better off just having a “slide in from right” dialogue object, and “slide in from bottom” dialogue object.

Then you can just have a condition of “If variable = right | slide in dialogueright” or something.

I was more worried you were going to have a unique dialogue object for every single dialogue you were going to have in the game.

In this instance, with only 2 objects, you don’t get a lot of benefit by worrying about dynamic variables.