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

prevening code from exiting on a loadstring lua error

Started by PixelToast, 11 May 2012 - 05:19 PM
PixelToast #1
Posted 11 May 2012 - 07:19 PM
Spoiler
<br>shell.run("clear")<br>print("computer id is: "..os.getComputerID())<br>write("modem direction: ")<br>side=read()<br>rednet.open(side)<br>write("server id: ")<br>server=tonumber(read())<br>while true do<br>&amp;nbsp;&amp;nbsp;id,data=rednet.receive()<br>&amp;nbsp;&amp;nbsp;print("received '"..data.."' from id: '"..id.."' correct id: '"..server.."'")<br>&amp;nbsp;&amp;nbsp;if id == server then<br>&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;print("executing")<br>send=assert(loadstring(data))()<br>print("returning: '"..send.."'")<br>rednet.send(server,send)<br>&amp;nbsp;&amp;nbsp;end<br>end
<br>what am i doing wrong?<br><br>i can get the client to run lua commands via the lua command rednet.broadcast("turtle.forward()") but it will always send back nil for some reason<br>i managed to get it error free once but i started messing around with it after it kept returning nil<br>error is: "client:16: attempt to concatenate nil and string"
new problem, how do i make it so it wont exit on a lua error, instead make it return the error as a string
MysticT #2
Posted 11 May 2012 - 07:36 PM
I don't see any error in that code, so it might be the received code what's wrong. What are you sending from the server?
Do you get any error? if you do, what is it?
PixelToast #3
Posted 11 May 2012 - 07:43 PM
updated OP
Dr_Zarreh #4
Posted 11 May 2012 - 07:48 PM
I get an error on the 13 line, "=" expected
PixelToast #5
Posted 11 May 2012 - 07:52 PM
I get an error on the 13 line, "=" expected
i dont
MysticT #6
Posted 11 May 2012 - 07:53 PM
There's no error, just that you aren't doing a return in the function (the loaded function). You can add the return on the server or the client, depending on what's the purpose of this. On the client side you can add it by concatenating "return " with the received data:

data = "return "..data
but it won't work if you send more complex code, so it's better to do it on the server (just send "return turtle.forward").
PixelToast #7
Posted 11 May 2012 - 07:54 PM
facepalm, forgot that it loads the string as an actual function and dosent just make a new command
PixelToast #8
Posted 11 May 2012 - 07:57 PM
-snip-
PixelToast #9
Posted 11 May 2012 - 08:01 PM
new problem
MysticT #10
Posted 11 May 2012 - 08:03 PM
Use pcall:

local func, err = loadstring(data)
if func then
  local ok, err = pcall(func)
  if not ok then
	print("Error: "..err)
  end
else
  print("Error loading function: "..err)
end
PixelToast #11
Posted 11 May 2012 - 08:41 PM
finished code:

– by infinikiller64 aka abc on computercraft.info
– you can use this code without permission and do not have to give credit
shell.run("clear") print("computer id is: "..os.getComputerID()) write("modem direction: ") rednet.open(read()) write("server id: ") server=tonumber(read()) function run(string) local func, err = loadstring(string) if func then local ok, err = pcall(func) if not ok then return "Error: "..err end return err else return "Error loading function: "..err end end while true do id,data=rednet.receive() print("received '"..data.."' from id: '"..id.."' correct id: '"..server.."'") if id == server then print("executing") send=tostring(run(data)) print("returning: '"..send.."'") rednet.send(server,send) end end
EDIT: lol forgot to add rednet.open(side), its fixed now
Lyqyd #12
Posted 12 May 2012 - 04:49 PM
So, what's it supposed to do? Appears to load an arbitrary string it gets from another computer and send the results back.
PixelToast #13
Posted 14 May 2012 - 12:07 AM
yes :P/>/>
im still working on it too, here is my current version
client:
Spoiler
-- by infinikiller64 aka abc on computercraft.info
-- you can use this code without permission and do not have to give credit
shell.run("clear")
print("computer id is: "..os.getComputerID())
write("modem direction: ") side=read()
rednet.open(side)
write("server id: ")
s=read()
if s == "?" then
  broadcast=true
  server=nil
else
  broadcast=false
  server=tonumber(s)
end
function run(string)
  local func,err= loadstring(string)
  if func then
	local ok,err=pcall(func)
	if not ok then
return "Error: "..err
end
return err
  else
	return "Error loading function: "..err
  end
end
while true do
  id,data=rednet.receive()
  print("received '"..tostring(data).."' from id: '"..tostring(id).."' correct id: '"..tostring(server).."'")
  if id == server or broadcast == true then
	print("executing")
	send=tostring(run(data))
	print("returning: '"..send.."'")
if broadcast == false then
	  rednet.send(server,send)
else
rednet.broadcast(send)
end
  end
end
server:
Spoiler
-- by infinikiller64 aka abc on computercraft.info
-- you can use this code without permission and do not have to give credit
shell.run("clear")
print("computer id is: "..os.getComputerID())
write("modem direction: ")
rednet.open(read())
write("client id: ")
client=read()
if client=="?" then
  broadcast=true
else
  broadcast=false
  client=tonumber(client)
end
local tCommandHistory={}
while true do
  write("> ")
  local s = read(nil,tCommandHistory )
  table.insert(tCommandHistory,s)
  if broadcast==false then
	rednet.send(client,s)
  else
	rednet.broadcast(s)
  end
  corectid=0
  while corectid == 0 do
	corectid=1
	id,data=rednet.receive()
	if id ~= client and id ~= nil and broadcast == false then
	  corectid=0
	end
  end
  print(tostring(data))
end
you can enter "?" if you want to go to broadcast mode and it has a command history too!