This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
RedNet
Started by sleawnis, 25 March 2013 - 01:30 AMPosted 25 March 2013 - 02:30 AM
I have this program on my main computer that sends out a wireless signal. I also have one on my turtle that receves it and prints the message. How to i let it receve a message , say : Dig and then it runs the dig program ? thanks :)/>
Posted 25 March 2013 - 04:23 AM
Computer
Turtle
rednet.open("side")
rednet.send(id, "dig")
Turtle
rednet.open("side")
Id, msg = rednet.recieve()
shell.run(msg)
Posted 25 March 2013 - 05:42 PM
… And then someone sends something destructive via rednet…
It is better to use some functions inside the program instead of using shell.run() - that's just a bad style, I think. And if you do use the shell.run() at least check the message you recieved.
It is better to use some functions inside the program instead of using shell.run() - that's just a bad style, I think. And if you do use the shell.run() at least check the message you recieved.
rednet.open("side")
while true do
Id, msg = rednet.recieve()
if msg == "Dig" or msg == "Don't dig", or msg == "Whatever you want" then
shell.run(msg)
end
end
Posted 25 March 2013 - 06:36 PM
I know that the OP said he wanted to run the dig program, but in case you want to run a piece of code instead (say turtle.dig) you can do this:
To run code in string form, you'd have to use loadstring.
Example:
Also there is not too much danger of using shell.run directly from the rednet because you will only be able to run programs which have already been created.
To run code in string form, you'd have to use loadstring.
Example:
local example = "print('hello, this has been printed by loading a string!')"
local runtime = loadstring(example)
runtime()
Also there is not too much danger of using shell.run directly from the rednet because you will only be able to run programs which have already been created.
Posted 25 March 2013 - 06:39 PM
I know that the OP said he wanted to run the dig program, but in case you want to run a piece of code instead (say turtle.dig) you can do this:
To run code in string form, you'd have to use loadstring.
Example:local example = "print('hello, this has been printed by loading a string!')" local runtime = loadstring(example) runtime()
Is there a way to check if the sent message is actually a valid function to call?
Posted 25 March 2013 - 06:47 PM
I know that the OP said he wanted to run the dig program, but in case you want to run a piece of code instead (say turtle.dig) you can do this:
To run code in string form, you'd have to use loadstring.
Example:local example = "print('hello, this has been printed by loading a string!')" local runtime = loadstring(example) runtime()
Is there a way to check if the sent message is actually a valid function to call?
Yup, just use pcall(loaded func). Pcall will execute the code, but it won't stop the program if it's invalid
Example:
local invalid = "notafunc()"
local loaded=loadstring(invalid)
local success, catch = pcall(loaded)
print("Success: "..success)
print("Errors (if any): "..catch)
Output:
Success: false
Errors (if any): string:1: attempt to call nil
Posted 25 March 2013 - 06:51 PM
Oh yeah forgot about pcall :P/>
But I think that code would error on the second last line because you can't concatenate string and a boolean :P/>
Going to need to tostring it I think xD
But I think that code would error on the second last line because you can't concatenate string and a boolean :P/>
Going to need to tostring it I think xD
Posted 25 March 2013 - 06:54 PM
Huh… I though lua had coercion though. Maybe it's a LuaJ thing :/
Edit: Oops nevermind. That's just for strings and numbers, not strings and booleans. Right you are, RemiX :)/>
Edit: Oops nevermind. That's just for strings and numbers, not strings and booleans. Right you are, RemiX :)/>
Posted 25 March 2013 - 08:04 PM
It's not just a danger or something… What if it receives a random broadcast such as "Hi!" or other irrelevant stuff? And, honestly, I think shell.run() should be avoided at all. It's up to you of course, but, meh…Also there is not too much danger of using shell.run directly from the rednet because you will only be able to run programs which have already been created.
Posted 25 March 2013 - 08:53 PM
It's not just a danger or something… What if it receives a random broadcast such as "Hi!" or other irrelevant stuff? And, honestly, I think shell.run() should be avoided at all. It's up to you of course, but, meh…Also there is not too much danger of using shell.run directly from the rednet because you will only be able to run programs which have already been created.
That's what pcall is for. And you think that shell.run should be avoided in all situations? I feel like shell.run does have its uses, although many people use it overly much or in the wrong way.
Posted 25 March 2013 - 09:53 PM
I just try to avoid using it. It's OK for a startup script, but if I want my program to do something, I'll use a function or an API. For example, Dire, I think, uses shell.run() too much.
Posted 28 March 2013 - 04:46 PM
I just try to avoid using it. It's OK for a startup script, but if I want my program to do something, I'll use a function or an API. For example, Dire, I think, uses shell.run() too much.
For what he's doing, it's fine. shell.run() is an easy and effective way to handle a program from within another program, like he did with his frame quarry in season 5.
Posted 28 March 2013 - 08:18 PM
I don't know. Honestly I don't like his coding style at all. For example, he used shell.run() to call his "goto" program on PahiCraft… That's not the way how you do it. I'd rather make a goto API and use it in my program - like I've done with my turtle fleet. He makes programs that work only in specific case and so on. Even with his frame quarry, what is the problem inserting a few strings into the main program? It's like
Also, his master computer doesn't care about what is going on there. It waits 30 seconds and repeats the cycle. So it is possible that miners would mine the same place again. Under certain circumstanses of course, but that's a bit inaccurate… I like his idea of using CC for small utility programs, I just don't like the style…
function make_hole_incredibly_fast ()
<place miner>
<wait some seconds>
<dump stuff>
end
while true do
local message = <wait for message>
if message == "I need a hole! NOW!" then
make_hole_incredibly_fast ()
end
end
Also, his turtles dump the stuff to a chest only from first 10 slots. Why he ignores other ones? It is possible to find 11 or more different types of blocks in one column, so some of them would stay in turtle.Also, his master computer doesn't care about what is going on there. It waits 30 seconds and repeats the cycle. So it is possible that miners would mine the same place again. Under certain circumstanses of course, but that's a bit inaccurate… I like his idea of using CC for small utility programs, I just don't like the style…
Posted 28 March 2013 - 08:24 PM
Direwolf20 is not a good coder, he does MANY inefficient and bad things with his code. He does not know most of the ComputerCraft and Lua functions at his disposal. People need to stop treating him as some kind of idol and programming god, the amount of people that struggle to understand his extremely verbose code is ridiculous, most people just seems to copy/paste, then come here when they have even the slightest problem.
EDIT: And when I say verbose I mean, a ~100 line script that could be redone in a max of ~30 lines
EDIT: And when I say verbose I mean, a ~100 line script that could be redone in a max of ~30 lines
There is only one advantage to doing it this way………. you can run the goto program independently of the rest without having to make a program that calls the api with one line meaning 2 programs. but i do agree with you, I prefer apis.For example, he used shell.run() to call his "goto" program on PahiCraft… That's not the way how you do it. I'd rather make a goto API and use it in my program - like I've done with my turtle fleet.
Edited on 28 March 2013 - 07:34 PM
Posted 28 March 2013 - 08:44 PM
Yeah, you are right. But actually, that doesn't mean that he is a bad minecraft engineer… He makes a lot of interesting stuff with all the mods he has and computers/turtles are just tools for him, like, for example, RedPower logic gates or blockbreakers. He doesn't use CC to it's full potential, but that's compensated by other mods and so on… And his mixed CC-BC-EnderChest mining solution is much faster than my pure CC (except for Ender Chests) Swarm, even though his code is not the best one…
*Kuu looks at his 500-string APIs*
Le sigh….
EDIT: And when I say verbose I mean, a ~100 line script that could be redone in a max of ~30 lines
*Kuu looks at his 500-string APIs*
Le sigh….
Posted 28 March 2013 - 08:56 PM
ExactlyYeah, you are right. But actually, that doesn't mean that he is a bad minecraft engineer… He makes a lot of interesting stuff with all the mods he has and computers/turtles are just tools for him, like, for example, RedPower logic gates or blockbreakers. He doesn't use CC to it's full potential, but that's compensated by other mods and so on… And his mixed CC-BC-EnderChest mining solution is much faster than my pure CC (except for Ender Chests) Swarm, even though his code is not the best one…
500-string APIs?EDIT: And when I say verbose I mean, a ~100 line script that could be redone in a max of ~30 lines
*Kuu looks at his 500-string APIs*
Le sigh….
Ok if you can get my any of my apis and reduce their size down 66% I will concede my point.
Posted 28 March 2013 - 09:05 PM
I'm talking about my APIs, not yours ^_^/>'
I'm not sure if I it is possible to shrink them down… It is possible, I think, I just don't see how to do that. But if I remove ll the comments they would be a bit smaller… Or maybe a lot smaller ~_~
I'm not sure if I it is possible to shrink them down… It is possible, I think, I just don't see how to do that. But if I remove ll the comments they would be a bit smaller… Or maybe a lot smaller ~_~
Posted 28 March 2013 - 09:14 PM
oh, lol, I read that as "k u" not 'kuu' oops.
if you are talking about the master/module, iirc there were some improvements that could be made. pm me to discuss more, lets not clog up this thread with more off topic stuffz.
if you are talking about the master/module, iirc there were some improvements that could be made. pm me to discuss more, lets not clog up this thread with more off topic stuffz.