Jump to content

Add handler to method


Anderl

Recommended Posts

Posted

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!

"[...] If you don’t love it, if you’re not having fun doing it, you don’t really love it, you’re going to give up." - Steve Jobs, 2007

Posted

Damn, no one knows?

"[...] If you don’t love it, if you’re not having fun doing it, you don’t really love it, you’re going to give up." - Steve Jobs, 2007

Posted

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.

Do NOT PM ME for help unless invited. - New MTA Script Editor

Scripting help "etiquette": understandable language, relevant code (ALL code if unsure), [Lua] tags, error messages with line numbers. Super simple stuff.

Posted

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.

"[...] If you don’t love it, if you’re not having fun doing it, you don’t really love it, you’re going to give up." - Steve Jobs, 2007

Posted

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?

Do NOT PM ME for help unless invited. - New MTA Script Editor

Scripting help "etiquette": understandable language, relevant code (ALL code if unsure), [Lua] tags, error messages with line numbers. Super simple stuff.

Posted

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?

Do NOT PM ME for help unless invited. - New MTA Script Editor

Scripting help "etiquette": understandable language, relevant code (ALL code if unsure), [Lua] tags, error messages with line numbers. Super simple stuff.

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

http://vk.com/the_kenix

Вопросы задавайте на форуме, не пишите мне в личку.

Please don't pm me.

Posted

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.

"[...] If you don’t love it, if you’re not having fun doing it, you don’t really love it, you’re going to give up." - Steve Jobs, 2007

Posted

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

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

"[...] If you don’t love it, if you’re not having fun doing it, you don’t really love it, you’re going to give up." - Steve Jobs, 2007

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