Jump to content




Running a line of code from a rednet msg.


  • You cannot reply to this topic
12 replies to this topic

#1 Sillyjake

  • Members
  • 92 posts
  • LocationTexas (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

#2 CometWolf

  • Members
  • 1,283 posts

Posted 23 March 2014 - 01:59 AM

use dostring, it loads a string as a piece of code and executes it.
dostring(msg)


#3 oeed

    Oversimplifier

  • Members
  • 2,095 posts
  • LocationAuckland, 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.

#4 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 23 March 2014 - 04:45 AM

View PostCometWolf, on 23 March 2014 - 01:59 AM, said:

use dostring
dostring? loadstring maybe?

#5 oeed

    Oversimplifier

  • Members
  • 2,095 posts
  • LocationAuckland, New Zealand

Posted 23 March 2014 - 04:56 AM

View Posttheoriginalbit, on 23 March 2014 - 04:45 AM, said:

View PostCometWolf, on 23 March 2014 - 01:59 AM, said:

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 by oeed, 24 March 2014 - 08:50 PM.


#6 cygnis

  • Members
  • 11 posts
  • LocationBoston

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


#7 CometWolf

  • Members
  • 1,283 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.

#8 cygnis

  • Members
  • 11 posts
  • LocationBoston

Posted 23 March 2014 - 10:38 PM

View PostCometWolf, on 23 March 2014 - 10:37 PM, said:

shell.run does not execute code, and as such it would not do what the op asked for.

doh... right you are.

#9 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 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.

#10 CometWolf

  • Members
  • 1,283 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.

#11 apemanzilla

  • Members
  • 1,421 posts

Posted 24 March 2014 - 02:20 PM

View Postoeed, on 23 March 2014 - 04:56 AM, said:

Oh right, there isn't a dostring is there, only dostring.
dostring ~= dostring? :P

#12 oeed

    Oversimplifier

  • Members
  • 2,095 posts
  • LocationAuckland, New Zealand

Posted 24 March 2014 - 08:50 PM

View PostApemanzilla, on 24 March 2014 - 02:20 PM, said:

View Postoeed, on 23 March 2014 - 04:56 AM, said:

Oh right, there isn't a dostring is there, only dostring.
dostring ~= dostring? :P
*faceplam*
I meant dofile obviously.

#13 Sillyjake

  • Members
  • 92 posts
  • LocationTexas (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!





3 user(s) are reading this topic

0 members, 3 guests, 0 anonymous users