Jump to content

Video Tutorial: Switching levels


Rick
 Share

Recommended Posts

hey rick.

 

in the same time I wrote also a mapload script. The Mapload works nice when I call the mapload from the App:Start() or the App:Loop() my Idea was to use another lua script to Push that mapload but it will not work when mycollisiontrigger will push the mapload. There is a error msg like:

 

attempt to index global 'Script' (a nil value)

 

 

this is my app.lua

 

--This function will be called once when the program starts
function App:Start()
--screensettings
self.screenWidth = 600    --x
self.screenHeight= 400	 --y
self.contextMsaa = 0    --msaa
--mapsettings
self.startMap = "level_01.map" -- map which will be loaded first
self.currentMap = ""    -- currentmap which is loaded

--Create a window
self.window=Window:Create("MyTestArea", 0, 0, App:getScreenWidth(), App:getScreenHeight(), Window.Titlebar)
self.window:HideMouse()

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

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

-- load startmap
App:LoadMap(self.startMap)
return true
end
--This is our main program loop and will be called continuously until the program ends
function App:Loop()

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

--mapchangetest
if self.window:KeyDown(Key.H) then
App:LoadMap("level_02.map")
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:DrawText("Map: "..App:getCurrentMap(),2,22)
 self.context:DrawText(App:getScreenWidth() .." x " .. App:getScreenHeight() .. " - msaa: " ..App:getContextMsaa(),2,42)
 self.context:DrawText("Memory usage: "..System:GetMemoryUsage(),2,62)
 self.context:SetColor(1,1,1,1)
 self.context:DrawStats(2,102)
 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
--this function will load a mapfile located in the Maps folder
function App:LoadMap(_mapfile)
--loading msg
self.context:SetBlendMode(Blend.Alpha)
self.context:SetColor(1,1,1,1)
self.context:DrawText("Loading...",App:getScreenWidth()/2, App:getScreenHeight()/2)
self.context:Sync(false)

self.world:Release()
self.world=World:Create()
--Load map
local mapfile = System:GetProperty("map","Maps/".. _mapfile)
if Map:Load(mapfile)==false then
 Debug:Error("cant find or load mapfile: Maps/".. _mapfile)
 return false
else
 System:Print(_mapfile .. " successful loaded!")

 App:setCurrentMap(_mapfile)
 return true
end
end

--Getter/Setter functions
function App:getScreenWidth()
return self.screenWidth
end
function App:getScreenHeight()
return self.screenHeight
end
function App:getCurrentMap()
return self.currentMap
end
function App:setCurrentMap(_mapfile)
 self.currentMap = _mapfile
end
function App:getContextMsaa()
return self.contextMsaa
end

 

 

 

this is my trigger

 

--[[
This script will make any entity act as a collision trigger. It works best when you set the
entity's collision type to "Trigger". This will continuously detect collisions without causing
any physical reaction.
]]--
Script.nextMap = "" --path "next Map" "Map (*.map):map|Maps"
function Script:Start()
self.enabled=true
end
function Script:Collision(entity, position, normal, speed)
if self.enabled then
self.component:CallOutputs("Collision")
end
System:Print("MAPLOAD PUSH")
App:LoadMap(Script.nextMap)
end
function Script:Enable()--in
if self.enabled==false then
self.enabled=true
self:CallOutputs("Enable")
end
end
function Script:Disable()--in
if self.enabled then
self.enabled=false
self:CallOutputs("Disable")
end
end

 

is this not possible cause I call it in the level? I dont like that "always checking to change level option"

It doesn´t work... why? mhmmm It works... why?

Link to comment
Share on other sites

The error you are getting right now is because you have

 

App:LoadMap(Script.nextMap) vs App:LoadMap(self.nextMap)

 

When you define script parameters you use Script., but when you use them in the script you use self.

 

However, once you solve that I think you'll get a new error. Trust me I'd prefer not to check for a new map every frame, but it's really nothing and not a big deal, but you can't (since I last checked) destroy the world while in an entity script. The reason being that entity is inside the world itself. That script is running from within that world where App.lua runs outside of the world. So once you fix the error I pointed out above, you should get a new one (might crash you game too).

  • Upvote 1
Link to comment
Share on other sites

really cant wait for c++ and thread support and writing a nice while loading animation smile.png

 

is it possible to change the map and control the point of Loading/Changing?

 

like:

 

-pushing prepare load by a trigger -> map will be loaded into RAM (but not changed only prepared)

 

-pushing changemap by a trigger ->map is already in RAM so it will do a fast level switch

 

 

when I use threads for it the player dont have to wait that long because there is a prepare function that only load the map triggered by a collision (some area near the end of the level) and when I reach the end of the level it gives an OK to change the map.

 

 

in the LE3 API the Map:Load function will do both. loading and changing.

is there a way or maybe some hope to split this?

 

I would like to code a changelevel function that do everything in the background because I dont like normal loadingscreens^^

It doesn´t work... why? mhmmm It works... why?

Link to comment
Share on other sites

Hi, I'm new here and pre-ordered 3.1. Thanks for this tutorial! Just checking the more I can before I receive it.

 

Is there a way to keep and update (like a savegame) "persistent" data (Quests info, Health info or any other data that need to be checked all over the game) when you load the new level?

 

If you had NPC on theses map, they would re-spawn since we did not kept their last state before getting the new level...

Link to comment
Share on other sites

So since App lives beyond maps, you can make variables as part of App and they will be available in all scripts and live between map loads. I would look into how the AnimationManager looks (it's basically a way to make a "class" in Lua). Make one of these Lua "classes" to store the save information you need. Then in App:Start() create an instance of said object and access said object wher ever you want in other scripts.

 

example

import "SaveData.lua" -- this would be what you have to create. look at ANimationManager.lua for a way to make these "classes"

function App:Start()
   self.SaveData = SaveData:Create()
end

 

Then in entity scripts you can access it via:

 

App.SaveData.PlayerHealth = self.playerHealth
Link to comment
Share on other sites

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.

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.

 Share

×
×
  • Create New...