forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

dofile() help

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Lua Player Development
View previous topic :: View next topic  
Author Message
cgruber



Joined: 06 Sep 2005
Posts: 36

PostPosted: Sun Sep 18, 2005 8:58 pm    Post subject: dofile() help Reply with quote

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
View user's profile Send private message
LuMo



Joined: 21 Aug 2005
Posts: 410
Location: Austria

PostPosted: Sun Sep 18, 2005 9:40 pm    Post subject: Reply with quote

maybe a dumb question...
is 11.lua in the folder ./levels/ ?
Back to top
View user's profile Send private message Visit poster's website
cgruber



Joined: 06 Sep 2005
Posts: 36

PostPosted: Sun Sep 18, 2005 9:56 pm    Post subject: Reply with quote

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
View user's profile Send private message
LuMo



Joined: 21 Aug 2005
Posts: 410
Location: Austria

PostPosted: Sun Sep 18, 2005 10:10 pm    Post subject: Reply with quote

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?
Code:
io.open(name, r)


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
View user's profile Send private message Visit poster's website
Shine



Joined: 03 Dec 2004
Posts: 728
Location: Germany

PostPosted: Sun Sep 18, 2005 10:24 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Lua Player Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
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