Jump to content

Add handler to method


Anderl

Recommended Posts

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

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

I can't pretend to know what, exactly, that self thing means. When coding OOP, I prefer it to be... err, proper OOP? :P

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

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
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

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
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

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...