gamecreator Posted January 26, 2018 Share Posted January 26, 2018 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? Quote Link to comment Share on other sites More sharing options...
AggrorJorn Posted January 26, 2018 Share Posted January 26, 2018 You need to clear the current world first. https://www.leadwerks.com/learn?page=API-Reference_Object_World_Clear 2 Quote Link to comment Share on other sites More sharing options...
gamecreator Posted January 26, 2018 Author Share Posted January 26, 2018 Easy enough, just missed that function. Thank you Jorn. Quote Link to comment Share on other sites More sharing options...
gamecreator Posted January 26, 2018 Author Share Posted January 26, 2018 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/ Quote Link to comment Share on other sites More sharing options...
Josh Posted January 27, 2018 Share Posted January 27, 2018 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. 1 Quote 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 More sharing options...
gamecreator Posted January 27, 2018 Author Share Posted January 27, 2018 Good to have a solution, if this works. Thanks Josh. Though a clean function like world->ClearMap() would be far more intuitive/elegant. Quote Link to comment Share on other sites More sharing options...
Core Posted January 29, 2018 Share Posted January 29, 2018 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. Quote Link to comment Share on other sites More sharing options...
AggrorJorn Posted January 29, 2018 Share Posted January 29, 2018 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 1 Quote Link to comment Share on other sites More sharing options...
gamecreator Posted August 6, 2019 Author Share Posted August 6, 2019 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. Quote Link to comment Share on other sites More sharing options...
Josh Posted August 7, 2019 Share Posted August 7, 2019 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. Quote 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 More sharing options...
gamecreator Posted August 7, 2019 Author Share Posted August 7, 2019 Dang. Thanks for looking. So I guess the workaround would be to put everything you want to carry over between maps (like players), into all maps, hidden somewhere. Thankfully it looks like it doesn't delete loaded textures and fonts so I can keep them for my custom GUI. 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.