Jump to content

A little help needed


Recommended Posts

I am not pro or even close, maybe someone could tell me whats wrong with this code? or maybe even write it correctly and show me the difference.Trying to send a signal from one computer to another using rednet to turn on redstone machines. Searched the internet for hours looking for an answer but nothing.

rednet.open ("top")

local event,p1,p2,p3 = os.pullEvent()

if event=="rednet_message" and p1==16 then

rednet.send(p1,p2,p3"ack")

if p2 == "m1 on" then

redset left false

elseif p2 == "m1 off" then

redset left true

elseif p2 == "m2 on" then

redset left false

elseif p2 == "m2 off" then

redset left true

end

end

end

this code would be on the receiving end of the command hooked to the redstone/wire

Link to comment
Share on other sites

not sure what you've got going on here but here is are basic server/client scripts

the client code:

rednet.open("top")

print("Looking for a server...")

while ID == nil do

  rednet.announce()  --sends an empty rednet message so server can get its ID

  ID, message, distance = rednet.receive(5)  --5 is the timeout in seconds

end

print("Server replied!")

print("ID: "..ID)

print("Message: "..message)

print("distance:  "..distance)

rednet.send(ID, "Found you!")  --To send a message back to the server

rs.setOutput("back", true) --Or to change the redstone state of a side


 

the server code:


rednet.open("top") -- parameter is the side with the modem

while true do

  print("Waiting for a client...")

  ID, message, distance = rednet.receive()  --no parameter means wait forever

 

  if ID ~= nil then  --nil ID might be returned if you have a timeout on rednet.receive()

    print("Found a client!")

    print("ID: "..ID)

    print("Message: "..message)

    print("distance:  "..distance)

    rednet.send(ID, "Hello")  --To send a message back to the client

  end

end

I hope this helps

Link to comment
Share on other sites

thanks, still a little confused. I wanna send from my main compter down to where the "client" computer is say like "m1 on" it will turn on machine 1 and "m1 off" it will turn it off. I have 7 machines so I could do the bundled cable but not sure if what you wrote would work for that?

Link to comment
Share on other sites

It was just a general example of syntax

You can modify the server code to send the message "m1 on" and put something like this on the client

if message == "m1 on" then

  -- turn the first machine on

else if message == "m1 off" then

  -- turn the first machine off

end


 

Bundled cable is more complicated than standard redstone signals but here's a basic example


rs.setBundledOutput("left", colors.white+colors.blue+colors.green) --power the white, blue, and green wires of a bundled cable attached to the left side (this will turn off any other colors)

if rs.getBundledOutput("left") == colors.white then

  print("The white cable is powered")

end

rs.setBundledOutput("left", 0) --turn all wires off


 

My advice would be to put the client into a loop and wait for server messages indefinitely


local activeColors = 0  --this will be a numeric representation of all the colors currently powered

while true do

  ID, message, distance = rednet.receive()

  if message == "m1 on" then

    if colors.test(activeColors, colors.blue) == false then  --if the blue cable is off, you need this check so activeColors doesn't get messed up by adding a color that's already on

      activeColors = activeColors + colors.blue  --to remember blue as on for later

      rs.setBundledOutput("left", activeColors) -- to power it (this will keep all the other colors powered if you follow the same format for each machine)

    end

  end

  if message == "m1 off" then

    if colors.test(activeColors, colors.blue) then --if the blue cable is on

      activeColors = activeColors - colors.blue

      rs.setBundledOutput("left", activeColors)

    end

  end

  -- put similar if statements for all the other machines, just changing the colors.xxxx and "mx on/off"

end

Im writing this on my break between classes so if there's an error I'm sorry I couldn't get on minecraft to test it

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...