Skip to content

Commit 4cd0b58

Browse files
authored
load json as resource example (#126)
1 parent b4dbf3e commit 4cd0b58

File tree

10 files changed

+174
-0
lines changed

10 files changed

+174
-0
lines changed

file/json_load/.gitattributes

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Defold Protocol Buffer Text Files (https://github.com/github/linguist/issues/5091)
2+
*.animationset linguist-language=JSON5
3+
*.atlas linguist-language=JSON5
4+
*.camera linguist-language=JSON5
5+
*.collection linguist-language=JSON5
6+
*.collectionfactory linguist-language=JSON5
7+
*.collectionproxy linguist-language=JSON5
8+
*.collisionobject linguist-language=JSON5
9+
*.cubemap linguist-language=JSON5
10+
*.display_profiles linguist-language=JSON5
11+
*.factory linguist-language=JSON5
12+
*.font linguist-language=JSON5
13+
*.gamepads linguist-language=JSON5
14+
*.go linguist-language=JSON5
15+
*.gui linguist-language=JSON5
16+
*.input_binding linguist-language=JSON5
17+
*.label linguist-language=JSON5
18+
*.material linguist-language=JSON5
19+
*.mesh linguist-language=JSON5
20+
*.model linguist-language=JSON5
21+
*.particlefx linguist-language=JSON5
22+
*.render linguist-language=JSON5
23+
*.sound linguist-language=JSON5
24+
*.sprite linguist-language=JSON5
25+
*.spinemodel linguist-language=JSON5
26+
*.spinescene linguist-language=JSON5
27+
*.texture_profiles linguist-language=JSON5
28+
*.tilemap linguist-language=JSON5
29+
*.tilesource linguist-language=JSON5
30+
31+
# Defold JSON Files
32+
*.buffer linguist-language=JSON
33+
34+
# Defold GLSL Shaders
35+
*.fp linguist-language=GLSL
36+
*.vp linguist-language=GLSL
37+
38+
# Defold Lua Files
39+
*.editor_script linguist-language=Lua
40+
*.render_script linguist-language=Lua
41+
*.script linguist-language=Lua
42+
*.gui_script linguist-language=Lua

file/json_load/.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/.editor_settings
2+
/.internal
3+
/build
4+
.externalToolBuilders
5+
.DS_Store
6+
Thumbs.db
7+
.lock-wscript
8+
*.pyc
9+
.project
10+
.cproject
11+
builtins

file/json_load/example.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
tags: file
3+
title: Load JSON data
4+
brief: This example shows how to load json data using sys.load_resource().
5+
author: jerakin
6+
scripts: json_load.script
7+
---
8+
9+
The example will load a json file. This can be useful for something like level data.
10+
11+
Before we can load a resource we need to tell Defold that we have custom resources.
12+
We do this by changing the "custom resources" entry within our game.project file.
13+
14+
![set-custom-resource](set_custom_resource.png)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: "main"
2+
scale_along_z: 0
3+
embedded_instances {
4+
id: "go"
5+
data: "components {\n"
6+
" id: \"json_load\"\n"
7+
" component: \"/example/json_load.script\"\n"
8+
"}\n"
9+
"embedded_components {\n"
10+
" id: \"title\"\n"
11+
" type: \"label\"\n"
12+
" data: \"size {\\n"
13+
" x: 128.0\\n"
14+
" y: 32.0\\n"
15+
"}\\n"
16+
"text: \\\"No level loaded\\\"\\n"
17+
"font: \\\"/builtins/fonts/default.font\\\"\\n"
18+
"material: \\\"/builtins/fonts/label-df.material\\\"\\n"
19+
"\"\n"
20+
" position {\n"
21+
" x: 360.0\n"
22+
" y: 360.0\n"
23+
" }\n"
24+
"}\n"
25+
"embedded_components {\n"
26+
" id: \"Description\"\n"
27+
" type: \"label\"\n"
28+
" data: \"size {\\n"
29+
" x: 128.0\\n"
30+
" y: 32.0\\n"
31+
"}\\n"
32+
"text: \\\"Press \\\\\\\"1\\\\\\\" to load level_001 and \\\\\\\"2\\\\\\\" to to load level_002.\\\"\\n"
33+
"font: \\\"/builtins/fonts/default.font\\\"\\n"
34+
"material: \\\"/builtins/fonts/label-df.material\\\"\\n"
35+
"\"\n"
36+
" position {\n"
37+
" x: 360.0\n"
38+
" y: 50.0\n"
39+
" }\n"
40+
"}\n"
41+
""
42+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
local function load_level(level_name)
2+
local level_path = "/levels/" .. level_name .. ".json" -- <1>
3+
local data = sys.load_resource(level_path) -- <2>
4+
local json_data = json.decode(data) -- <3>
5+
label.set_text("#title", json_data.title) -- <4>
6+
end
7+
8+
function init(self)
9+
msg.post(".", "acquire_input_focus")
10+
end
11+
12+
13+
function on_input(self, action_id, action)
14+
if action_id == hash("key_1") then
15+
if action.released then
16+
load_level("level_001")
17+
end
18+
elseif action_id == hash("key_2") then
19+
if action.released then
20+
load_level("level_002")
21+
end
22+
end
23+
end
24+
25+
--[[
26+
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.
27+
2. Load the resource, this will return a string.
28+
3. Use the json.decode to make our string into a lua table.
29+
4. Use the loaded level data in whatever way we want.
30+
--]]

file/json_load/game.project

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[bootstrap]
2+
main_collection = /example/json_load.collectionc
3+
4+
[script]
5+
shared_state = 1
6+
7+
[display]
8+
width = 720
9+
height = 720
10+
high_dpi = 1
11+
12+
[android]
13+
input_method = HiddenInputField
14+
15+
[html5]
16+
scale_mode = stretch
17+
18+
[project]
19+
title = json_load
20+
custom_resources = levels
21+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
key_trigger {
2+
input: KEY_1
3+
action: "key_1"
4+
}
5+
key_trigger {
6+
input: KEY_2
7+
action: "key_2"
8+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"title": "Starting Area"
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"title": "Mystic Glades"
3+
}
34.6 KB
Loading

0 commit comments

Comments
 (0)