I am trying to use a string ('npcName') as a variable name. So far I have tried casting dialogMap into a DynamicAccess object, but it gives me the error 'Invalid array access' when I try this:
var npcName:String = 'TestNPC';
var casted = (cast Registry.dialogMap:haxe.DynamicAccess<Dynamic>);
var tempname = casted[root.npcName[0].message];
trace(tempname);
'dialogMap' is an empty map which I want to fill like so:
Registry.dialogMap['message'] = root.npcName[0].message;
How can I use npcName, a string, in the above line of code? Is there a way to transform the string into something usable? Any help would be appreciated.
The
haxe.DynamicAccess
doesn't have array access (likemap[key]
), but is an abstract type for working with anonymous structures that are intended to hold collections of objects by the string key. It is designed to work withmap.get(key)
andmap.set(key)
. It is basically a nicer wrapper aroundReflect.field
andReflect.setField
and does some safety checks withReflect.hasField
.I'm noticing you are doing very much cast and dynamic, so untyped code, which is a bit of contradiction in a typed language. What is the actual type of dialogMap?
Not sure you are aware of it but, Haxe has its own maps, which are fully typed, so you don't need casts.
I think this article helps understanding how to work with dynamic (untyped) objects.
Hope this helps, otherwise here is some relevant documentation: