How do I know when an object is deactivated?

Post Reply
User avatar
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?

by BrunoMine » Post

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?

User avatar
AiTechEye
Member
Posts: 1000
Joined: Fri May 29, 2015 21:14
GitHub: AiTechEye
Location: Sweden

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

by AiTechEye » Post

get_staticdata are called to save

Termos
Member
Posts: 417
Joined: Sun Dec 16, 2018 12:50

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

by Termos » Post

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.

User avatar
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?

by BrunoMine » Post

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.

User avatar
AiTechEye
Member
Posts: 1000
Joined: Fri May 29, 2015 21:14
GitHub: AiTechEye
Location: Sweden

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

by AiTechEye » Post

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

User avatar
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?

by BrunoMine » Post

Yes, I make a structure similar to this and worked well for now, I just hope it keeps working fine in the future.

Termos
Member
Posts: 417
Joined: Sun Dec 16, 2018 12:50

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

by Termos » Post

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?

User avatar
sorcerykid
Member
Posts: 1841
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?

by sorcerykid » Post

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.

User avatar
AiTechEye
Member
Posts: 1000
Joined: Fri May 29, 2015 21:14
GitHub: AiTechEye
Location: Sweden

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

by AiTechEye » Post

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

User avatar
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?

by BrunoMine » Post

Here you can see my code:
get_staticdata

on_deactivate

Termos
Member
Posts: 417
Joined: Sun Dec 16, 2018 12:50

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

by Termos » Post

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.

User avatar
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?

by BrunoMine » Post

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?

Termos
Member
Posts: 417
Joined: Sun Dec 16, 2018 12:50

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

by Termos » Post

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.

User avatar
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?

by BrunoMine » Post

I can make a luaentity copy and verify that the object still exists after 0.1 or more seconds.

Termos
Member
Posts: 417
Joined: Sun Dec 16, 2018 12:50

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

by Termos » Post

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

User avatar
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?

by BrunoMine » Post

- Removed -
Last edited by BrunoMine on Fri May 17, 2019 19:33, edited 1 time in total.

User avatar
sorcerykid
Member
Posts: 1841
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?

by sorcerykid » Post

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.
Last edited by sorcerykid on Fri May 17, 2019 19:33, edited 2 times in total.

User avatar
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?

by BrunoMine » Post

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.

Termos
Member
Posts: 417
Joined: Sun Dec 16, 2018 12:50

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

by Termos » Post

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?

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests