Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

better place to load a json table?
Started by dodi_games Dec 29 2018 03:44 PM

4 replies to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

dodi_games

[GLOBAL: userInfoPane.html]
dodi_games
  • Contributor

  • 347 posts
  • Corona SDK

which would be the best place to load a json

table

with information that will be used in the scene create and scene show?


Above the scene create?

-- -----------------------------------------------------------------------------------
-- Code outside of the scene event functions below will only be executed ONCE unless
-- the scene is removed entirely (not recycled) via "composer.removeScene()"
-- -----------------------------------------------------------------------------------

--save/load game settings
local gameSettings = gameSettings = (progress.loadTable("gameSettings.json") or {})

in the scene create?

local gameSettings

-- create()
function scene:create( event )
 local sceneGroup = self.view
 -- Code here runs when the scene is first created but has not yet appeared on screen
  --load JSON table to access data
  gameSettings = (progress.loadTable("gameSettings.json") or {})

I was initially making the call in the scene show that was where I needed it. Once I was requesting to use the table in different parts of the scene, I moved the call to the scene create, I find myself in doubt, I do not get the console errors but I am trying to access a data in the scene show will phase, and it seems not read the table. I do not have much experience with json and I do not know if it has any negative impact call it above the scene create. I understand that it is a silly question for expert users, but for the first time I feel that I have the RAW of what I can call a game and I want to have a good scope.

Thanks in advance
DoDi



[TOPIC: post.html]
#2

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,492 posts
  • Corona SDK

Just make a variable at the top of the file and then 'fill' it later in create()

 

If you're loading a HUGE file and it gives you hiccups in display, then pre-load the scene or load the data elsewhere and pass it in to the scene.

 

A table of data named gameSettings makes more sense to me to load on start up and make a global or a module.

 

Of course, SSK solves this elegantly. 

 

You can abandon the entire problem and use persist: https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/persist/

 

For example, lets say you have a couple variables you know you want and then maybe you'll track more later.

 

You can do this:

 

main.lua

-- Provide default cases for sound_enabled and last_score
-- At this point, the table will be pre-loaded
ssk.persist.setDefault( "settings.json", "sound_enabled", true )
ssk.persist.setDefault( "settings.json", "last_score", 0 )

later in play scene:

-- get the score (maybe to use for initializing a label)
local score = ssk.persist.get( "settings.json", "last_score" )

later yet, maybe in the same scene or maybe in a module used by the scene, it doesn't matter:

-- save our score (assumes it was updated at some time)
ssk.persist.set( "settings.json", "last_score", score )

-- award the player some coins
ssk.persist.set( "settings.json", "coins", 10 )


Edited by roaminggamer, 30 December 2018 - 09:49 AM.

  • dodi_games likes this

[TOPIC: post.html]
#3

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,492 posts
  • Corona SDK

The persist library/module has these features:

  1. Caches tables for you in memory.  You don't need to maintain this.  It is hands-off and out-of-sight.
  2. Handles multiple saves in a short period of time without needing to write the 'disk' over and over.  Instead it accumulates the changes and schedules a write a short time later.  This lowers battery consumption while making it simple to update scores and frequently changed values.
  3. Allows you to provide default values for some fields/settings if you want.  Otherwise, a get for a undefined value returns nil.

  • dodi_games likes this

[TOPIC: post.html]
#4

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,372 posts
  • Enterprise

There isn't a straightforward answer to this because we don't if you're caching your scene or using removeScene() to make sure you go to a fresh scene every time. We don't know if the data you're loading is used to create things that should be created in scene:create() or if it is things that would be beneficial to have at the time you have to show the scene.

 

Composer basically has three scene loading concepts:

 

1. the scene doesn't exist (or has been fully removed and unrequired). In this case the code outside of scene:create() will run once until the module is unrequired (which composer.removeScene() does by default). scene:create() will also run. If you are using composer.removeScene() to remove the scene, then putting your data loading at the top of the scene will work.

 

2. You use scene purging to unload memory allocated by scene:create(), but the rest of the module stays in memory. In this case, the module will not be re-required, but scene:create() will be called when you to the scene again. In this case, you will probably want to load the data in scene:create() since it will be run again, but the whole module isn't loaded. You probably still want the data table to be defined outside of scene:create() so that other scene functions can access it.

 

3. You are using cached scenes, so you probably only want to load it at the beginning of the scene and code anything necessary in scene:show().

 

Rob


  • dodi_games likes this

[TOPIC: post.html]
#5

dodi_games

[GLOBAL: userInfoPane.html]
dodi_games
  • Contributor

  • 347 posts
  • Corona SDK

Thank you for your answers.

 

For now I think the best is "just make a variable at the top of the file and then 'fill' it later in create ()" as @roaminggamer says. Is very interesting what you offer in the SSK.

 

@Rob
Yes, each level is destroyed using composer.removeScene() to remove the scene.




[topic_controls]
[/topic_controls]