diff --git a/basics/json_encode_decode/.gitattributes b/basics/json_encode_decode/.gitattributes new file mode 100644 index 0000000..187cdf4 --- /dev/null +++ b/basics/json_encode_decode/.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/basics/json_encode_decode/.gitignore b/basics/json_encode_decode/.gitignore new file mode 100644 index 0000000..0f4d613 --- /dev/null +++ b/basics/json_encode_decode/.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/basics/json_encode_decode/example.md b/basics/json_encode_decode/example.md new file mode 100644 index 0000000..d5c65a4 --- /dev/null +++ b/basics/json_encode_decode/example.md @@ -0,0 +1,7 @@ +--- +tags: basic +title: Json encode/decode +brief: This example shows how to encode and decode json data. +author: Jerakin +scripts: json_encode_decode.script +--- diff --git a/basics/json_encode_decode/example/json_encode_decode.collection b/basics/json_encode_decode/example/json_encode_decode.collection new file mode 100644 index 0000000..1ca24c8 --- /dev/null +++ b/basics/json_encode_decode/example/json_encode_decode.collection @@ -0,0 +1,31 @@ +name: "main" +scale_along_z: 0 +embedded_instances { + id: "go" + data: "components {\n" + " id: \"json_encode_decode\"\n" + " component: \"/example/json_encode_decode.script\"\n" + "}\n" + "embedded_components {\n" + " id: \"label\"\n" + " type: \"label\"\n" + " data: \"size {\\n" + " x: 128.0\\n" + " y: 32.0\\n" + "}\\n" + "color {\\n" + " x: 0.0\\n" + " y: 0.0\\n" + " z: 0.0\\n" + "}\\n" + "text: \\\"Player health: ?\\\"\\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" + "" +} diff --git a/basics/json_encode_decode/example/json_encode_decode.script b/basics/json_encode_decode/example/json_encode_decode.script new file mode 100644 index 0000000..c53f84c --- /dev/null +++ b/basics/json_encode_decode/example/json_encode_decode.script @@ -0,0 +1,19 @@ +function init(self) + local jsonstring = '{"current_health":10}' -- <1> + self.player_data = json.decode(jsonstring) -- <2> + + label.set_text("#label", "Player health:" .. self.player_data.current_health) -- <3> +end + +function final(self) + local json_string = json.encode(self.player_data) -- <4> + pprint(json_string) -- <5> +end + +--[[ +1. The data that we want to load. +2. Decoding the json string data into a lua table. We store the data in a varibale we can easily access. +3. We use the data in our script, in this example we display our players health. +4. In our final method of this script we encode the player data into a string again. +5. We print our json string data. +--]] \ No newline at end of file diff --git a/basics/json_encode_decode/game.project b/basics/json_encode_decode/game.project new file mode 100644 index 0000000..e1b5ba9 --- /dev/null +++ b/basics/json_encode_decode/game.project @@ -0,0 +1,25 @@ +[bootstrap] +main_collection = /example/json_encode_decode.collectionc + +[script] +shared_state = 1 + +[display] +width = 720 +height = 720 +high_dpi = 1 + +[android] +input_method = HiddenInputField + +[html5] +scale_mode = stretch + +[project] +title = json_encode_decode + +[render] +clear_color_red = 1.0 +clear_color_green = 1.0 +clear_color_blue = 1.0 + diff --git a/basics/json_encode_decode/input/game.input_binding b/basics/json_encode_decode/input/game.input_binding new file mode 100644 index 0000000..8ed1d4e --- /dev/null +++ b/basics/json_encode_decode/input/game.input_binding @@ -0,0 +1,4 @@ +mouse_trigger { + input: MOUSE_BUTTON_1 + action: "touch" +}