Noki Posted November 19, 2015 Share Posted November 19, 2015 Preface: I'm trying to use loadstring to pass a variable. I'm using it with and without assert and not having any luck. I first call a function in another resource (using export), which passes the variable fine. However, the problem is in passing it using loadstring (see the second block of code). Everything else works fine, except this. It's worth mentioning this is entirely client-sided. It works fine using runcode, which is odd. arg = 1 exports.util:createConfirmationWindow("Are you sure you want to sell this vehicle?", "exports.system:callback_sellVehicle(arg)", arg) outputDebugString("util: argument passed = "..tostring(arg)) if (callbackString ~= "") then loadstring(callbackString)(arg) assert(loadstring(callbackString))(arg) end arg within the scope of that piece of code is equal to 1. When outputted in debugstring, it always outputs 1. function callback_sellVehicle(arg) outputDebugString("Successfully called callback_sellVehicle") outputDebugString("system: argument passed = "..tostring(arg)) end However, when calling this function via loadstring and passing the arg variable, it always returns nil, both with and without assert. Link to comment
Saml1er Posted November 19, 2015 Share Posted November 19, 2015 I don't remember exactly how I did it but my current code looks like this: local f, err = loadstring (callbackString) if err then --failed? Lets try with return that works most of the time f, err = loadstring ( "return "..callbackString) end if not err then pcall ( f ) end -- lua.org recommends to use pcall EDIT: I doubt if you can pass variables like into command function. Why don't you concat it? "exports.something ("..arg..")" Link to comment
Noki Posted November 20, 2015 Author Share Posted November 20, 2015 EDIT: I doubt if you can pass variables like into command function. Why don't you concat it? I was able to with runcode, like the way I posted it above. However, I did not think of concatenating it and I am now asking myself why I didn't think of that. Thanks! Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now