Skip to content
Merged
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 file/json_load/.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 file/json_load/.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
14 changes: 14 additions & 0 deletions file/json_load/example.md
Original file line number Diff line number Diff line change
@@ -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)
42 changes: 42 additions & 0 deletions file/json_load/example/json_load.collection
Original file line number Diff line number Diff line change
@@ -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"
""
}
30 changes: 30 additions & 0 deletions file/json_load/example/json_load.script
Original file line number Diff line number Diff line change
@@ -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.
--]]
21 changes: 21 additions & 0 deletions file/json_load/game.project
Original file line number Diff line number Diff line change
@@ -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

8 changes: 8 additions & 0 deletions file/json_load/input/game.input_binding
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
key_trigger {
input: KEY_1
action: "key_1"
}
key_trigger {
input: KEY_2
action: "key_2"
}
3 changes: 3 additions & 0 deletions file/json_load/levels/level_001.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"title": "Starting Area"
}
3 changes: 3 additions & 0 deletions file/json_load/levels/level_002.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"title": "Mystic Glades"
}
Binary file added file/json_load/set_custom_resource.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.