Jump to content

Anonymous Function inside Anonymous Function


DarkLink

Recommended Posts

Posted

I guess this doesnt work on LUA ? Because on my code the part of the removeEventHandler is not working...

I have no errors on the clientscript ..

Code:

  
addEventHandler( "onClientMouseEnter", small,  
    function() 
         
        addEventHandler("onClientRender", getRootElement(), function() textAmmo("7 pistol, 12 shotgun, 24 submachine, 30 assault rifles, 5 rifle")  end) 
         
         
    end, false) 
    addEventHandler("onClientMouseLeave", small,  
    function() 
        removeEventHandler("onClientRender", getRootElement(), function() textAmmo("7 pistol, 12 shotgun, 24 submachine, 30 assault rifles, 5 rifle")  end) 
    end, false) 
  

And I assure you, that small is defined, and textAmmo is fine..

It works to addEventHandler, but removing the event handler is not working...

Would appreciate some help! thanks again!

Posted

Aside from this looking horrible, you can't remove a function like that. You are defining a new function and passing it to rEH., even if it's exactly the same.

Assign it to a local variable, then pass that to rEH.

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

I cant do it..

Do you mean this ?

  
local textAmmoFunction  
  
function textAmmo(text) 
    if(ops and isElement(ops)) then 
                local x,y = guiGetPosition(ops, false) 
                local x1,y1 = guiGetSize (ops, false) 
                dxDrawText ( text, x + 30, y + y1/1.5 ,x + x1, y+y1, tocolor(255,255,255),  
                     1,"default", "left", "top", false, true, true) 
    end 
end 
  
  
addEventHandler( "onClientMouseEnter", small,  
    function() 
        textAmmoFunction = textAmmo("7 pistol, 12 shotgun, 24 submachine, 30 assault rifles, 5 rifle") 
        addEventHandler("onClientRender", getRootElement(), textAmmoFunction) 
         
         
    end, false) 
    addEventHandler("onClientMouseLeave", small,  
    function() 
        removeEventHandler("onClientRender", getRootElement(), textAmmoFunction) 
    end, false) 
  

[2012-07-15 17:34:54] WARNING: bp\client_money.lua:60: Bad argument @ 'addEventHandler' [Expected function at argument 3, got nil]

[2012-07-15 17:34:55] WARNING: bp\client_money.lua:66: Bad argument @ 'removeEventHandler' [Expected function at argument 3, got nil]

Posted

Nice to see that some people read LUA wikis yet.

"[...] 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

Heheh, function-as-variable can come as kind of a surprise if you're not used to it - relatively few languages support this kind of thing.

Might actually be my favorite thing about Lua. :3

(Edit: oh, and "horrible" might have been a slight exaggeration. If you indent it sanely, that'll go a long way. :))

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.

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