Maris Posted February 7, 2015 Share Posted February 7, 2015 For example, _activeplayers variable from kramped component. Link to comment https://forums.kleientertainment.com/forums/topic/50659-how-to-access-to-private-variables-of-a-component/ Share on other sites More sharing options...
DarkXero Posted February 7, 2015 Share Posted February 7, 2015 You don't.That's why they are private, the variables exist within the class function only. Link to comment https://forums.kleientertainment.com/forums/topic/50659-how-to-access-to-private-variables-of-a-component/#findComment-610739 Share on other sites More sharing options...
Kzisor Posted February 7, 2015 Share Posted February 7, 2015 @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. Link to comment https://forums.kleientertainment.com/forums/topic/50659-how-to-access-to-private-variables-of-a-component/#findComment-610770 Share on other sites More sharing options...
DarkXero Posted February 7, 2015 Share Posted February 7, 2015 Kzisor, what? http://www.tutorialspoint.com/lua/lua_debugging.htm Oooh. simplex is a fuckin'-A+ beast. Link to comment https://forums.kleientertainment.com/forums/topic/50659-how-to-access-to-private-variables-of-a-component/#findComment-610793 Share on other sites More sharing options...
Maris Posted February 7, 2015 Author Share Posted February 7, 2015 Show an example, please. Link to comment https://forums.kleientertainment.com/forums/topic/50659-how-to-access-to-private-variables-of-a-component/#findComment-610835 Share on other sites More sharing options...
Kzisor Posted February 8, 2015 Share Posted February 8, 2015 @Maris, if you want an example look at wicker. Link to comment https://forums.kleientertainment.com/forums/topic/50659-how-to-access-to-private-variables-of-a-component/#findComment-610847 Share on other sites More sharing options...
simplex Posted February 11, 2015 Share Posted February 11, 2015 (edited) 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")endNote 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 February 11, 2015 by simplex Link to comment https://forums.kleientertainment.com/forums/topic/50659-how-to-access-to-private-variables-of-a-component/#findComment-612049 Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now