mirror of
https://github.com/Keyslam-Group/Concord.git
synced 2025-09-02 12:24:11 -04:00
Entity's Keys
You can now give the 'key' component to Entities. A key will be generated automatically and stored in Entity.key.value. You can then use this key to fetch the Entity from the World with World:getEntityByKey(key) The keys are generated with a generator function that can be overriden.
This commit is contained in:
parent
3d195c790f
commit
a55efd042a
6 changed files with 148 additions and 15 deletions
37
concord/builtins/key.lua
Normal file
37
concord/builtins/key.lua
Normal file
|
@ -0,0 +1,37 @@
|
|||
local PATH = (...):gsub('%.builtins%.[^%.]+$', '')
|
||||
|
||||
local Component = require(PATH..".component")
|
||||
|
||||
local getKey = function (self, key)
|
||||
local entity = self.__entity
|
||||
|
||||
if not entity:inWorld() then
|
||||
error("entity needs to belong to a world")
|
||||
end
|
||||
|
||||
local world = entity:getWorld()
|
||||
|
||||
return world:__assignKey(entity, key)
|
||||
end
|
||||
|
||||
local Key = Component("key", function (self, key)
|
||||
self.value = getKey(self, key)
|
||||
end)
|
||||
|
||||
function Key:deserialize (data)
|
||||
self.value = getKey(self, data.value)
|
||||
end
|
||||
|
||||
function Key:removed (replaced)
|
||||
if not replaced then
|
||||
local entity = self.__entity
|
||||
|
||||
if entity:inWorld() then
|
||||
local world = entity:getWorld()
|
||||
|
||||
return world:__clearKey(entity)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return Key
|
Loading…
Add table
Add a link
Reference in a new issue