Jump to content

Change Map / Replace Old Map With New Map


gamecreator
 Share

Recommended Posts

Very simple question: how do you properly change a map with C/C++.  I tried doing it with the following code:

if(window->KeyHit(Key::NumPad1)) Map::Load("Maps/map1.map");
if(window->KeyHit(Key::NumPad2)) Map::Load("Maps/map2.map");

but it keeps loading the maps on top of each other, not replacing/removing the previous one, getting brighter and brighter as each map has a light.  Map::Load returns true or false, not a handle, so I don't even know how to try to release it.  So how is this done?

Link to comment
Share on other sites

I would like to keep the players/enemies (and other shared preloads) between maps and not clear them.  Is there a way to do that?

There was a function Josh showed me once that I think held/froze assets but I can't find that thread.  Edit: found it: https://www.leadwerks.com/community/topic/15212-improve-load-times-when-changing-maps/

Link to comment
Share on other sites

On ‎1‎/‎26‎/‎2018 at 1:50 PM, gamecreator said:

I would like to keep the players/enemies (and other shared preloads) between maps and not clear them.  Is there a way to do that?

There was a function Josh showed me once that I think held/froze assets but I can't find that thread.  Edit: found it: https://www.leadwerks.com/community/topic/15212-improve-load-times-when-changing-maps/

If you have items that are created in code before the map is loaded, you can call AddRef() on them before World::Clear() and then call Release() on them after that. This will temporarily increase the reference count and then set it back to 1.

In Leadwerks 5, simply holding onto the variable will keep it from being deleted.

  • Like 1

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

On 26.1.2018 at 10:06 AM, AggrorJorn said:

You need to clear the current world first. https://www.leadwerks.com/learn?page=API-Reference_Object_World_Clear

This is somewhat offtopic, but regarding the example provided on the API reference using Clear(), where it is actually used?

I've noticed that sometimes when you look something from API reference, the example is not actually helping at all on how to use specific function etc.

Link to comment
Share on other sites

4 minutes ago, Core said:

This is somewhat offtopic, but regarding the example provided on the API reference using Clear(), where it is actually used?

You are correct. The sample lacks the actual demonstration of the function.

Something like this would be needed. (haven't tested it)

--Create a window
window = Window:Create()
context = Context:Create(window)
world = World:Create()
camera = Camera:Create()
camera:Move(0,0,-3)
local light = DirectionalLight:Create()
light:SetRotation(35,35,0)
       
model = Model:Box() 
model:SetColor(0.0,0.0,1.0) 
while true do        
	if window:Closed() or window:KeyHit(Key.Escape) then return false end

	if model ~= nil then
		model:Turn(0,Time:GetSpeed(),0)
	end
	
	--Clears the current world
	if window:KeyHit(Key.Space) then
		world:Clear()
	end

	Time:Update()
	world:Update()
	world:Render()
	context:Sync(false)
end

 

 

  • Like 1
Link to comment
Share on other sites

  • 1 year later...
On 1/27/2018 at 9:41 AM, Josh said:

If you have items that are created in code before the map is loaded, you can call AddRef() on them before World::Clear() and then call Release() on them after that. This will temporarily increase the reference count and then set it back to 1.

How is this supposed to look?  I have the following code and it's not working.

#include "Leadwerks.h"

using namespace Leadwerks;

int main(int argc, const char *argv[])
{
	Leadwerks::Window* window = Leadwerks::Window::Create();
	Context* context = Context::Create(window);
	World* world = World::Create();

	Model* model = Model::Box();
	model->SetColor(0.0, 0.0, 1.0);

	Camera* camera = Camera::Create();
	camera->Move(0, 0, -3);

	Map::Load("Maps/map1.map");

	while (true)
	{
		if(window->Closed() || window->KeyDown(Key::Escape)) break;

		//  Clear old map, load new one
		if(window->KeyHit(Key::L))
		{
			System::GCSuspend();

			model->AddRef();
			camera->AddRef();

			world->Clear();

			model->Release();
			camera->Release();

			Map::Load("Maps/map2.map");

//			camera = Camera::Create();
//			camera->Move(0, 0, -3);

			System::GCResume();
		}

		Time::Update();
		world->Update();
		world->Render();
		context->Sync();
	}
}

I've printed out the GetRefCount() at all points.  It's 1 before AddRef(), 2 after then 0 right after Clear(), even before Release().  In case I misunderstood, I also tried to move the Releases after GCResume but that didn't work either.

Link to comment
Share on other sites

I looked at the source, and World::Clear() actually deletes all ref counts, so this is incorrect.

Smart pointers are great because they fix all this confusion, yet there's none of the slowdown that full GC requires.

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