drakth Posted May 20, 2014 Share Posted May 20, 2014 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. Quote Link to comment Share on other sites More sharing options...
Haydenmango Posted May 20, 2014 Share Posted May 20, 2014 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. Quote Check out my game Rogue Snowboarding- https://haydenmango.itch.io/roguesnowboarding Link to comment Share on other sites More sharing options...
Rick Posted May 20, 2014 Share Posted May 20, 2014 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. 1 Quote Link to comment Share on other sites More sharing options...
Haydenmango Posted May 20, 2014 Share Posted May 20, 2014 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? Quote Check out my game Rogue Snowboarding- https://haydenmango.itch.io/roguesnowboarding Link to comment Share on other sites More sharing options...
Rick Posted May 20, 2014 Share Posted May 20, 2014 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()). Quote Link to comment Share on other sites More sharing options...
Haydenmango Posted May 20, 2014 Share Posted May 20, 2014 Awesome, thanks for clearing that up for me Rick! Quote Check out my game Rogue Snowboarding- https://haydenmango.itch.io/roguesnowboarding Link to comment Share on other sites More sharing options...
drakth Posted May 20, 2014 Author Share Posted May 20, 2014 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 :-/ Quote Link to comment Share on other sites More sharing options...
Rick Posted May 20, 2014 Share Posted May 20, 2014 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? Quote Link to comment Share on other sites More sharing options...
drakth Posted May 20, 2014 Author Share Posted May 20, 2014 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. Quote Link to comment Share on other sites More sharing options...
Rick Posted May 20, 2014 Share Posted May 20, 2014 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? Quote Link to comment Share on other sites More sharing options...
drakth Posted May 21, 2014 Author Share Posted May 21, 2014 I tried commenting the create world line but still crashes. I created a new test project, that also seems to crash, i'll attach it in case you want to look at it. It seems to crash on the self.world:clear() but im not sure. http://www.bitperdido.com/TestLevelChange.zip Thanks. Quote Link to comment Share on other sites More sharing options...
Rick Posted May 21, 2014 Share Posted May 21, 2014 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. Quote Link to comment Share on other sites More sharing options...
drakth Posted May 21, 2014 Author Share Posted May 21, 2014 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? Quote Link to comment Share on other sites More sharing options...
Rick Posted May 21, 2014 Share Posted May 21, 2014 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.