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

Variables from input and parallel

Started by ShadowDisruptor, 28 November 2014 - 12:42 AM
ShadowDisruptor #1
Posted 28 November 2014 - 01:42 AM
Hello! I'm currently working on a project where I need a few things. I wish to be able to name and access variables from string-based input. Is this possible? For example:

function setVar(varname, value)
  (varname) = value
end

setVar("chicken", "Hello!")
print(chicken)
This code would print out "Hello!"

Another issue I'm having is with parallels. I need to run a countdown and a click-detection at the same time. I've tried using parallels, but when I do, it waits for a click then runs the timer. Any help?
KingofGamesYami #2
Posted 28 November 2014 - 01:53 AM
You can't, as far as I know, declare a variable variable. The closest you can come is by using a table, or by defining it in the global table.


setVar( varname, value )
  _G[ varname ] = value
end

As for parallels, you're probably using them incorrectly.


--#some valid parallel code

local function clickdetect() --#notice the empty ()
  while true do
    local event = { os.pullEvent( "mouse_click" ) }
    --#stuff here
  end
end

local function countdown() --#again, empty ()
  for i = 10, 0, -1 do
    print( i )
  end
end

parallel.waitForAll( clickdetect, countdown ) --#DON'T call the functions.  Note the lack of ()...