Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions basics/json_encode_decode/.gitattributes
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions basics/json_encode_decode/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/.editor_settings
/.internal
/build
.externalToolBuilders
.DS_Store
Thumbs.db
.lock-wscript
*.pyc
.project
.cproject
builtins
7 changes: 7 additions & 0 deletions basics/json_encode_decode/example.md
Original file line number Diff line number Diff line change
@@ -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
---
31 changes: 31 additions & 0 deletions basics/json_encode_decode/example/json_encode_decode.collection
Original file line number Diff line number Diff line change
@@ -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"
""
}
19 changes: 19 additions & 0 deletions basics/json_encode_decode/example/json_encode_decode.script
Original file line number Diff line number Diff line change
@@ -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>
Copy link
Contributor

@britzl britzl Oct 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem with this example is that anyone looking at the example won't really be able to run the entire project on the webpage. The result of the code running in final will never be seen when the user navigates away from the example. I think it would be better to show both json.encode() and json.decode() in the example somehow.

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.
--]]
25 changes: 25 additions & 0 deletions basics/json_encode_decode/game.project
Original file line number Diff line number Diff line change
@@ -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

4 changes: 4 additions & 0 deletions basics/json_encode_decode/input/game.input_binding
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mouse_trigger {
input: MOUSE_BUTTON_1
action: "touch"
}