Page 2 of 2
Re: A few minor questions
Posted: Mon Apr 18, 2011 11:12 pm
by Taehl
miko wrote:I think the best to start with is to do it the right way from the beginning.
The "right" solution is highly dependent to the project in question and is likely going to be more complex than a beginner will immediately grasp. One could hand them a perfect solution, but that doesn't help if they don't understand it.
miko wrote:And If I am not mistaken, you would need to unset package.loaded for every require()'d file, as Lua itself caches every require()'d file. So if part of your game setting was in a required file, you would miss it by only using loadfile('main.lua').
Why would you need to bother? Unless you're rewriting your included stuff (in which case, why include in the first place?), why do you need to reload? Just leave it alone - Lua will see you already have it loaded, and will essentially skip re-loading it for you. Weren't you just complaining about making game-resets taking longer than absolutely necessary?
miko wrote:Code: Select all
json=require 'dkjson'
--save:
serialized=json.encode(GameState)
io.open('savedgame.json', 'w'):write(serialized)
--load
gameInit(json.deocde(io.open('savedgame.json', 'r'):read('*a')))
You shouldn't use the io library in Love. Use
love.filesystem instead. Also, gf123, an alternate serializer you may be interested in is
TSerial (it's smaller and simpler than JSON).
Re: A few minor questions
Posted: Mon Apr 18, 2011 11:54 pm
by miko
Taehl wrote:
miko wrote:And If I am not mistaken, you would need to unset package.loaded for every require()'d file, as Lua itself caches every require()'d file. So if part of your game setting was in a required file, you would miss it by only using loadfile('main.lua').
Why would you need to bother? Unless you're rewriting your included stuff (in which case, why include in the first place?), why do you need to reload? Just leave it alone - Lua will see you already have it loaded, and will essentially skip re-loading it for you. Weren't you just complaining about making game-resets taking longer than absolutely necessary?
Your original advise was:
Taehl wrote:
2) You can reset the game very easily by reloading main.lua and calling love.load, like this:
Code: Select all
love.filesystem.load("main.lua")() love.load()
What I am saying is that if you use required files, and set some initial values in those files (OK, bad design, but you could do this, more likely if you are a beginner), then your solution will not work. See attached example and press "r" for your version of resetting the game, or any other key to change the values. You will see that the game does not get reset. And this is the reason why I wrote about setting a GameState table as a better solution.
Re: A few minor questions
Posted: Tue Apr 19, 2011 1:11 am
by Taehl
I think it's a bad idea to encourage new users to program badly.
Re: A few minor questions
Posted: Tue Apr 19, 2011 5:35 am
by Robin
Guys!
*cue "thread derailed" image macro*