diff --git a/file/json_load/.gitattributes b/file/json_load/.gitattributes new file mode 100644 index 0000000..187cdf4 --- /dev/null +++ b/file/json_load/.gitattributes @@ -0,0 +1,42 @@ +# Defold Protocol Buffer Text Files (https://github.com/github/linguist/issues/5091) +*.animationset linguist-language=JSON5 +*.atlas linguist-language=JSON5 +*.camera linguist-language=JSON5 +*.collection linguist-language=JSON5 +*.collectionfactory linguist-language=JSON5 +*.collectionproxy linguist-language=JSON5 +*.collisionobject linguist-language=JSON5 +*.cubemap linguist-language=JSON5 +*.display_profiles linguist-language=JSON5 +*.factory linguist-language=JSON5 +*.font linguist-language=JSON5 +*.gamepads linguist-language=JSON5 +*.go linguist-language=JSON5 +*.gui linguist-language=JSON5 +*.input_binding linguist-language=JSON5 +*.label linguist-language=JSON5 +*.material linguist-language=JSON5 +*.mesh linguist-language=JSON5 +*.model linguist-language=JSON5 +*.particlefx linguist-language=JSON5 +*.render linguist-language=JSON5 +*.sound linguist-language=JSON5 +*.sprite linguist-language=JSON5 +*.spinemodel linguist-language=JSON5 +*.spinescene linguist-language=JSON5 +*.texture_profiles linguist-language=JSON5 +*.tilemap linguist-language=JSON5 +*.tilesource linguist-language=JSON5 + +# Defold JSON Files +*.buffer linguist-language=JSON + +# Defold GLSL Shaders +*.fp linguist-language=GLSL +*.vp linguist-language=GLSL + +# Defold Lua Files +*.editor_script linguist-language=Lua +*.render_script linguist-language=Lua +*.script linguist-language=Lua +*.gui_script linguist-language=Lua diff --git a/file/json_load/.gitignore b/file/json_load/.gitignore new file mode 100644 index 0000000..0f4d613 --- /dev/null +++ b/file/json_load/.gitignore @@ -0,0 +1,11 @@ +/.editor_settings +/.internal +/build +.externalToolBuilders +.DS_Store +Thumbs.db +.lock-wscript +*.pyc +.project +.cproject +builtins \ No newline at end of file diff --git a/file/json_load/example.md b/file/json_load/example.md new file mode 100644 index 0000000..7684388 --- /dev/null +++ b/file/json_load/example.md @@ -0,0 +1,14 @@ +--- +tags: file +title: Load JSON data +brief: This example shows how to load json data using sys.load_resource(). +author: jerakin +scripts: json_load.script +--- + +The example will load a json file. This can be useful for something like level data. + +Before we can load a resource we need to tell Defold that we have custom resources. +We do this by changing the "custom resources" entry within our game.project file. + +![set-custom-resource](set_custom_resource.png) diff --git a/file/json_load/example/json_load.collection b/file/json_load/example/json_load.collection new file mode 100644 index 0000000..9b8cf8a --- /dev/null +++ b/file/json_load/example/json_load.collection @@ -0,0 +1,42 @@ +name: "main" +scale_along_z: 0 +embedded_instances { + id: "go" + data: "components {\n" + " id: \"json_load\"\n" + " component: \"/example/json_load.script\"\n" + "}\n" + "embedded_components {\n" + " id: \"title\"\n" + " type: \"label\"\n" + " data: \"size {\\n" + " x: 128.0\\n" + " y: 32.0\\n" + "}\\n" + "text: \\\"No level loaded\\\"\\n" + "font: \\\"/builtins/fonts/default.font\\\"\\n" + "material: \\\"/builtins/fonts/label-df.material\\\"\\n" + "\"\n" + " position {\n" + " x: 360.0\n" + " y: 360.0\n" + " }\n" + "}\n" + "embedded_components {\n" + " id: \"Description\"\n" + " type: \"label\"\n" + " data: \"size {\\n" + " x: 128.0\\n" + " y: 32.0\\n" + "}\\n" + "text: \\\"Press \\\\\\\"1\\\\\\\" to load level_001 and \\\\\\\"2\\\\\\\" to to load level_002.\\\"\\n" + "font: \\\"/builtins/fonts/default.font\\\"\\n" + "material: \\\"/builtins/fonts/label-df.material\\\"\\n" + "\"\n" + " position {\n" + " x: 360.0\n" + " y: 50.0\n" + " }\n" + "}\n" + "" +} diff --git a/file/json_load/example/json_load.script b/file/json_load/example/json_load.script new file mode 100644 index 0000000..26cc838 --- /dev/null +++ b/file/json_load/example/json_load.script @@ -0,0 +1,30 @@ +local function load_level(level_name) + local level_path = "/levels/" .. level_name .. ".json" -- <1> + local data = sys.load_resource(level_path) -- <2> + local json_data = json.decode(data) -- <3> + label.set_text("#title", json_data.title) -- <4> +end + +function init(self) + msg.post(".", "acquire_input_focus") +end + + +function on_input(self, action_id, action) + if action_id == hash("key_1") then + if action.released then + load_level("level_001") + end + elseif action_id == hash("key_2") then + if action.released then + load_level("level_002") + end + end +end + +--[[ +1. Convinience sake we only want pass in the name of the level, but to load the resource we need to give it the full path. +2. Load the resource, this will return a string. +3. Use the json.decode to make our string into a lua table. +4. Use the loaded level data in whatever way we want. +--]] diff --git a/file/json_load/game.project b/file/json_load/game.project new file mode 100644 index 0000000..b4033b5 --- /dev/null +++ b/file/json_load/game.project @@ -0,0 +1,21 @@ +[bootstrap] +main_collection = /example/json_load.collectionc + +[script] +shared_state = 1 + +[display] +width = 720 +height = 720 +high_dpi = 1 + +[android] +input_method = HiddenInputField + +[html5] +scale_mode = stretch + +[project] +title = json_load +custom_resources = levels + diff --git a/file/json_load/input/game.input_binding b/file/json_load/input/game.input_binding new file mode 100644 index 0000000..c0119e2 --- /dev/null +++ b/file/json_load/input/game.input_binding @@ -0,0 +1,8 @@ +key_trigger { + input: KEY_1 + action: "key_1" +} +key_trigger { + input: KEY_2 + action: "key_2" +} diff --git a/file/json_load/levels/level_001.json b/file/json_load/levels/level_001.json new file mode 100644 index 0000000..f9bbfc9 --- /dev/null +++ b/file/json_load/levels/level_001.json @@ -0,0 +1,3 @@ +{ + "title": "Starting Area" +} \ No newline at end of file diff --git a/file/json_load/levels/level_002.json b/file/json_load/levels/level_002.json new file mode 100644 index 0000000..e6580de --- /dev/null +++ b/file/json_load/levels/level_002.json @@ -0,0 +1,3 @@ +{ + "title": "Mystic Glades" +} \ No newline at end of file diff --git a/file/json_load/set_custom_resource.png b/file/json_load/set_custom_resource.png new file mode 100644 index 0000000..d43dcce Binary files /dev/null and b/file/json_load/set_custom_resource.png differ