Jump to content

Passing variables between resources with loadstring


Noki

Recommended Posts

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

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

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