Jump to content




RedNet


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

#1 sleawnis

  • Members
  • 29 posts

Posted 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 :)

#2 Sirharry0077

  • Members
  • 23 posts

Posted 25 March 2013 - 04:23 AM

Computer
rednet.open("side")
rednet.send(id, "dig")

Turtle
rednet.open("side")
Id, msg = rednet.recieve()

shell.run(msg)





#3 PonyKuu

  • Members
  • 215 posts

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.
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


#4 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

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:
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.

#5 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 25 March 2013 - 06:39 PM

View PostBubba, on 25 March 2013 - 06:36 PM, said:

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?

#6 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 25 March 2013 - 06:47 PM

View PostremiX, on 25 March 2013 - 06:39 PM, said:

View PostBubba, on 25 March 2013 - 06:36 PM, said:

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

#7 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

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

#8 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

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 :)

#9 PonyKuu

  • Members
  • 215 posts

Posted 25 March 2013 - 08:04 PM

View PostBubba, on 25 March 2013 - 06:36 PM, said:

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.
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...

#10 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 25 March 2013 - 08:53 PM

View PostPonyKuu, on 25 March 2013 - 08:04 PM, said:

View PostBubba, on 25 March 2013 - 06:36 PM, said:

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.
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...

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.

#11 PonyKuu

  • Members
  • 215 posts

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.

#12 Altair357

  • Members
  • 6 posts

Posted 28 March 2013 - 04:46 PM

View PostPonyKuu, on 25 March 2013 - 09:53 PM, said:

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.

#13 PonyKuu

  • Members
  • 215 posts

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
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...

#14 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

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


View PostPonyKuu, on 28 March 2013 - 08:18 PM, said:

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.
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.

Edited by TheOriginalBIT, 28 March 2013 - 08:34 PM.


#15 PonyKuu

  • Members
  • 215 posts

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...

View PostTheOriginalBIT, on 28 March 2013 - 08:24 PM, said:

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....

#16 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 28 March 2013 - 08:56 PM

View PostPonyKuu, on 28 March 2013 - 08:44 PM, said:

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...
Exactly

View PostPonyKuu, on 28 March 2013 - 08:44 PM, said:

View PostTheOriginalBIT, on 28 March 2013 - 08:24 PM, said:

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....
500-string APIs?
Ok if you can get my any of my apis and reduce their size down 66% I will concede my point.

#17 PonyKuu

  • Members
  • 215 posts

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 ~_~

#18 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users