diff --git a/include/engine/Cable.hpp b/include/engine/Cable.hpp index 43ccaded..1fe78a59 100644 --- a/include/engine/Cable.hpp +++ b/include/engine/Cable.hpp @@ -8,7 +8,11 @@ namespace engine { struct Cable { - int id = -1; + /** Unique ID for referring to the cable in the engine. + Between 0 and 2^53 since this is serialized with JSON. + Assigned when added to the engine. + */ + int64_t id = -1; Module* inputModule = NULL; int inputId; Module* outputModule = NULL; diff --git a/include/engine/Module.hpp b/include/engine/Module.hpp index e1eff9ae..26952c8a 100644 --- a/include/engine/Module.hpp +++ b/include/engine/Module.hpp @@ -31,9 +31,10 @@ struct Module { plugin::Model* model = NULL; /** Unique ID for referring to the module in the engine. + Between 0 and 2^53 since this is serialized with JSON. Assigned when added to the engine. */ - int id = -1; + int64_t id = -1; /** Arrays of components. Initialized with config(). diff --git a/src/engine/Engine.cpp b/src/engine/Engine.cpp index 66331756..6d368448 100644 --- a/src/engine/Engine.cpp +++ b/src/engine/Engine.cpp @@ -202,10 +202,6 @@ struct Engine::Internal { int stepFrames = 0; Module* primaryModule = NULL; - // For generating new IDs for added objects - int nextModuleId = 0; - int nextCableId = 0; - // Parameter smoothing Module* smoothModule = NULL; int smoothParamId = 0; @@ -547,9 +543,6 @@ void Engine::clear() { removeModule(module); delete module; } - // Reset engine state - internal->nextModuleId = 0; - internal->nextCableId = 0; } @@ -693,20 +686,10 @@ void Engine::addModule(Module* module) { // Check that the module is not already added auto it = std::find(internal->modules.begin(), internal->modules.end(), module); assert(it == internal->modules.end()); - // Set ID - if (module->id < 0) { - // Automatically assign ID - module->id = internal->nextModuleId++; - } - else { - // Manual ID - // Check that the ID is not already taken - auto it = internal->modulesCache.find(module->id); - assert(it == internal->modulesCache.end()); - // If ID is higher than engine's next assigned ID, enlarge it. - if (module->id >= internal->nextModuleId) { - internal->nextModuleId = module->id + 1; - } + // Set ID if unset or collides with an existing ID + while (module->id < 0 || internal->modulesCache.find(module->id) != internal->modulesCache.end()) { + // Randomly generate ID + module->id = random::u64() % (1ul << 53); } // Add module internal->modules.push_back(module); @@ -846,20 +829,10 @@ void Engine::addCable(Cable* cable) { if (cable2->outputModule == cable->outputModule && cable2->outputId == cable->outputId) outputWasConnected = true; } - // Set ID - if (cable->id < 0) { - // Automatically assign ID - cable->id = internal->nextCableId++; - } - else { - // Manual ID - // Check that the ID is not already taken - for (Cable* w : internal->cables) { - assert(cable->id != w->id); - } - if (cable->id >= internal->nextCableId) { - internal->nextCableId = cable->id + 1; - } + // Set ID if unset or collides with an existing ID + while (cable->id < 0 || internal->cablesCache.find(cable->id) != internal->cablesCache.end()) { + // Randomly generate ID + cable->id = random::u64() % (1ul << 53); } // Add the cable internal->cables.push_back(cable);