json_load.script 858 B

123456789101112131415161718192021222324252627282930
  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. function init(self)
  8. msg.post(".", "acquire_input_focus")
  9. end
  10. function on_input(self, action_id, action)
  11. if action_id == hash("key_1") then
  12. if action.released then
  13. load_level("level_001")
  14. end
  15. elseif action_id == hash("key_2") then
  16. if action.released then
  17. load_level("level_002")
  18. end
  19. end
  20. end
  21. --[[
  22. 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.
  23. 2. Load the resource, this will return a string.
  24. 3. Use the json.decode to make our string into a lua table.
  25. 4. Use the loaded level data in whatever way we want.
  26. --]]