This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Micheal Pearce's profile picture

Passing variables though loadstring functions

Started by Micheal Pearce, 08 December 2015 - 03:31 AM
Micheal Pearce #1
Posted 08 December 2015 - 04:31 AM
I've hit a roadblock in my program, is there anyway to do something like this?

x = "Hello World"
func = "write(x)"
assert(loadstring(func))()
Lyqyd #2
Posted 08 December 2015 - 04:45 AM
What are you actually trying to do?
NanoBob #3
Posted 08 December 2015 - 10:53 AM
I think he wants to store a function to a variable, from a string. You can do this by using the global namespace

local func=_G["print"]
func("hi there")
Edited on 08 December 2015 - 09:53 AM
Bomb Bloke #4
Posted 08 December 2015 - 11:00 AM
There are multiple ways to do what he's asking, and there are many more ways to get similar effects. The most suitable answer depends on what the actual purpose is.
H4X0RZ #5
Posted 08 December 2015 - 12:56 PM
Are you trying to "bind" the first argument, or do you want to pass arguments?
SquidDev #6
Posted 08 December 2015 - 04:30 PM
The easiest thing to do might be something like:


x = "Hello World"
func = "local x = ... write(x)"
assert(loadstring(func))(x)

If you want to generalise this you could write something like:


local function withEnv(str, env)
  local names, args = {}, {}
  for k, v in pairs(env) do names[#names + 1] = k args[#args + 1] = v end

  return load("local " .. table.concat(names) .. " = ...; " .. str)(unpack(args))
end

withEnv("write(x)", { x = "Hello world")

Or using globals

local function withEnv(str, env)
  return load(str, setmetatable(env, {__index = _ENV}))
end
Edited on 08 December 2015 - 03:30 PM
Micheal Pearce #7
Posted 09 December 2015 - 02:30 AM
The easiest thing to do might be something like:


x = "Hello World"
func = "local x = ... write(x)"
assert(loadstring(func))(x)

Thanks! this is exactly what I need, but I'm having a hard time understanding why it works tho, anyone care to expand on this?
Micheal Pearce #8
Posted 09 December 2015 - 02:43 AM
What are you actually trying to do?

World domination? Haha that project isn't until the 3rd quarter of 2017. It's an IDE of sorts and game engine for Cc complete with a map maker able to create a map up to 999x999, move able camera, player api and logic point creator which is what i'm needing help with.
KingofGamesYami #9
Posted 09 December 2015 - 03:04 AM
If you have ever used command-line arguments in a program, you're familier with '…' If not, it's simply an argument that accepts any number of arguments.

A short example:

local f = function( ... )
  local t = {...} --#stores the arguments in a table, such that t[ 1 ] will equal the first argument, t[ 2 ] the second, and so on.
  for k, v in pairs( t ) do --#iterate through the table
    print( v ) --#print each argument
  end
end

f( "Hello Word" ) --#prints Hello World
f( "Hello World", "And everyone else" ) --#prints Hello world on one line, then And everyone else on the second

In SquidDev's example, he sets the local variable x equal to …, which might not behave as you'd expect. Instead of x becoming the same as …, x instead becomes the first value stored in … (in this case, the first argument provided to the function). He then writes that value to the screen.
Edited on 09 December 2015 - 02:05 AM
Micheal Pearce #10
Posted 09 December 2015 - 03:32 AM
Radical! I didn't realize I could use it like that!