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

loadstring not working, been trying for ages with no hope.

Started by 2000korosh, 02 July 2015 - 07:04 PM
2000korosh #1
Posted 02 July 2015 - 09:04 PM
I am trying to make a delivery robot but when I try to use loadstring i get: attempt to call nil
My code on the robot is: http://pastebin.com/5j9Svdkk
The code on the sending computer is: modem.transmit(3,1,"cobblestone") modem.transmit(3,1,"6")
I have spent at least an hour searching for solutions but none of them have been working. By the way the error occurs at the 3rd to last line. Thank you very much!
Yevano #2
Posted 02 July 2015 - 10:56 PM
You're trying to use modem as an upvalue in your string function, but since you're creating the function on the spot with loadstring it won't actually recognize your modem variable. Instead, it will try to index the global table and since modem is declared local it will just get nil. There are two ways you could fix this: Either make modem global or give the modem variable to your string function as a parameter.

Though I have to ask, why do you need to do this with loadstring anyways?

EDIT: I think I misread your code/post a bit. Valithor seems to have a good answer for you.
Edited on 02 July 2015 - 11:37 PM
valithor #3
Posted 03 July 2015 - 01:10 AM
Another solution would be to set the function environment using setfenv on the function returned by loadstring.


func = loadstring("some code here")
if type(func) == "function" then
  setfenv(func,getfenv())
  func()
end

Apart from that could you give us an example of what itemname or person would be? From what I can tell the loadstring there would accomplish nothing.


edit:

The above was assuming you were attempting to loadstring something that would actually work.

On the line above the error you are attempting to loadstring "cobblestone". When you do this loadstring returns 2 things. The first thing it returns (what func is being set to) is nil. The second is a error saying why the loadstring failed. "cobblestone" is not something you would loadstring, so could you tell us what you were trying to accomplish by using loadstring on it?


edit2:

Now that I actually read all of the code I understand what you were trying to do.

When you loadstring something it creates a function that will do whatever you passed to the loadstring. So "cobblestone" is not a valid thing to pass to loadstring as it does not know what you want to do with it. Instead you will want to change the 4 to last line and the second to last line to the following:


func = loadstring(itemname.."()") -- runs the cobblestone function
if func then func() end
func = loadstring(person.."()") -- runs the ko function
if func then func() end
Edited on 02 July 2015 - 11:23 PM
Bomb Bloke #4
Posted 03 July 2015 - 08:28 AM
Personally I'd stick the functions in a table and just index into it.

Eg:

local myFuncs = {}

function myFuncs.ko()
	-- Do stuff.
end

function myFuncs.collect()
	-- Do stuff.
end

function myFuncs.cobblestone()
	-- Do stuff.
end

local doThis = "cobblestone"

myFuncs[doThis]()