106 posts
Location
Texas (USA)
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
1281 posts
Posted 23 March 2014 - 01:59 AM
use dostring, it loads a string as a piece of code and executes it.
dostring(msg)
2151 posts
Location
Auckland, New Zealand
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.
7508 posts
Location
Australia
Posted 23 March 2014 - 04:45 AM
use dostring
dostring? loadstring maybe?
2151 posts
Location
Auckland, New Zealand
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
11 posts
Location
Boston
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
1281 posts
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.
11 posts
Location
Boston
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.
8543 posts
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.
1281 posts
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.
1610 posts
Posted 24 March 2014 - 02:20 PM
Oh right, there isn't a dostring is there, only dostring.
dostring ~= dostring? :P/>
2151 posts
Location
Auckland, New Zealand
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.
106 posts
Location
Texas (USA)
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!