Anderl Posted July 5, 2012 Share Posted July 5, 2012 Hi guys. I was making a script when I got a problem: I need to add a handler to a method, and it's the only way to use the function, with methods. I don't know how would I add it cuz I get error and I can't do: function( ) self:__window( ) end as function cuz I'll need to remove handler later. Can somebody help? Note: If you don't know, don't answer! Link to comment
robhol Posted July 7, 2012 Share Posted July 7, 2012 I can't really say that I understand what the problem is. "I need to add a handler to a method, and it's the only way to use the function, with methods." doesn't really tell us what's wrong, why it's wrong, or what you're trying to do. Link to comment
Anderl Posted July 7, 2012 Author Share Posted July 7, 2012 lolz, I want to add handler to a method like: addEventHandler( 'onClientRender', root, self:__window ); But it gives error like: something.. in function arguments '('. Something like that. Link to comment
robhol Posted July 7, 2012 Share Posted July 7, 2012 I can't pretend to know what, exactly, that self thing means. When coding OOP, I prefer it to be... err, proper OOP? Looks like it's causing some problems though. For starters, how about a proxy function that calls the method? Assigning that method to a local variable and using that, on the off chance that the self: stuff is causing issues with the addEventHandler call? Link to comment
Tete omar Posted July 8, 2012 Share Posted July 8, 2012 debugscript 3 and show what's wrong Link to comment
Jaysds1 Posted July 8, 2012 Share Posted July 8, 2012 debugscript 3 and show what's wrong Actually post a screenshot of the debug error(if any) Link to comment
robhol Posted July 8, 2012 Share Posted July 8, 2012 He did already post an error, but a verbatim copy would be nice. Anyway, Lua is a bad language for OOP (half-assed and less-than-intuitive implementation) and MTA's API doesn't really go well with it either. How about sticking to procedural-style programming? Link to comment
Kenix Posted July 8, 2012 Share Posted July 8, 2012 lolz, I want to add handler to a method like: addEventHandler( 'onClientRender', root, self:__window ); But it gives error like: something.. in function arguments '('. Something like that. it's wrong, because you call method, so correct addEventHandler( 'onClientRender', root, self.__window ); here you use function, instead of call method. Link to comment
Anderl Posted July 8, 2012 Author Share Posted July 8, 2012 But you can't call self in the function __window anymore. Let me show you the code in Skype to you see how it should be. Link to comment
50p Posted July 8, 2012 Share Posted July 8, 2012 Seems like you're trying to make a lib with DX functions? Why don't you have a "global" onClientRender in your class for all its objects? local renderObjects = { }; table.insert( renderObjects, yourObject ); -- when new object is created, add it to the list of objects to render addEventHandler( "onClientRender", root, function( ) for k, v in pairs( renderObjects ) do if v:Visible() then -- i guess you have a method to check if object is visible v:__window( ); end end end ); Link to comment
Anderl Posted July 8, 2012 Author Share Posted July 8, 2012 Seems like you're trying to make a lib with DX functions? Why don't you have a "global" onClientRender in your class for all its objects? local renderObjects = { }; table.insert( renderObjects, yourObject ); -- when new object is created, add it to the list of objects to render addEventHandler( "onClientRender", root, function( ) for k, v in pairs( renderObjects ) do if v:Visible() then -- i guess you have a method to check if object is visible v:__window( ); end end end ); lolz, so much time thinking and didn't remember that way Thank you. And yes, I'm making a lib with DX functions. Link to comment
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