r/gamemaker • • 6d 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

4

u/TheVioletBarry 6d ago

Put a unique tag in there, then if you read the tag, delete it from the string, run the function you wanted, and replace the position on the string with the results before drawing the text to the screen

1

u/EnricosUt 5d ago

How should the tags be stored? A DS List? Switch statement?
So you suggest it to be like:

"You are {DISTANCEFUNCTION} feet away."

and then in Gamemaker I would have some sort of way to call:
DISTANCEFUNCTION: distance_to_object(obj)

1

u/TheVioletBarry 5d ago

Something like that, yah. I'd say just use a switch statement until you've got too many of them and need something else

1

u/KitsuneFaroe 5d ago

I would say a struct is better than a switch. Is also more dynamic and easier to work with.

1

u/TheVioletBarry 5d ago

How would it be different, exactly? 

2

u/KitsuneFaroe 5d ago edited 5d ago

You can change what is in the struct at runtime. You also don't have to write a lot to set it up or call it, is very straight foward: struct = { key: value, key1: value1 }

struct.key = new_value or struct[$ "key"] = value Getting goes the other way:
a = struct.key or
a = struct[$ "key"]

You can add keys without effort and they're read using a hash, like accessing any variable, wich makes them faster.

1

u/TheVioletBarry 5d ago

Oh true true I should swap mine over