User Tools

Site Tools


data_files

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
data_files [2020/05/05 11:07] justindata_files [2022/06/26 02:27] (current) justin
Line 2: Line 2:
 ---- ----
  
-Although it should be considered an experimental feature at this time and requires a bit of manual setup, it's possible to access custom data stored in JSON files from [[script|scripts]]. To do so, you must first create a subfolder named "data" within your game's project folder and place the desired JSON file into that folder. When [[exporting your game]], the editor will automatically include any files in the data folder having the .json extension.+Although it requires a bit of manual setup, it's possible to access custom data stored in JSON files from [[script|scripts]]. To do so, you must first create a subfolder named "data" within your game's project folder and place the desired JSON file into that folder. When [[exporting your game]], the editor will automatically include any files in the data folder having the .json extension.
  
 As a simple example, let's say you wanted to maintain a list of [[item|items]] that the [[player character]] should begin the game with without needing to update your game's [[startup script]] every time the list changes. In this scenario, you could create a JSON file within the data folder named "start_items.json" that looks something like this, with an array of [[item]] IDs corresponding to the desired [[item|items]]. As a simple example, let's say you wanted to maintain a list of [[item|items]] that the [[player character]] should begin the game with without needing to update your game's [[startup script]] every time the list changes. In this scenario, you could create a JSON file within the data folder named "start_items.json" that looks something like this, with an array of [[item]] IDs corresponding to the desired [[item|items]].
Line 14: Line 14:
 </code> </code>
  
-You can then use the "data" syntax to reference the filename (minus the .json extension) and access its data from a [[script]]. In our example, you could use this syntax along with a "for" loop to give each [[item]] to the player from your game's [[startup script]].+You can then use the "data" syntax to reference the filename and access its data from a [[script]]. In our example, you could use this syntax along with a [[script_syntax|For loop]] to give each [[item]] to the player from your game's [[startup script]].
  
 <code bauxite> <code bauxite>
-for item_id in data["start_items"] do +for $item_id in data["start_items.json"] do 
-   give_item(item_id) +   give_item($item_id); 
-end+end
 +</code> 
 + 
 +Alternatively, each [[item]] in the list could be accessed using the array index syntax, or you could even randomly select one [[item]] from the list to give to the player. 
 + 
 +<code bauxite> 
 +for $i in range(3) do 
 +   give_item(data["start_items.json"][$i]); 
 +end; 
 +</code> 
 + 
 +<code bauxite> 
 +give_item(data["start_items.json"][random(0, 2)]);
 </code> </code>
  
Line 37: Line 49:
 </code> </code>
  
-Using the expanded JSON file, the [[script]] below will give the player two of ITEM_0001 and five of ITEM_0002.+Using the expanded JSON file containing counts for each [[item]], the [[script]] below would give the player two of ITEM_0001 and five of ITEM_0002.
  
 <code bauxite> <code bauxite>
-for item in data["start_items"] do +for $item in data["start_items.json"] do 
-  give_item(item["id"], item["count"]) +  give_item($item["id"], $item["count"]); 
-end+end;
 </code> </code>
  
 +As another example, assume that we want to maintain a list of [[entity]] properties that will be assigned to a [[character]] at some point during the game. A file named "npc_props.json" could be created in the data folder with the following contents (specifically an object with a set of key/value pairs as the root element).
 +
 +<code javascript>
 +{
 +   "visited": false,
 +   "visit_count": 0,
 +   "mood": "happy"
 +}
 +</code>
 +
 +You could then loop through the key/value pairs and assign each value to a property on the [[entity]].
 +
 +<code bauxite>
 +for $prop in data["npc_props.json"] do
 +   set_entity_property(entity["npc01"], $prop, data["npc_props.json"][$prop]);
 +end;
 +</code>
  
 ~~NOTOC~~ ~~NOTOC~~
data_files.1588702070.txt.gz · Last modified: 2020/05/05 11:07 by justin