 |
forums.ps2dev.org Homebrew PS2, PSP & PS3 Development Discussions
|
| View previous topic :: View next topic |
| Author |
Message |
cgruber
Joined: 06 Sep 2005 Posts: 36
|
Posted: Sun Sep 18, 2005 8:58 pm Post subject: dofile() help |
|
|
| Code: | function LoadLevel()
num_moves=0
levelnum = tostring(level)
map=string.format("%s%s", "./levels/", levelnum)
map=string.format("%s%s", map, ".lua")
if(io.open(map, r)==nil) then --open the file first to make sure it's there
level=1 --wrap back to start
gamefield=nil
screen.waitVblankStart(60) --short delay
LoadLevel(1) --call yourself to load first level since we ran out.
io.close()
else -- yep it's there better close it
io.close()
gamefield=nil
if(level~=1) then
screen.waitVblankStart(60) --short delay
end
dofile(map)
mappos=StartupPosition()
return 1
end
end |
This is my load level code which works perfectly for the first 10 times.
My game will always crash on the 11th time dofile is called. It's not the specific 11th file because I've changed that to one that works for a previous load.
error: cannot read ./levels/11.lua
Error: No script file found.
Press start to restart |
|
| Back to top |
|
 |
LuMo
Joined: 21 Aug 2005 Posts: 410 Location: Austria
|
Posted: Sun Sep 18, 2005 9:40 pm Post subject: |
|
|
maybe a dumb question...
is 11.lua in the folder ./levels/ ? |
|
| Back to top |
|
 |
cgruber
Joined: 06 Sep 2005 Posts: 36
|
Posted: Sun Sep 18, 2005 9:56 pm Post subject: |
|
|
Yes it is..Seems the problem is with io.open()
If I remove it i can get more than 10 calls, but why can i only call io.open 10 times? |
|
| Back to top |
|
 |
LuMo
Joined: 21 Aug 2005 Posts: 410 Location: Austria
|
Posted: Sun Sep 18, 2005 10:10 pm Post subject: |
|
|
i am not that deep into lua (even getting weird results sometimes due lua-specific stuff)
pil uses this example:
| Code: | file, msg = io.open(name, "r")
if not file then print(msg) end |
and they are not even closing the io.open again...
would be sick but may it be ur missing colons?
i'll try too, cause i will load levels from files too (but my aim is bit differen... not executing lua files, importin' and parsing files)
greets
Lumo |
|
| Back to top |
|
 |
Shine
Joined: 03 Dec 2004 Posts: 728 Location: Germany
|
Posted: Sun Sep 18, 2005 10:24 pm Post subject: |
|
|
| LuMo wrote: | pil uses this example:
| Code: | file, msg = io.open(name, "r")
if not file then print(msg) end |
and they are not even closing the io.open again...
|
I think files are GC'ed and then closed automaticly, but should be better to close it. If you open too many files and they are not GC'ed, opening the next file will cause an error.
I'm using this for loading and saving the options in Snake, which are all strings, except "speed", which results in the hack in loadOptions :-)
| Code: |
options = { speed = 2, level = "desert", music = "on", sound = "on" }
function saveOptions()
file = io.open(optionsFile, "w")
if file then
for key, value in options do
file:write(key .. "=" .. value .. "\n")
end
file:close()
end
end
function loadOptions()
file = io.open(optionsFile, "r")
if file then
for line in file:lines() do
equal = string.find(line, "=")
if equal then
key = string.sub(line, 1, equal - 1)
value = string.sub(line, equal + 1)
if key == "speed" then value = tonumber(value) end
options[key] = value
end
end
file:close()
end
end
|
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|