How do I know when an object is deactivated?
- BrunoMine
- Member
- Posts: 1082
- Joined: Thu Apr 25, 2013 17:29
- GitHub: BrunoMine
- Location: SP-Brasil
- Contact:
How do I know when an object is deactivated?
There is a call to when the object "on_activate", but I want the same callback to the opposite, when an object "on_deactivate". Is there any idea how to do this before the object is deactivated?
Re: How do I know when an object is deactivated?
get_staticdata are called to save
Re: How do I know when an object is deactivated?
Afaik, get_staticdata is called on various occasions, not just deactivation, so it can't be relied upon to intercept deactivation event.
The way it's usually done is to save any relevant info in get_staticdata, and any stuff you'd do at deactivation, you do it in on_activate instead, when an object becomes active again.
The way it's usually done is to save any relevant info in get_staticdata, and any stuff you'd do at deactivation, you do it in on_activate instead, when an object becomes active again.
- BrunoMine
- Member
- Posts: 1082
- Joined: Thu Apr 25, 2013 17:29
- GitHub: BrunoMine
- Location: SP-Brasil
- Contact:
Re: How do I know when an object is deactivated?
But I can separate the activation from the deactivation using `self.activated = true`. But this will only work if get_staticdata runs only in these two situations.
Re: How do I know when an object is deactivated?
try:
it prints nil when it's deactivated and true when activated
it prints nil when it's deactivated and true when activated
Code: Select all
on_activate=function(self, staticdata)
self.activated = true
end,
get_staticdata = function(self)
print(self.activated)
self.activated=nil
end
- BrunoMine
- Member
- Posts: 1082
- Joined: Thu Apr 25, 2013 17:29
- GitHub: BrunoMine
- Location: SP-Brasil
- Contact:
Re: How do I know when an object is deactivated?
Yes, I make a structure similar to this and worked well for now, I just hope it keeps working fine in the future.
Re: How do I know when an object is deactivated?
Well the self exists only when the object is active, same with self.anything. What this code does is it sometimes sets active object's self.activated to nil.
Can you explain what it is you're trying to do?
Can you explain what it is you're trying to do?
- sorcerykid
- Member
- Posts: 1844
- Joined: Fri Aug 26, 2016 15:36
- GitHub: sorcerykid
- In-game: Nemo
- Location: Illinois, USA
Re: How do I know when an object is deactivated?
There is no way to know when an object is deactivated. The getstaticdata solution only works if an entity is being converted to a static object, but not specifically when it is removed from the environment.
https://github.com/minetest/minetest/issues/6084
My fork of the engine includes an on_deactivate callback. If you are interested, I can create a Minetest 5.0.0 git patch and post it here.
https://github.com/minetest/minetest/issues/6084
My fork of the engine includes an on_deactivate callback. If you are interested, I can create a Minetest 5.0.0 git patch and post it here.
Re: How do I know when an object is deactivated?
nope, get_staticdata is called before or after the object gets inactived and after on_activate, to storing data from self, so corrently it works fine, but self.object is missingTermos wrote:Well the self exists only when the object is active, same with self.anything. What this code does is it sometimes sets active object's self.activated to nil.
Can you explain what it is you're trying to do?
Re: How do I know when an object is deactivated?
This doesn't say anything about what kind of tasks you'd want to perform on deactivation.
Checking self.activated will only result with occasional false positives and calling on_deactivate for active objects.
Checking self.activated will only result with occasional false positives and calling on_deactivate for active objects.
- BrunoMine
- Member
- Posts: 1082
- Joined: Thu Apr 25, 2013 17:29
- GitHub: BrunoMine
- Location: SP-Brasil
- Contact:
Re: How do I know when an object is deactivated?
The task is to create a callback to reach other tasks in the future. Does that answer your question?Termos wrote:This doesn't say anything about what kind of tasks you'd want to perform on deactivation.
I figured this out, so I need to fix this code. Maybe I should even check if the self.object is nil to make sure it is deactivated?Termos wrote:Checking self.activated will only result with occasional false positives and calling on_deactivate for active objects.
Re: How do I know when an object is deactivated?
Yes it does, you want to have it just in case you need it. Waste of time imo.BrunoMine wrote: The task is to create a callback to reach other tasks in the future. Does that answer your question?
Not possible. Deactivated entities don't exist, therefore no callbacks are being called on their behalf, therefore no access to self, there's no way to check anything. If you can check, that already tells you it's active.BrunoMine wrote:Maybe I should even check if the self.object is nil to make sure it is deactivated?
Because minetest api doesn't provide a real on_deactivated callback, the only way is whatever you wanted to do in on_deactivated, to do it in on_activated instead, accounting for any dtime_s passed since deactivation.
- BrunoMine
- Member
- Posts: 1082
- Joined: Thu Apr 25, 2013 17:29
- GitHub: BrunoMine
- Location: SP-Brasil
- Contact:
Re: How do I know when an object is deactivated?
I can make a luaentity copy and verify that the object still exists after 0.1 or more seconds.
Re: How do I know when an object is deactivated?
Sure, but that's another topic, not related to callbacks and the self.BrunoMine wrote:I can make a luaentity copy and verify that the object still exists after 0.1 or more seconds.
If you want to access a stored luaentity, you should always check for its existence, and because entities can disappear for various reasons, on_deactivate won't help you with that.
Now I'm not sure it's the best workaround, but trying to call a lightweight object member function should do the trick, just not get_hp() - for some reason this one returns 1 for nonexistent objects.
Code: Select all
local function luaexists(luaent)
if luaent and luaent.object and luaent.object:get_yaw() then return true end
end
- BrunoMine
- Member
- Posts: 1082
- Joined: Thu Apr 25, 2013 17:29
- GitHub: BrunoMine
- Location: SP-Brasil
- Contact:
Re: How do I know when an object is deactivated?
- Removed -
Last edited by BrunoMine on Fri May 17, 2019 19:33, edited 1 time in total.
- sorcerykid
- Member
- Posts: 1844
- Joined: Fri Aug 26, 2016 15:36
- GitHub: sorcerykid
- In-game: Nemo
- Location: Illinois, USA
Re: How do I know when an object is deactivated?
An on_deactivate callback would always be invoked any time the object is removed from the environment for whatever reason. So it is the ideal way to track removal of entities. In fact I use the callback for this very purpose in my physics mod in order to track which physical objects are still active in the environment.Termos wrote: and because entities can disappear for various reasons, on_deactivate won't help you with that.
Last edited by sorcerykid on Fri May 17, 2019 19:33, edited 2 times in total.
- BrunoMine
- Member
- Posts: 1082
- Joined: Thu Apr 25, 2013 17:29
- GitHub: BrunoMine
- Location: SP-Brasil
- Contact:
Re: How do I know when an object is deactivated?
I'm not sure I understood you here.Termos wrote: Sure, but that's another topic, not related to callbacks and the self.
Okay, It may not be semantically correct to call this a luaentity callback, but if luaentity is only part of a MOB (like object is from luaentity), it may be acceptable call it MOB callback.
Re: How do I know when an object is deactivated?
This is your implementation, I was referring to OP's attempt which is supposed to work differently.sorcerykid wrote: An on_deactivate callback would always be invoked any time the object is removed from the environment for whatever reason.
By the way, are you trying to get your modifications into official MT?
Who is online
Users browsing this forum: No registered users and 1 guest