252 posts
Posted 06 December 2012 - 01:12 PM
I would like to know how to read a computer ID from a file, and use it in rednet.send(). I haven't been able to find a way to do it. Here is what I have tried.
if fs.exists("ServerID") then
f = fs.open("ServerID", "r")
Server = f.readLine()
f.close()
IsServer = true
else
IsServer = false
end
I tried this out and it returned the error: Rednet:[347]: positive number expected. This is because I wanted to use this to send stuff to the server computer by using "rednet.send(Server, "Stuff")". I'm pretty sure that f.readLine() returns a string rather than a number, and so that would be the problem. Is there a way I could get the number out of the string? Please help, thanks in advance.
1619 posts
Posted 06 December 2012 - 01:20 PM
When you read the line, it returns a string, or piece of text. Do something like this:
rednet.send(tonumber(Server),"msg")
EDIT: 300th post!
252 posts
Posted 06 December 2012 - 01:25 PM
When you read the line, it returns a string, or piece of text. Do something like this:
rednet.send(tonumber(Server),"msg")
Alright, thanks.
2005 posts
Posted 06 December 2012 - 08:22 PM
Why are you getting the computerID from a file instead of just using os.computerID()?
1548 posts
Location
That dark shadow under your bed...
Posted 07 December 2012 - 12:03 AM
this code isn't run on the server, he is storing another computer's ID in a file so the program can recall it
2005 posts
Posted 07 December 2012 - 12:26 AM
Oh, that makes sense. Except that the IsServer boolean suggests that it could be a server.
1548 posts
Location
That dark shadow under your bed...
Posted 07 December 2012 - 01:01 AM
I think it is just to say that a server exists or is defined… which makes no sense lol
252 posts
Posted 07 December 2012 - 11:51 AM
I am planning on making something that uses a server computer, but I don't want to have to have people who might use it edit the code… So I would have them type it in using an interface, and it stores it…
I ran into the problem with "Positive number expected". That is why I came here.
Also, the IsServer is so it can define Server, without running into an error before the user is able to define it using the interface. If IsServer is false, it wouldn't use any rednet commands.
1548 posts
Location
That dark shadow under your bed...
Posted 07 December 2012 - 11:57 AM
why don't you just say if server~=nil then? that way it checks if 'server' is defined?
252 posts
Posted 07 December 2012 - 01:22 PM
why don't you just say if server~=nil then? that way it checks if 'server' is defined?
Well.. I didn't know about that, but either way works just as well :P/>
1548 posts
Location
That dark shadow under your bed...
Posted 08 December 2012 - 02:44 AM
It is quite useful to be able to check if a variable is defined, when you make a function you should not let it run if important variables are missing, it makes catching errors very easy
2217 posts
Location
3232235883
Posted 08 December 2012 - 02:52 AM
why don't you just say if server~=nil then? that way it checks if 'server' is defined?
or just
if server then
.-.
1548 posts
Location
That dark shadow under your bed...
Posted 08 December 2012 - 02:55 AM
why don't you just say if server~=nil then? that way it checks if 'server' is defined?
or just
if server then
.-.
that works perfectly too. just note that if server==false then it will also not execute, in some cases that can cause issues