Jump to content

Improve load times when changing maps


Josh
 Share

Recommended Posts

If you add a call to GCSuspend before clearing the world, and call GCResume after, the engine won't delete and reload all the resources that are shared between maps. This also helps if you are reloading a map to restart the level.

--Handle map change
if changemapname~=nil then

 --Pause the clock
 Time:Pause()

 --Pause garbage collection
 System:GCSuspend()  

 --Clear all entities
 world:Clear()

 --Load the next map
 if Map:Load("Maps/"..changemapname..".map")==false then return end

 --Resume garbage collection
 System:GCResume()

 --Resume the clock
 Time:Resume()

 changemapname = nil
end 

  • Upvote 10

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

That sounds like very useful functionality.

 

Are there any downsides to using this function? Perhaps addding this as a boolean parameter to the load Map function for clean coding. A lot of users will forget this exists if it not a default behaviour.

As the code was written before, the engine would clear the world, deleting all textures and models, and then reload whatever was needed.

 

The GCSuspend command makes it so when an object's reference count hits zero, it is added to a list of deleted objects, but its destructor isn't called yet. The destructors will all be called and the list cleared when the GCResume call is made.

 

Another way this could be done is to create a new world and load the map into that, then delete the old one:

 --Handle map change
if changemapname~=nil then

--Pause the clock
Time:Pause()

local prevworld = world
world = World:Create()

--Load the next map
if Map:Load("Maps/"..changemapname..".map")==false then return end

prevworld:Release()

--Resume the clock
Time:Resume()

changemapname = nil
end

  • Upvote 3

My job is to make tools you love, with the features you want, and performance you can't live without.

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...