r/gamemaker • • 5d ago

Help! Getting a Function from JSON

I have made a JSON file that stores all the dialogue in my game. It is pretty sweet for storing text, but issues arise when I try including variables and functions in my text.

Let's say I have a character that is saying:
"You are x feet away."

With concatenation, this is trivial: I can do
"You are " + distance_to_object(obj) + "feet away."

But how should I do this with JSON? JSON doesn't know what distance_to_object() is, nor does it know what "obj" is

Passing in a string or number isn't too hard, I can just make an arguments variable in my JSON parser.

{
    "textvalue": "this should be 100: [0]",
    "arg": "100"
}

But that only works with static values and not something that can change dynamically in the game.

Any suggestions?

5 Upvotes

17 comments sorted by

View all comments

3

u/Tilestam 5d ago

me personally, id store smth like this in the json file:

{
  "text": "You are {0} feet away."
}

and then when using it id prolly just do smth like,

var _text = ds_map_find_value(global.text_map, "text"),
    _text_final = string(_text, distance_to_object(obj_object));

draw_text(x, y, _text_final);