how do i acess a dictionary with a variable?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By DIenve

sry having a hard time with the syntax in many cases. Could not figure it out my self for a few hours now.

var s= “scene”+str(global.scene)

i want to replace scene1 with the var s like…


:bust_in_silhouette: Reply From: wombatstampede

We’re talking GDScript here, right?

I suppose you already read about Dictionaries here?:

I propose that you access Dictionaries elements with [].

var dia = {"scene1": {"row1":{"someid":12, "text": "hi there"},"row2":{}},"scene2":{}}

print("text contents: " + dia["scene"+str(global.scene)]["row1"]["text"]))

Edit and another example:

  var six = "scene"+str(global.scene)
  if dia.has(six):
    var s = dia[six]
    print("index '"+six+"' unknown in dia")

Thx for the response. Its been a while. (25jears) last time i programed (turbo pascal)

the first probpose gave out the same error that i was dealing with before:. expected end of statement after expression in the print line around this position [“text”]))

but the second example worked. Thank you!

DIenve | 2019-05-24 09:04

ah ok there was a ) to much at the end!


DIenve | 2019-05-24 09:13

Yes turbo pascal. Skipped the CP/M version (and UCSD-Pascal) and began with it on MS-DOS. Uh, getting off topic.

wombatstampede | 2019-05-24 09:30

wow CP/M nice. Childhood memorys of my c128.

Just wanted to thank you again. Im realy happy to be able to continiue. The helped alot. Sometimes you stand in the tub (i hope my english was correct)

DIenve | 2019-05-24 09:38