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

Program Immediately Closes Without Error

Started by Kingdaro, 03 February 2013 - 04:05 AM
Kingdaro #1
Posted 03 February 2013 - 05:05 AM
I'm attempting to make a program that rewrites the write and print functions to send whatever is written to the screen as rednet signals to a specific computer. When I run the program, it simply closes, and the computer shuts down, no error.


local args = {...}
if not args[1] then
  print 'Usage: redir-print <receiver id>'
  return
end

local recipient = tonumber(args[1])
local w = write

function write(text)
  rednet.open 'right'
  rednet.send(recipient, text)
  rednet.close 'right'

  w(text)
end

function print(...)
  write(table.concat{...})
  w '\n'
end

term.write = write

I'm thinking I might've missed something glaringly obvious, but I can't manage to catch the problem here.
drats666 #2
Posted 03 February 2013 - 06:01 AM
i may be wrong but could it be that your issue is that you are declaring your variable as local? i am not 100% sure that a function can read local variables declared outside of itself.
http://www.lua.org/pil/4.2.html has more information about lua, the language computer craft uses.
tesla1889 #3
Posted 03 February 2013 - 06:10 AM
functions have access to all variables declared outside and inside of their scope, meaning that unless the variables were declared in a different scope

do


local var1 = 1
local var2 = 2

end

local var3 = 3

local fn = function()

print(var1) -- will error: nil value
print(var2) -- will error: nil value
print(var3) -- will not error


end
the function has access to them
tesla1889 #4
Posted 03 February 2013 - 06:17 AM
–snip–

Lua 4.2 is waaaaaaay outdated

use Lua 5 (either 5.1 or 5.2)
propeng #5
Posted 03 February 2013 - 06:23 AM
Seems to be working fine for me. I did get that same behavior though when I used the wrong computer ID. Are you sure you're using the correct receiver ID obtained through os.computerID()?
drats666 #6
Posted 03 February 2013 - 06:27 AM
hmm not sure if that is the full code, i doubt it. but try modifying the text to print the variables as it proceeds down the script to find out where it is crashing on?
Kingdaro #7
Posted 03 February 2013 - 06:36 AM
Seems to be working fine for me. I did get that same behavior though when I used the wrong computer ID. Are you sure you're using the correct receiver ID obtained through os.computerID()?
My main computer is ID 0, so yes. I just tested it with 0 and it does the same thing.

It's weird, because the world I'm playing on is a world from my server, and it works fine on the server.

When I tried it again, I caught a glimpse of some text before it blacked out, something having to do with bios.lua and CC's installation. I'll try reinstalling CC.
1lann #8
Posted 03 February 2013 - 07:47 AM
When overriding term.write, it stuffs things up. Try it yourself by going to the lua prompt, then doing
term.write = write
Kingdaro #9
Posted 03 February 2013 - 08:10 AM
I suppose I should just not overwrite term.write then. I'll try that when I get home, thanks.
propeng #10
Posted 03 February 2013 - 09:35 AM
I don't really know if this would make much of a difference, but perhaps you should try using os.run to pass in your function as an environment variable, instead of overriding it directly. This would also allow you to just terminate the program if you want to return back to the default print/write functions.
Kingdaro #11
Posted 03 February 2013 - 09:49 AM
I don't really know if this would make much of a difference, but perhaps you should try using os.run to pass in your function as an environment variable, instead of overriding it directly. This would also allow you to just terminate the program if you want to return back to the default print/write functions.
Good idea, I might try that too.
tesla1889 #12
Posted 03 February 2013 - 10:03 AM
solution:

w = term.write
instead of:

w = write

write just references term.write, so define w as term.write instead
1lann #13
Posted 03 February 2013 - 01:45 PM
solution:

w = term.write
instead of:

w = write

write just references term.write, so define w as term.write instead
Actually (according to gravityscore) write does text wrapping, whereas term.write doesn't.
tesla1889 #14
Posted 03 February 2013 - 07:28 PM
–snipitty snip–
Actually (according to gravityscore) write does text wrapping, whereas term.write doesn't.

oh, yeah, forgot about that. well, you may have to copypasta the code for write to your new write
Grim Reaper #15
Posted 03 February 2013 - 07:52 PM
You're replacing 'term.write' with your personally written 'write' function which uses the native 'write' function defined in the bios. However, the original 'write' function uses 'term.write' and thus causes a recursive stack overflow and the termination of your computer's lua thread (at least, that's what I think is happening).

A simple adjustment to solve this problem might be:


local args = {...}
if not args[1] then
  print 'Usage: redir-print <receiver id>'
  return
end

local recipient = tonumber(args[1])
local old_termWrite = _G["term"]["write"] or term.write


term.write = function(text)
	rednet.open("right")
	rednet.send(recipient, text)
	rednet.close("right")

	old_termWrite(text)
end