Jump to content

Recommended Posts

@Maris, you can only access private variable using debug information. It's a very complicated method and is rated at advanced level lua programming. It's only recommended for the most advance of advance users as you can easily mess up things if not careful.

 

simplex does this in wicker with their custom RPC's being registered and sent to the server.

Show an example, please. :-)

From wicker's game/reflection.lua:

function FindUpvalue(fn, upvalue_name)	assert(type(fn) == "function", "Function expected as 'fn' parameter.")	local info = debug.getinfo(fn, "u")	local nups = info and info.nups	if not nups then return end	local getupvalue = debug.getupvalue	for i = 1, nups do		local name, val = getupvalue(fn, i)		if name == upvalue_name then			return val, true		end	endend
(note that both 'assert' and 'debug' would need to be prefixed by GLOBAL if defining this in the mod environment).

And upvalue is an external local variable used by a function (i.e., a local variable not defined within the function itself). Therefore, getting access to such a value relies on having an accessible function using the value, which is why this method should be a last resort (since if the function is changed to no longer use that variable directly, such as calling another function which in turn uses that value, the code will just break). An usage example would be:

if GLOBAL.TheWorld.components.kramped then    local _activeplayers = FindUpvalue(GLOBAL.TheWorld.components.kramped.OnUpdate, "_activeplayers")end
Note that the OnUpdate method of the kramped component accesses _activeplayers, which is why it may be used as a "pivot" to reach it.

You may prefer to pester PeterA instead, asking for more things to be properly exposed. This is the best approach for the sake of the whole modding community, since it may improve the quality of life of modders in the future, though it's certainly slower than just using the debug library to get things done. I personally like to do both things.

Edited by simplex

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
  • Create New...