Jump to content

Pass function as parameter (or maybe callback?)


anchor

Recommended Posts

Hello all.

I'm creating a function that the parameters are functions (or maybe it called callback? idk)
What i'm going to do is creating a function that creates buttons and do some dxdraw, but with a handler, and exports the function so i can reuse it whenever i need it.

The problem is the function is returning nil.

I've read in another forums, it said " Lua functions are just values, and you can asssign them using their name without paren"
Link
But it doesnt work.

Here the codes (I simplify it)

 

Quote

confirmations.lua

local messages = {}

function drawConfirmationButton( )
	for k,message in ipairs( messages ) do
		if ( message[2] ) and ( message[3] ) then
			destroyElement( message[2] )
			destroyElement( message[3] )
		end
		message[2] = guiCreateButton(--[[blabla]])
		message[3] = guiCreateButton(--[[blabla]])
		addEventHandler( "onClientGUIClick", message[2], message[4] )
		addEventHandler( "onClientGUIClick", message[3], message[5] )
	end
end


function createConfirmations( m, onAccept, onReject )
  -- i've been log the onAccept and onReject and it return nil
  	table.insert( messages, { m, acceptButton, rejectButton, onAccept, onReject } )
  
	addEventHandler( "onClientRender", root, drawConfirmation )
	drawConfirmationButton()
end

 

Use it in another file.

exports.common:createConfirmations( "Accept the request?", function() --[[code here]] end, function() --[[code here]] end )

 

Maybe some of you can help me or maybe another methods.

 

Thanks for your attentions, sorry for my bad English :)

Edited by anchor
Link to comment

The code seems to be alright and should work.

I don't know but maybe you just can't send functions over exports. Try adding the following debug line on line 18: 

outputDebugString("onAccept: "..tostring(onAccept).." | onReject: "..tostring(onReject))

and see whether it outputs "function: ..." or "nil." Can't think of anything else to try.

Link to comment
16 hours ago, MrTasty said:

The code seems to be alright and should work.

I don't know but maybe you just can't send functions over exports. Try adding the following debug line on line 18: 


outputDebugString("onAccept: "..tostring(onAccept).." | onReject: "..tostring(onReject))

and see whether it outputs "function: ..." or "nil." Can't think of anything else to try.

It returns nil, as the comment that i've been written.

and yes i think the problem is exports doesnt allow function to be passed.

Edited by anchor
Link to comment

Same has happened to me previously, I can surely say for experience that the exports function inflicts when you input a function value. One of the reasons can be because everytime you send the function, it won't make a reference to the original function, acting like a sandbox within resources, but also indeed, create a new one hence making it  excessively usage to the memory.

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