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

Rednet_Message Events.

Started by tehdetox, 06 February 2012 - 12:44 PM
tehdetox #1
Posted 06 February 2012 - 01:44 PM
Looking through the help of the rednet API, I noticed a VERY brief description of using the rednet_message event.

I feel there is not enough information for new comers to ComputerCraft (or LUA for that matter) on that.

It says that you can use it to be able to avoid using the recieve() function. How could I use it? Is it already written and have to be called? Is rednet_message() the correct syntax?

Thank you
Casper7526 #2
Posted 06 February 2012 - 01:48 PM
The rednet_message event is an event type (help events) that can be used within the pullEvent() function.

The rednet_message event returns the ID and message as arguments.

Sender
– Open Port
– Send Data

Receiver
– Open Port

while true do
event, param1, param2 = os.pullEvent()
print ("Event = "..event)
print ("Param1 = "..param1)
print ("Param2 = "..param2)
end
You'll see exactly what is happening everytime you send a message if you start the receiver program first.

Let me know if this is a bit too general and you need more help.
tehdetox #3
Posted 06 February 2012 - 02:09 PM
I think that's a little too generalised for my tastes, I'm no programmer/scripter by any means, just someone who is trying his hardest to pick it up as he goes.

Below is a brief explanation as to what I'm doing to be able to make sense of anything I may describe or discuss;

The image shows an LED display (lamps from RedPower2) set up in an old school LCD figure of eight number display.



The first terminal at ground level only sends data. The second receives the data and routes the power to where it needs to go. The third terminal is exactly the same as the second so needs to explanation.

I need the second terminal to permanently await instruction using the rednet_message events, then using a series of IF commands to route the power output to where it needs to go. That's my logic of making it work anyhow! lol

Using this code:


rednet.open("back")

write("on?")
message1 = read()

if message1 == "on" then
redstone.setOutput("back", true)
rednet.announce()
rednet.send("14", message1)
else
print("wrong command")
end

write("off?")
message2 = read()

if message2 == "off" then
redstone.setOutput("back", false)
rednet.announce()
rednet.send("14", message2)
else
print("wrong command")
end
rednet.close("back")

What would be the appropriate receive script to make sure it works without a hitch?
Espen #4
Posted 06 February 2012 - 03:37 PM
I see you're using only red wire alloy. But if you're going to use redpower you could make your life much easier.
You can use the "bundled cable" instead of the "red wire alloy". The former can send 16 bits of information, whereas the latter can only send 1 bit.
So you'd only need to put "bundled cable" on one side of a computer.
You then connect colored cables from all 7 segments of your display to the bundled cable.
Each color encodes a specific bit, e.g. white-colored cable is '1', orange-colored cable is '2', and so on.
But you don't even have to know the exact number for each of the cables, because instead of typing their numbers directly you can also just use e.g. "colors.white" to access the number for the white-colored cable.

If you've setup your system like that, then you can activate colored cables like this:
redstone.setBundledCable( "back", colors.white )
This will set the bundled cable at the back of your computer to '1'.
redstone.setBundledCable( "back", colors.orange )
This will set the bundled cable at the back of your computer to '2'.
And so on…

To turn off all signals you just set the bundled cable output to 0:
redstone.setBundledCable( "back", 0 )

I hope this was clear enough. If there's any confusion though, ask away! ;)/>/>
tehdetox #5
Posted 06 February 2012 - 07:42 PM
Thanks for the comprehensive post ;)/>/>

Helped a lot. Tbh i already knew about the bundled cables but found i quite daunting without even having a look into it.

I decided to use red wire as i went for the first idea that came to mind without fully thinking any other way through.

I wanted to use multiple terminals to practice networking by using getInput but ended up finding rednet much easier to use ;)/>/>
Espen #6
Posted 06 February 2012 - 09:19 PM
No problem, glad to be of help.
One more thing I forgot in my last post:
If you want to set add or subtract specific colors for the same bundled cable, then you can make use of:
colors.combine( color1, color2, color3, ... )
and
colors.subtract( colors, color1, color2, ... )

Example:
bunkerWires = colors.combine( colors.white, colors.red, colors.green, colors.blue )   -- adds their values together
bunkerDoorsWires = ( bunkerWires, colors.red, colors.blue )   -- subtracts the values for red and blue from bunkerWires

-- Activating Bunker Security System
redstone.setBundledOutput( "back", bunkerWires )   -- cable is set to white, red, green and blue
-- Activating Bunker-Doors-Power Only ( white and green )
redstone.setBundledOutput( "back", bunkerDoorsWires )   -- cable is set to white and green, the rest is switched off

Important:
bunkerDoorsWires = ( bunkerWires, colors.red, colors.blue ) is the same like saying "bunkerDoorsWires equals bunkerWires minus the colors red and blue".
We could just as well have used: bunkerDoorsWires = colors.combine(colors.white, colors.green)
tehdetox #7
Posted 06 February 2012 - 09:49 PM
Using your explanation of the bundled cables I rebuilt my display and got to hacking out a very long winded way of getting the display to count from 1-0.

Below is the code I used to test before I hacked out hundreds of lines for nothing and it threw up the following compilation error: "attempting to call nil"


