Jump to content

Short lua question


xScatta

Recommended Posts

Posted

Hey guys!

Is there a way to get "source Function" ?

Like this

  
-- in timer now we can get his source by sourceTimer 
setTimer(function () outputChatBox("1") killTimer(sourceTimer) end, 1000, 5) 
-- and it will outputChatBox only once 
  

And the question is:

Can we get the function variable without a name like this

  
addEventHandler("onPlayerClick",some player, function (button,state) 
if button == "left" and state == "down" then 
outputChatBox("you triggered the event only once! u can't do it anymore!") 
removeEventHandler(eventName,source,sourceFunction) -- here is what i want to know 
end 
end,false) 
  

Because it would be faster way to use.

Waiting for some help! :)

Posted

Maybe like this?

removeEventHandler(eventName, source, handlerFunction) 

Wiki says

handlerFunction: The handler function you wish to call when the event is triggered. This function will be passed all of the event's parameters as arguments, but it isn't required that it takes all of them.

Posted

I think u dont understand me, i want to create a local function without a name in the event handler. Not to create new one with name.

Someone can help tho?

Posted

Did you try it like this?

local sourceTimer = setTimer(function () outputChatBox("1") killTimer(sourceTimer) end, 1000, 5) 

Or rather use this:

local sourceTimer 
function someFunction() 
    sourceTimer = setTimer(timerFunction, 1000, 5) 
end 
  
function timerFunction() 
    outputChatBox("1") 
    killTimer(sourceTimer) 
end 

  • MTA Team
Posted
Did you try it like this?
local sourceTimer = setTimer(function () outputChatBox("1") killTimer(sourceTimer) end, 1000, 5) 

That won't work, but this might:

local sourceTimer 
sourceTimer = setTimer(function () outputChatBox("1") killTimer(sourceTimer) end, 1000, 5) 

Posted
Did you try it like this?
local sourceTimer = setTimer(function () outputChatBox("1") killTimer(sourceTimer) end, 1000, 5) 

That won't work, but this might:

local sourceTimer 
sourceTimer = setTimer(function () outputChatBox("1") killTimer(sourceTimer) end, 1000, 5) 

Yeah, I was thinking about this too later. Try it.

Posted

Guys im not talking about the timer, the timer was the example....., im talking about sourceFunction in event, when i don't name the function, can i get it from a variable?

Posted

As novo said there's not a predefined variable with that value, But you can obtain the "source function" using debug.getinfo:

local sourceFunction = debug.getinfo( 1, "f" ).func 

however the easiest solution is naming the function.

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