How to know if function definitions have been overwritten?

User avatar
Wuzzy
Member
 
Posts: 2161
Joined: Mon Sep 24, 2012 15:01
GitHub: Wuzzy2
IRC: Wuzzy
In-game: Wuzzy

How to know if function definitions have been overwritten?

by Wuzzy » Sat Nov 05, 2016 04:32

I have the following code:

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
local item = minetest.registered_items[some_variable]


Assuming that some_variable is a legit itemstring, then item will contain a legit item definition table.
Question: How do I find out if certain callback functions, such as on_use or on_place are left at the default functions?
I need it to figure out whether a craftitem or tool has the default on_place or on_use behaviour as opposed to a custom behaviour where the modder added a custom function into the definition table?

Sadly, those values seem to be always set to a function value. They don't seem to be nil, ever. This makes it very hard to check, I suppose. :-/
I also tried comparing the function pointers, but this doesn't help either. I don't know to which function I should compare. :-(

I hope you understand my problem and you can help me. :-)
I'm creating MineClone 2, a Minecraft clone for Minetest.
I made the Help modpack, adding in-game help to Minetest.
 

Chiu ChunLing
Member
 
Posts: 39
Joined: Sat Oct 22, 2016 09:37

Re: How to know if function definitions have been overwritte

by Chiu ChunLing » Sat Nov 05, 2016 04:46

Heh, I don't know that this is even possible. The on_use/on_place default functions are generated automatically by the item/placeable metatable, but that should mean that each generated function has a unique address, which is why you can't compare to see if things have the same function as the default function.

I guess the real question is why you need to know. There might be another work-around.
 

User avatar
TheReaperKing
Member
 
Posts: 493
Joined: Sun Nov 22, 2015 21:36

Re: How to know if function definitions have been overwritte

by TheReaperKing » Sat Nov 05, 2016 05:39

I'm probably the last person that should be giving coding advice but for me I like to use a lot of "print"s to see if my code is how it should be and where they might be problems. Perhaps you could have it "print" when you use your on_use?
Project Lead of the Doom 3 Mod Last Man Standing - http://Doom3Coop.com

Project Lead of Platinum Arts Sandbox Free 3D Game Maker - http://SandboxGameMaker.com

Youtube Channel - https://www.youtube.com/user/PlatinumArtsKids
 

User avatar
rubenwardy
Member
 
Posts: 4500
Joined: Tue Jun 12, 2012 18:11
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy

Re: How to know if function definitions have been overwritte

by rubenwardy » Sat Nov 05, 2016 11:47

Try debug.getinfo(foo).source with foo as the function
 

User avatar
Wuzzy
Member
 
Posts: 2161
Joined: Mon Sep 24, 2012 15:01
GitHub: Wuzzy2
IRC: Wuzzy
In-game: Wuzzy

Re: How to know if function definitions have been overwritte

by Wuzzy » Sat Nov 05, 2016 13:11

Thank you, rubenwardy! This was exactly what I was looking for. :-)

TheReaperKing: I wanted to know this to write a checker script for the qa_block mod. The checker script checks for “useless” items. It tries to find some some obvious uses of an item (is a node [=can be placed], is a mining tool, is a weapon, etc.) and if it finds none, it claims the item is “useless”, i.e. it seems to an item which appears to not be useful for anything. Another part of the check is to check whether on_use, after_use, on_secondary_use, on_place or on_drop are nil or at default values. My assumption is, if any of those is NOT the default value, the item is probably used for something. This is why I asked.

The current version of the checker had some false-positives because I did not know how to check 3 of these functions (it is not a simple check against nil).
With rubenwardy's help, I was able to improve the checker. For Minetest Game, it now only return the Mese Crystal Fragment. Which is of course correct, this item is not used by anything. :-)
I'm creating MineClone 2, a Minecraft clone for Minetest.
I made the Help modpack, adding in-game help to Minetest.
 

Byakuren
Member
 
Posts: 441
Joined: Tue Apr 14, 2015 01:59
GitHub: raymoo
IRC: Hijiri

Re: How to know if function definitions have been overwritte

by Byakuren » Sat Nov 05, 2016 17:36

Would rawget work to check, since apparently default definitions are done through metatables?
Every time a mod API is left undocumented, a koala dies.
 


Return to Modding Discussion

Who is online

Users browsing this forum: No registered users and 57 guests

cron