print("Counter v1.0 Activated")
--1
redstone.setBundledCable("back", colors.orange)
redstone.setBundledCable("back", colors.green)
sleep(2)
--2
redstone.setBundledCable("back", 0)
colors.combine(colors.pink, colors.orange, colors.brown, colors.red, colors.white)
sleep(2)
--3
redstone.setBundledCable("back", 0)

Before posting this reply I've gone over and over and over my code to find whats wrong before posting but comparing what you have in your replies, I cant see any difference. Tried the UK + US spelling of colours … I found some very stupid and easily missed errors I've corrected, but no matter what i change, its still attempting to call nil ;)/>/>
Espen #8
Posted 06 February 2012 - 10:01 PM
That's because your program tries to call a function that doesn't exist.
If you look carefully you wrote redstone.setBundledCable, but it should be redstone.setBundledOutput.
Something like that just happened to me today, too. I had a function names f1Key and wasn't able to call it.
Later I saw that I wrote f1key with a lowercase letter, instead of an uppercase letter. Can you imagine how long it took me to find this error? ;)/>/>

As a hint: You can also type "help redpower" for help directly form the computer.
"help index" lists all help entries.

Also: If you want to try out lua functions directly without having to write a program, you can enter "lua" and then directly enter function commands. See also "help lua"

EDIT: Just one more thing:
If you're using colors.combine() without assigning it to any variable, then it doesn't do anything for you.
Remember, the colors represent numbers and colors.combine() adds them together.
But to use the result you have to assign it to a variable, like so:
allMyColors = colors.combine(colors.pink, colors.orange, colors.brown, colors.red, colors.white)
redstone.setBundledOutput( "back", allMyColors ) 
It's like saying:
Result = 5 + 2 + 6 + 8 (not the acutal numbers for the colors above)
But what you've said is just:
5 + 2 + 6 + 8
That means the result might be calculated, but it can't be used.

K, hope this helps and you can get your program running. ;)/>/>
Edited on 06 February 2012 - 09:17 PM
tehdetox #9
Posted 06 February 2012 - 10:23 PM
haha I can imagine how long it took. I remember back in college I had to work with Pascal. The amount of times it threw up an error on line 20 when it was actually on the line before ;)/>/>

Funnily enough, I managed to figure out the redstone.setBundledOutput seconds after joining the IRC someone mentioned it ;)/>/>

The trouble I'm having now is that I have the code:


redstone.setBundledOutput("back", colors.orange, true)
redstone.setBundledOutput("back", colors.green, true)

and only the green is powered :(/>/>

Edit:

Something I forgot to ask, the colours.combine() function, does that power them also? or is that a seperate command?

would I have to store them into a variable like:


bundlecolor = colors.combine(colors.pink, colors.orange, colors.brown, colors.red, colors.white)
redstone.setBundledOutput("back", bundlecolor, true)

?

Edit Edit:

Forget about my last edit about the combined bundles, IT WORKED!! haha :P/>/>

However I still for the life of me cannot get the orange cable to power :(/>/>

FINAL EDIT!!!!!!!!

I have it all working. I still can't get the orange to power by itself, but seeing as im using two different colors within the first number I used the combine function and it works perfectly :)/>/>

For anyone interested, here is my code:



print("Counter v1.0 Activated")

--1
--redstone.setBundledOutput("back", colors.orange, true)
--redstone.setBundledOutput("back", colors.green, true)
mix = colors.combine(colors.orange, colors.green)
redstone.setBundledOutput("back", mix, true)
sleep(2)

--2
redstone.setBundledOutput("back", 0)
mix = colors.combine(colors.pink, colors.orange, colors.brown, colors.red, colors.white)
redstone.setBundledOutput("back", mix, true)
sleep(2)

--3
redstone.setBundledOutput("back", 0)
Edited on 06 February 2012 - 09:35 PM
Espen #10
Posted 06 February 2012 - 10:29 PM
That's because you shouldn't use boolean values to set bundled cables.
They are not working with them, only with the normal redstone.setOutput for the vanilla redstone dust.
You only use this:
redstone.setBundledOutput("back", colors.orange)
redstone.setBundledOutput("back", colors.green)
But this code probably doesn't do what you think it does.
It first turns the bundled cable to the value of the orange color and then switches it to the value of the green cable.
If you want both to be on at the same time you have to use something like:
result = colors.combine( colors.orange, colors.green )
redstone.setBundledOutput("back", result )
tehdetox #11
Posted 06 February 2012 - 10:49 PM
Yeah, just figured it out :)/>/>

Thanks A LOT!!!! haha

I would never have finished this little project without your wisdom :P/>/>

Then again, I say I've finished, a program is never finished. There's always code to be neatened up :P/>/>
Once I have this counting via the long winded way I shall try to cut it down using various functions :)/>/>

Once again, thanks :D/>/>
Espen #12
Posted 06 February 2012 - 10:52 PM
No problem, glad to help. Good luck with your program! :P/>/>
spysean1499 #13
Posted 16 March 2013 - 09:06 PM
dose anayone know how to send a messige through rednet throu a computers for longer range
LNETeam #14
Posted 17 March 2013 - 06:21 AM
BTW, as of 1.51, the rednet_message was removed. I'm pretty sure, stopped working in my programs. Replaced by "modem_message"
Lyqyd #15
Posted 17 March 2013 - 06:34 AM
Locked.