I could just do an on global step, check all logged in players inventories and update the player variable, but just trying to see if there's a better way to do this since I only need to change/update the variable when the item is added or removed from the player inventory, I expect this will not be very often so I'm basically checking all logged in players for no reason using a global step, I could set the time quiet high like every 30/60secs but even then seems like a unnecessary addition to global step to me....
I can see with Node and Detached Inventories there on_put, on_move and on_take. But no equivalent for player inventories....well not that I can see.
I don't understand callbacks at all, which might be because I tried seeing how 3darmor did it but it uses a detached inventory so Im not confident it isn't leveraging on_put et al.
I did find a simple Lua explanation On stackover flow -https://stackoverflow.com/questions/506 ... -parameter
Code: Select all
callback = function ()
... do stuff ...
end
function caller(callback)
callback()
end
-- run caller function
caller(callback)
I understand the first code block define your function in my case it would be for player update variable X. However I don't understand what step 2 does is it in some way tieing the callback function to the main function? Three is the function running when caller is run well I think because if thats the case Im not sure what step2 does then, so clearly Im not understanding step2 at all. I am struggling to tie the very simple theory above into an actual example usable in game I tried understanding 3d armors callbacks but I cant follow what is what.
Using 3d armor on equip call back, this looks like Step 1 at the beginning but then inside the code block I get lost as it looks more like step2 above ie we are tieing our function to on_equip....except I then cant dig out any example of what step 1 is....
Code: Select all
armor.register_on_equip = function(self, func)
if type(func) == "function" then
table.insert(self.registered_callbacks.on_equip, func)
end
end
Essentially i'm way out of my depth and I have too many things I dont know. Like does player_inv even have a function to tie a callback too. Even if it does which i don't know I'm then not sure how the theory above on callbacks ties into a real example. Lastly I don't even know from a programing Lua angle if a callback is more efficient in this case it seems it would be to me but then I have exactly zero expertise with callbacks....
Its more than possible I've missed some cool info somewhere but I haven't been able to dig anything up. Found this interesting post from SK which ive tried following through but at the end uses the built in minetest.register_on_dignode to trigger the callback and from what i can see player inv doesn't seem to have something like that I could tap into so I back to stepone.....
viewtopic.php?p=365175#p365175
As always I appreciate the help and I hope the above babbling makes some sense. I'll keep reading around myself but wanted to put a question up as Im coming up very blank. Im more than happy to take links to general Lua help files :).