Quantcast
Channel: Active questions tagged game-maker - Stack Overflow
Viewing all articles
Browse latest Browse all 189

Having trouble accessing object data from JSON file in Gamemaker

$
0
0

I'm having an issue accessing JSON data for my game. Basically, I'm trying to pull this data in to use as dialogue. Here's the (relevant) snippet of JSON code below:

{"actOne" : {"foxA" : {"gameStateA" : ["Oh hi","So you must be the new fox in town","I'm Beatrice. Not Bea. Beatrice. what's your favorite color?"                {"optionA" : ["Red obvi", 0],"optionB" : ["Black like my heart", 1],"optionC" : ["Gray like my morals", 2]                }            ]        }    }}

And here's the code I'm using to access it in STEP:

currDialogue = jsonParsed.actOne[$ npcName][$ currGameState]

where currDialogue is an array (declared in Create) and is iterated through to draw text to the screen and jsonParsed is the parsed JSON string of course.npcName and currGameState are both obj variables declared in Create as well and in this case, they are currently hardcoded to "foxA" and "gameStateA", respectively.

So, the above code doesn't work. I'm getting this error:

############################################################################################ERROR inaction number 1of  Step Event0for object obj_dialogue:variable_struct_get argument 1 incorrect type (undefined) expecting a Number (YYGI32)at gml_Object_obj_dialogue_Step_0 (line 8) - currDialogue = jsonParsed.actOne[$ npcName][$ currGameState];############################################################################################gml_Object_obj_dialogue_Step_0 (line 8)

HOWEVER, what's really strange is that these lines of code DO work:

currDialogue = jsonParsed.actOne.foxA[$ currGameState]

and

currDialogue = jsonParsed.actOne.foxA.gameStateA

So I know for sure that I am accessing the JSON file correctly and when I use the . accessor it's working. However, I can't just hardcode it like this because those variables need to change depending on which NPC you're talking to, the game state, etc. Is there something wrong with my syntax in using the template string accessor [$ ]?

I am at a complete loss for why this isn't working so any help would be greatly appreciated. On MacOS btw. Thanks in advance!


Viewing all articles
Browse latest Browse all 189

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>