My friends, we’ve journeyed together through the labyrinthine landscape of JSON, exploring its structure, peering into its nested complexities. Today, let’s raise the curtain on our third act – a thorough investigation of GameMaker Studio 2’s repertoire of JSON functions, the very instruments that enable us to orchestrate the melody of our game’s data.
Let’s begin our symphony with the cornerstone of our JSON orchestra – json_encode()
and json_decode()
. As the yin and yang of the JSON world, these functions allow us to convert GameMaker data structures into JSON strings and vice versa. Imagine json_encode()
as the magician who transforms a rabbit into a dove, or, in our world, a ds_map into a JSON string.
var player = ds_map_create();
player[? "name"] = "Gerald";
player[? "level"] = 25;
var jsonString = json_encode(player);
Our json_encode()
function has effortlessly transmuted our ds_map into a JSON string. But what happens when we wish to reverse the spell? This is where json_decode()
comes into play. Just as the dove transforms back into the rabbit, json_decode()
allows us to retrieve our original data from the JSON string.
var decodedMap = json_decode(jsonString);
show_message(decodedMap[? "name"]); // This will display "Gerald"
But why would you need to encode and decode data, you ask? Picture an intricate multiplayer game, where player data must be transmitted across the ether of the internet. json_encode()
allows you to package this data into a neat JSON string, ready to be sent on its journey. On the other side, json_decode()
unravels the package, translating the received JSON string back into usable game data.
Next, we must acquaint ourselves with json_get_type()
, the function that peers into the essence of our JSON value and determines its type. Like a discerning jeweler inspecting a gem, this function reveals whether our JSON value is a real, a string, an array, or an object.
var valueType = json_get_type(decodedMap[? "level"]);
show_message(valueType); // This will display "real"
Our json_get_type()
function has deduced that the “level” of our player is a real number. Recognizing the type of our JSON value is critical when we’re navigating our data, ensuring we handle each piece appropriately, like knowing which tool to use when sculpting a masterpiece.
As we venture deeper into our exploration, we encounter json_stringify()
and json_parse()
. These sophisticated functions are the advanced cousins of json_encode()
and json_decode()
, adding layers of precision and control to our symphony.
json_stringify()
encodes our data into a JSON string, but with the added ability to beautify the output. Consider this function as the makeup artist, adding definition and color to our raw form.
var jsonString = json_stringify(player, true);
Our json_stringify()
function with the second argument set as true will print the JSON string in a human-friendly, indented format, making it easier to read and debug.
Conversely, json_parse()
decodes a JSON string, but it also verifies if the JSON string is valid. It’s like our personal detective, ensuring we’re dealing with authentic information.
var parsedMap = json_parse(jsonString);
Using json_parse()
, we can protect our game from potential data corruption, avoiding runtime errors, and ensuring smooth gameplay.
As we conclude today’s masterclass, remember, dear reader, the power of these functions lies not just in understanding them but in harmoniously orchestrating them to shape your game’s data structure. In our next chapter, we will delve further into the practical applications of JSON in the world of GameMaker Studio 2. Until then, let the symphony of JSON play on in your mind.