A few minor questions

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: A few minor questions

Post 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).
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
miko
Party member
Posts: 410
Joined: Fri Nov 26, 2010 2:25 pm
Location: PL

Re: A few minor questions

Post 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.
Attachments
BadReset.love
(1.05 KiB) Downloaded 55 times
My lovely code lives at GitHub: http://github.com/miko/Love2d-samples
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: A few minor questions

Post by Taehl »

I think it's a bad idea to encourage new users to program badly.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: A few minor questions

Post by Robin »

Guys!

*cue "thread derailed" image macro*
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 4 guests