DS-minetest wrote:I've got a very big problem. I'm not sure if this is the right place to post this but I'm not sure enough to open an issue.
To test I made this mod:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
minetest.register_entity(
"eat:test1",
{
on_activate = function(self, staticdata, dtime_s)
minetest.chat_send_all("<1> activated")
minetest.chat_send_all("<1> "..dtime_s)
if staticdata == nil then
minetest.chat_send_all("<1> nil")
elseif staticdata == "" then
minetest.chat_send_all("<1> \"\"")
else
minetest.chat_send_all("<1> "..staticdata)
end
if not self.at then
self.at = minetest.add_entity(self.object:getpos(), "eat:test2")
end
if self.at then
self.at:set_attach(self.object, "", {x=0,y=1,z=1}, {x=0,y=0,z=0})
minetest.chat_send_all("<1> attach")
end
self.object:setvelocity({x=1,y=0,z=0})
end,
get_staticdata = function(self)
return "sta"
end,
}
)
minetest.register_entity(
"eat:test2",
{
on_activate = function(self, staticdata, dtime_s)
minetest.chat_send_all("<2> activated")
minetest.chat_send_all("<2> "..dtime_s)
if staticdata == nil then
minetest.chat_send_all("<2> nil")
elseif staticdata == "" then
minetest.chat_send_all("<2> \"\"")
else
minetest.chat_send_all("<2> "..staticdata)
end
if staticdata ~= "" then
if not self.object:get_attach() then
minetest.chat_send_all("<2> remove")
self.object:remove()
end
end
end,
get_staticdata = function(self)
return "sta"
end,
}
)
entity-activation-test.zip
Entity2 should be always attached to entity1, even after de- and activation.
I spawned the entity and flew away and came again and watched what happens.
I'm pretty sure that something is wrong. I'm just not sure what exactly is wrong. The reason for this is that different things happen.
On join everything is ok.
Most of the times entity2 isn't attached for some reason.
It seems like if I don't fly far enough away the entities are deactivated but not activated again.
Try it out yourself and be confused like me.
Someone would need to rewrite all attachment code. There are many issues with it.
Some things you should know:
When objects get unloaded, attachments are lost. You need to reattach in the on_activate function.
Try this:
- Staff your second object (the attachee) with something unique, such as a concatenation of os.time()..os.clock()
(not that I am using this in advtrains....) and save that in staticdata
- In on_activate() of the first object (attacher) or inside step as long as not found, loop through minetest.luaentites and search for exactly that unique property. When found, attach that object.
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
attacher on_step()
....
if not self.has_attached then
for aoid, luaentity in pairs(minetest.luaentities) do
if luaentity.unique_id==id then
local attachee=minetest.object_refs[aoid]
self.object:set_attach(attachee, ....)
self.has_attached=true
end
end
end
attachee on_activate(self,staticdata)
self.unique_id=staticdata
if self.unique_id=="" then
self.unique_id=os.time()..os.clock()
end
attachee get_staticdata()
return self.unique_id