Page 1 of 1

How do I know when an object is deactivated?

Posted: Tue May 14, 2019 19:35
by BrunoMine
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?

Posted: Wed May 15, 2019 15:01
by AiTechEye
get_staticdata are called to save

Re: How do I know when an object is deactivated?

Posted: Wed May 15, 2019 17:46
by Termos
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.

Re: How do I know when an object is deactivated?

Posted: Wed May 15, 2019 18:50
by BrunoMine
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?

Posted: Wed May 15, 2019 19:30
by AiTechEye
try:
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

Re: How do I know when an object is deactivated?

Posted: Wed May 15, 2019 19:32
by BrunoMine
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?

Posted: Wed May 15, 2019 20:51
by Termos
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?

Posted: Thu May 16, 2019 04:32
by sorcerykid
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.

Re: How do I know when an object is deactivated?

Posted: Thu May 16, 2019 10:10
by AiTechEye
Termos 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?
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 missing

Re: How do I know when an object is deactivated?

Posted: Thu May 16, 2019 15:38
by BrunoMine
Here you can see my code:
get_staticdata

on_deactivate

Re: How do I know when an object is deactivated?

Posted: Thu May 16, 2019 21:01
by Termos
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.

Re: How do I know when an object is deactivated?

Posted: Fri May 17, 2019 00:57
by BrunoMine
Termos wrote:This doesn't say anything about what kind of tasks you'd want to perform on deactivation.
The task is to create a callback to reach other tasks in the future. Does that answer your question?
Termos wrote:Checking self.activated will only result with occasional false positives and calling on_deactivate for active objects.
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?

Re: How do I know when an object is deactivated?

Posted: Fri May 17, 2019 10:15
by Termos
BrunoMine wrote: The task is to create a callback to reach other tasks in the future. Does that answer your question?
Yes it does, you want to have it just in case you need it. Waste of time imo.
BrunoMine wrote:Maybe I should even check if the self.object is nil to make sure it is deactivated?
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.

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.

Re: How do I know when an object is deactivated?

Posted: Fri May 17, 2019 14:28
by BrunoMine
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?

Posted: Fri May 17, 2019 17:54
by Termos
BrunoMine wrote:I can make a luaentity copy and verify that the object still exists after 0.1 or more seconds.
Sure, but that's another topic, not related to callbacks and the self.

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

Re: How do I know when an object is deactivated?

Posted: Fri May 17, 2019 18:20
by BrunoMine
- Removed -

Re: How do I know when an object is deactivated?

Posted: Fri May 17, 2019 19:27
by sorcerykid
Termos wrote: and because entities can disappear for various reasons, on_deactivate won't help you with that.
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.

Re: How do I know when an object is deactivated?

Posted: Fri May 17, 2019 19:30
by BrunoMine
Termos wrote: Sure, but that's another topic, not related to callbacks and the self.
I'm not sure I understood you here.
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?

Posted: Fri May 17, 2019 21:52
by Termos
sorcerykid wrote: An on_deactivate callback would always be invoked any time the object is removed from the environment for whatever reason.
This is your implementation, I was referring to OP's attempt which is supposed to work differently.
By the way, are you trying to get your modifications into official MT?