Skip to content

Commit

Permalink
Preserve UIDs of moved objects in GameObjectManager
Browse files Browse the repository at this point in the history
Continuation of 3b2485d. This ensures that a UID which references a now-moved object from a `GameObjectManager` will be valid again if the object is moved back to said `GameObjectManager`.
  • Loading branch information
Vankata453 committed Dec 1, 2024
1 parent dfbd3b9 commit e3deded
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/supertux/game_object_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ GameObjectManager::GameObjectManager(bool undo_tracking) :
m_last_saved_change(),
m_gameobjects(),
m_gameobjects_new(),
m_moved_object_uids(),
m_solid_tilemaps(),
m_all_tilemaps(),
m_objects_by_name(),
Expand Down Expand Up @@ -136,12 +137,20 @@ GameObjectManager::add_object(std::unique_ptr<GameObject> object)

if (!object->get_uid()) // Undo/redo requires re-creating objects with the same UID.
{
object->set_uid(m_uid_generator.next());
if (m_moved_object_uids.find(object.get()) == m_moved_object_uids.end())
{
object->set_uid(m_uid_generator.next());

// No object UID would indicate the object is not a result of undo/redo.
// Any newly placed object in the editor should be on its latest version.
if (m_initialized && Editor::is_active())
object->update_version();
// No object UID would indicate the object is not a result of undo/redo.
// Any newly placed object in the editor should be on its latest version.
if (m_initialized && Editor::is_active())
object->update_version();
}
else
{
object->set_uid(m_moved_object_uids[object.get()]);
m_moved_object_uids.erase(object.get());
}
}

// Make sure the object isn't already in the list.
Expand Down Expand Up @@ -337,11 +346,14 @@ GameObjectManager::move_object(const UID& uid, GameObjectManager& other)
log_warning << "Couldn't move object: Object with UID " << uid << " not found." << std::endl;
return;
}
auto& obj = *it;

m_moved_object_uids[obj.get()] = uid;

this_before_object_remove(**it);
before_object_remove(**it);
this_before_object_remove(*obj);
before_object_remove(*obj);

other.add_object(std::move(*it));
other.add_object(std::move(obj));
m_gameobjects.erase(it);

other.flush_game_objects();
Expand Down
4 changes: 4 additions & 0 deletions src/supertux/game_object_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,10 @@ class GameObjectManager : public ExposableClass
/** container for newly created objects, they'll be added in flush_game_objects() */
std::vector<std::unique_ptr<GameObject>> m_gameobjects_new;

/** Former UIDs of all objects moved to another GameObjectManager.
Will be assigned back to any of the objects if they are moved back here. */
std::unordered_map<GameObject*, UID> m_moved_object_uids;

/** Fast access to solid tilemaps */
std::vector<TileMap*> m_solid_tilemaps;

Expand Down

0 comments on commit e3deded

Please sign in to comment.