How do I know when an object is deactivated?

User avatar
BrunoMine
Member
 
Posts: 1048
Joined: Thu Apr 25, 2013 17:29
Location: SP-Brasil
GitHub: BrunoMine

How do I know when an object is deactivated?

by BrunoMine » Tue May 14, 2019 19:35

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: 745
Joined: Fri May 29, 2015 21:14
Location: Unknown
GitHub: AiTechEye
 

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

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

by Termos » Wed May 15, 2019 17:46

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: 1048
Joined: Thu Apr 25, 2013 17:29
Location: SP-Brasil
GitHub: BrunoMine

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

by BrunoMine » Wed May 15, 2019 18:50

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: 745
Joined: Fri May 29, 2015 21:14
Location: Unknown
GitHub: AiTechEye

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

by AiTechEye » Wed May 15, 2019 19:30

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: 1048
Joined: Thu Apr 25, 2013 17:29
Location: SP-Brasil
GitHub: BrunoMine

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

by BrunoMine » Wed May 15, 2019 19:32

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: 184
Joined: Sun Dec 16, 2018 12:50

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

by Termos » Wed May 15, 2019 20:51

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: 1061
Joined: Fri Aug 26, 2016 15:36
Location: Illinois, USA
GitHub: sorcerykid
In-game: Nemo

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

by sorcerykid » Thu May 16, 2019 04:32

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: 745
Joined: Fri May 29, 2015 21:14
Location: Unknown
GitHub: AiTechEye

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

by AiTechEye » Thu May 16, 2019 10:10

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: 1048
Joined: Thu Apr 25, 2013 17:29
Location: SP-Brasil
GitHub: BrunoMine
 

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

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

by Termos » Thu May 16, 2019 21:01

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: 1048
Joined: Thu Apr 25, 2013 17:29
Location: SP-Brasil
GitHub: BrunoMine

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

by BrunoMine » Fri May 17, 2019 00:57

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: 184
Joined: Sun Dec 16, 2018 12:50

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

by Termos » Fri May 17, 2019 10:15

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: 1048
Joined: Thu Apr 25, 2013 17:29
Location: SP-Brasil
GitHub: BrunoMine

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

by BrunoMine » Fri May 17, 2019 14:28

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

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

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

by Termos » Fri May 17, 2019 17:54

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: 1048
Joined: Thu Apr 25, 2013 17:29
Location: SP-Brasil
GitHub: BrunoMine

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

by BrunoMine » Fri May 17, 2019 18:20

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

User avatar
sorcerykid
Member
 
Posts: 1061
Joined: Fri Aug 26, 2016 15:36
Location: Illinois, USA
GitHub: sorcerykid
In-game: Nemo

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

by sorcerykid » Fri May 17, 2019 19:27

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: 1048
Joined: Thu Apr 25, 2013 17:29
Location: SP-Brasil
GitHub: BrunoMine

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

by BrunoMine » Fri May 17, 2019 19:30

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: 184
Joined: Sun Dec 16, 2018 12:50

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

by Termos » Fri May 17, 2019 21:52

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?
 


Return to Modding Discussion



Who is online

Users browsing this forum: Steamed_Punk and 3 guests