Jump to content
drakth

Problem changing level

Recommended Posts

Hi everyone,

 

I recently updated a test project on the Project Manager (update button) and suddently a lua script stopped working. The script is supposed to load another level when the player collides with an object.

 

Here is the script:

 

Script.entered = false
Script.exited = false
Script.hadCollision = false;
Script.Map = "" --Path
function Script:UpdatePhysics()
if self.entered then
if self.hadCollision == false then
 if self.exited == false then
 self.exited = true
 self.component:CallOutputs("TriggerExit")
 self.entered = false
 end
end
end
self.hadCollision = false
end
function Script:Collision(entity, position, normal, speed)
self.hadCollision = true
if self.entered == false then
self.component:CallOutputs("TriggerEnter")
App:SwitchLevel(self.Map)
self.entered = true
self.exited = false
end
end

 

The script seems to fail at the line:

 

App:SwitchLevel(self.Map)

 

With the error Attempt to call method 'SwitchLevel' (a nil value)

 

Any ideas?

 

Thanks.

Share this post


Link to post

I think it may have to do with Calling:Clear() on your current World and creating a new one for your new map. Check out this code below.

 

if self.mapnum==1 then
self.mapnum=0
self.currentmapnum=1
self.world:Clear()
Map:Load("Maps/Fight Fire with Fire.map")
elseif self.mapnum==2 then
self.mapnum=0
self.currentmapnum=2
self.world:Clear()
Map:Load("Maps/Save the Trees 2.map")
end

 

It's kind of hard to tell with the example you gave though. Maybe if you also showed the App.lua I could get more insight as to what is going on.

Share this post


Link to post

I don't think you want to release the world anymore. Calling :Clear() on it I believe is the way Josh recommended doing it.

 

 

The reason your script is failing is because when you update Leadwerks it overwrites you App.lua file to it's original state, which means that function doesn't exist anymore. Not all is lost, LE should have made a backup of your App.lua that you had before. Look in the directory App.lua is in and you should see a backup of it. Rename that back and all should be good. Yes, it sucks that LE does this.

Share this post


Link to post

Thanks Rick that actually makes a lot more sense then using Release().

A bit off topic but when trying to get a Script to run do you have to call App.script:Function() or does App:Function() just work?

Share this post


Link to post

When it comes to App, you can just do App:Function(). If you have an instance of an entity (like in the Collision() function, then you have to use entity.script:Function()).

Share this post


Link to post

Thanks Rick,

 

That indeed solved the loading level issue (replacing the app.lua).

 

However now when the new level loads the game just crashes with a "Stopped working" error :-/

Share this post


Link to post

Can you post your App.lua file for us to see.

 

Also, does it look like it free's the old level and loads the new level? What do you have in the new level?

Share this post


Link to post

This is the content of app.lua:

 

--This function will be called once when the program starts
function App:Start()

--Set the application title
self.title="MyGame"

--Create a window
self.window=Window:Create(self.title)
self.window:HideMouse()

--Create the graphics context
self.context=Context:Create(self.window,0)
if self.context==nil then return false end

--Create a world
self.world=World:Create()

--Load a map
--local mapfile = System:GetProperty("map","Maps/start.map")
self.mapFile = "Maps/start.map"
if Map:Load(self.mapFile)==false then return false end
self.mapFile = ""

return true
end
function App:SwitchLevel(name)
self.mapFile = name
end
function App:ShouldSwitchLevel()
if self.mapFile ~= "" then
 --self.world:Release()
 self.world:Clear()

 self.world = World:Create()
 Map:Load(self.mapFile)
 self.mapFile = ""
end
end

--This is our main program loop and will be called continuously until the program ends
function App:Loop()
self:ShouldSwitchLevel()

--If window has been closed, end the program
if self.window:Closed() or self.window:KeyDown(Key.Escape) then return false end

--Update the app timing
Time:Update()

--Update the world
self.world:Update()

--Render the world
self.world:Render()

--Render statistics
self.context:SetBlendMode(Blend.Alpha)
if DEBUG then
 self.context:SetColor(1,0,0,1)
 self.context:DrawText("Debug Mode",2,2)
 self.context:SetColor(1,1,1,1)
 self.context:DrawStats(2,22)
 self.context:SetBlendMode(Blend.Solid)
else
 self.context:SetColor(1,1,1,1)
 self.context:DrawText("FPS: "..Math:Round(Time:UPS()),2,2)
end

--Refresh the screen
self.context:Sync(false)

--Returning true tells the main program to keep looping
return true
end

 

I tried loading the 2nd level instead of the 1st one, and it loads fine, it seems to crash when called from the script.

 

The second level is just a platform with a another object where you can collide and will take you again to the first level.

 

Thanks.

Share this post


Link to post

Comment out the creation of the new world in ShouldSwitch() and try that. You clear the world which means you clear all the entities in it so no need to recreate it as it'll be blank after clearing.

 

Can you specifically where it crashes?

Share this post


Link to post

So inside FPSPlayer.lua in the Script:Release() function comment out the releasing of the listener and flashlight and it should work for you. All I did was place System:Print() functions between all those releases and ran to get the error and then checked the output tab in the script editor to see what printed. This told me how far it was getting. Commenting those out allowed the world to fully be cleared and load the next world. However there might be a leak and issue with those.

 

I'm not 100% sure at this time why the listener and flashlight fail to release.

Share this post


Link to post

Thanks Rick,

 

That fixed the issue. However, i think there is also not working right, with the default weapon prefab. Cause on other project i have, fixing the FPSPlayer script, didnt fix the crash, however as soon as i removed the autopistol prefab it worked fine.

 

Also thanks for the debugging tip.

 

Should i report this on the bug forums?

Share this post


Link to post

I remember an issue with the weapon also. It's just how it was coded changing levels wasn't considered. If I remember correctly there is a change that needs to be done to that script to avoid this error. A bug report might not do much, but keep debugging. I'll see if I can remember what I did to fix that error.

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...