Jump to content

Using exports with function as a parameter/callback


chris1384

Recommended Posts

It's going to be hard to explain so bear with me.

Supposed we have a function like this (function is in a string):

exports.myresource:addProperty("security", "function(player) return getPlayerName(player) == 'owner' end")

Then, in myresource the string is then put in a loadstring function, as following (it only checks it, doesn't add it anywhere):

local testPlayer = getPlayerFromName("player1") -- testing purposes
local loadstringed = loadstring(name.. " = ".. func_str)
local success, info = pcall(loadstringed, testPlayer)

iprint(success, info)
-- outputs:
true, nil

Apparently everything seems to work, the function does indeed exist, but doesn't seem to take any parameters into it.

How do you make a function loaded with loadstring() that also supports parameters?
 

Edited by chris1384
Link to comment
  • Moderators
52 minutes ago, chris1384 said:

How do you make a function loaded with loadstring() that also supports parameters?

 

When loading code, you are putting a kind of chunk/function around it.

So basically this:

function(player) return getPlayerName(player) == 'owner' end

Looks more or less like this:

 --  pcall(loadstringed) is calling this chunk/function:
function ()
	function(player) return getPlayerName(player) == 'owner' end
end

 

So in order to solve this:

return function(player) return getPlayerName(player) == 'owner' end

 

local success, theFunc = pcall(loadstringed)

local success2, isOwner = pcall(theFunc, testPlayer)

 

See also xpcall for better error handling:

https://www.gammon.com.au/scripts/doc.php?lua=xpcall

  • Like 1
Link to comment
local func_str = "function(player) return getPlayerName(player) == owner end"
local loadstringed = loadstring("local owner = 'owner'; return function(player) "..func_str.." end")
local myFunc = loadstringed()

-- Call the function with a parameter
local testPlayer = getPlayerFromName("player1")
local result = myFunc(testPlayer)
print(result) -- true

---

local func_str = "function(player) return getPlayerName(player) == 'owner' end"
local loadstringed = loadstring("return "..func_str)
local success, myFunc = pcall(loadstringed)

-- Call the function with a parameter
local testPlayer = getPlayerFromName("player1")
local success2, isOwner = pcall(myFunc, testPlayer)

print(success2, isOwner) -- true    false

 

  • Thanks 1
Link to comment

The proper way in Lua to use arguments in a function created using loadstring is vararg expression, written as three dots.

local argument1, argument2, argument3 = ...

In your case, the function body would look like this:

local player = ...
return getPlayerName(player) == 'owner'

An example with loadstring:

local func_str = "local player = ... return getPlayerName(player) == 'owner'"
local loadstringed = loadstring(func_str)
local testPlayer = getPlayerFromName("player1")
local success, info = pcall(loadstringed, testPlayer)
  • Like 2
Link to comment

Everything is working now. It was easier than I thought lol.

11 hours ago, DiSaMe said:

The proper way in Lua to use arguments in a function created using loadstring is vararg expression, written as three dots.

local argument1, argument2, argument3 = ...

In your case, the function body would look like this:

local player = ...
return getPlayerName(player) == 'owner'

An example with loadstring:

local func_str = "local player = ... return getPlayerName(player) == 'owner'"
local loadstringed = loadstring(func_str)
local testPlayer = getPlayerFromName("player1")
local success, info = pcall(loadstringed, testPlayer)

I was planning to use this in the future, I had to get over the basics first because that's where it screwed me up the most. Thanks for the tip

And thank you all for helping!

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