Running a line of code from a rednet msg.
Started by Sillyjake, Mar 23 2014 01:49 AM
12 replies to this topic
#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
while true do
local msg = rednet.receive()
RunThisLineHere(msg)
end
#2
Posted 23 March 2014 - 01:59 AM
use dostring, it loads a string as a piece of code and executes it.
dostring(msg)
#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.
#5
Posted 23 March 2014 - 04:56 AM
theoriginalbit, on 23 March 2014 - 04:45 AM, said:
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 by oeed, 24 March 2014 - 08:50 PM.
#6
Posted 23 March 2014 - 10:27 PM
Personally, i just use the shell.run function:
If security is your thing, I'd also include an ID check to consider where the msg is coming from:
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
#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.
shell.run does not execute code, and as such it would not do what the op asked for.
#9
Posted 23 March 2014 - 10:41 PM
If I recall correctly, the loadstring trick needs another set of parentheses to actually work:
It's been a while since I've done anything with it, though.
(loadstring())()
It's been a while since I've done anything with it, though.
#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.
#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!
2 user(s) are reading this topic
0 members, 2 guests, 0 anonymous users











