Help With Dictionary

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By vonflyhighace2
:warning: Old Version Published before Godot 3 was released.

I can’t seem to reproduce the same functionality that python has when it comes to dictionaries. I have a function inside a dictionary func foo(): but when I try to access it using standard python syntax eg."myDict['foo']() theirs automatically a error with the IDE saying that it needs an end of statement after the expression. What Gives? I’m still a newb after all I guess

Perhaps it’s cause Godot doesnt use Python?

The_Duskitty | 2016-03-31 04:58

Try

myDict["foo"]

and yeah it’s GDScript not Python.

docs

duke_meister | 2016-03-31 06:09

@duke_meister That won’t change anything :D

Bojidar Marinov | 2016-03-31 09:15

:bust_in_silhouette: Reply From: Bojidar Marinov

The problem is that functions are not first-class objects. This means that objects/classes can have methods, but those methods are completely useless unless you have both the object and the method. Also, the other point is that you can’t just mixin a function into an existing object, without changing its class.

What you might be trying to do is using Dictionary as a class for everything, JavaScript-style. In GDScript, this is done by making a new class, either via a new .gd file, or via the class keyword. You can find more information about this on the GDScript article in the docs or in this other answer I gave