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

Running a line of code from a rednet msg.

Started by Sillyjake, 23 March 2014 - 12:49 AM
Sillyjake #1
Posted 23 March 2014 - 01:49 AM
Hello, I'm trying to make a television-like program, But the thing is I want to know how to run "print("Hello World!")" In a line, So its like… (RunThisLineHere is used for it, I just need to know what it really is)
while true do
local msg = rednet.receive()
RunThisLineHere(msg)
end
CometWolf #2
Posted 23 March 2014 - 01:59 AM
use dostring, it loads a string as a piece of code and executes it.

dostring(msg)
oeed #3
Posted 23 March 2014 - 04:31 AM
While CometWolf's method will work, it's probably not the most efficient or secure way to do it. Personally, I'd send it as a table then pick out certain bits.
theoriginalbit #4
Posted 23 March 2014 - 04:45 AM
use dostring
dostring? loadstring maybe?
oeed #5
Posted 23 March 2014 - 04:56 AM
use dostring
dostring? loadstring maybe?

Oh right, there isn't a dostring is there, only dofile.

In that case, you'll want this:

while true do
	local msg = rednet.receive()
	local f = loadstring(msg)
	f()
	-- you could do this in one line if you wish using the below, but the above allows for more flexibility
	loadstring(msg)()
end
Edited on 24 March 2014 - 07:50 PM
cygnis #6
Posted 23 March 2014 - 10:27 PM
Personally, i just use the shell.run function:

while true do
    local msg = rednet.receive()
    shell.run(msg)
end

If security is your thing, I'd also include an ID check to consider where the msg is coming from:

while true do
    local id,msg = rednet.receive()
    if id == yourSender then
	    shell.run(msg)
    end
end
CometWolf #7
Posted 23 March 2014 - 10:37 PM
Right, forgot there is no actual dostring function, although loadstring()() is pretty much the same.

shell.run does not execute code, and as such it would not do what the op asked for.
cygnis #8
Posted 23 March 2014 - 10:38 PM
shell.run does not execute code, and as such it would not do what the op asked for.

doh… right you are.
Lyqyd #9
Posted 23 March 2014 - 10:41 PM
If I recall correctly, the loadstring trick needs another set of parentheses to actually work:


(loadstring())()

It's been a while since I've done anything with it, though.
CometWolf #10
Posted 23 March 2014 - 10:46 PM
Just tested, it dosen't. loadstring only returns the function, provided there are no errors while compiling it.
apemanzilla #11
Posted 24 March 2014 - 02:20 PM
Oh right, there isn't a dostring is there, only dostring.
dostring ~= dostring? :P/>
oeed #12
Posted 24 March 2014 - 08:50 PM
Oh right, there isn't a dostring is there, only dostring.
dostring ~= dostring? :P/>
*faceplam*
I meant dofile obviously.
Sillyjake #13
Posted 24 March 2014 - 10:01 PM
Oh wow, I just noticed that I took the 100th view of this post. XD but thank you all for your help!