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

[Question][LUA]

Started by TheWeeIrish, 05 April 2012 - 01:43 AM
TheWeeIrish #1
Posted 05 April 2012 - 03:43 AM
So, I've been writing up an elevator code with bundlecables and railcraft and it goes to five floors currently. There currently is a computer on each floor (all different colors), I can control all the floors with one computer. If I go to another floor and try to call the elevator the minecart stays on the last floor it was use on with the last computer. In short, its not clearing the rail power to reset to the new floor.

This is the LUA code, its sloppy but decent for my second script I'd say… http://pastebin.com/1gM9akVj

If you need anything cleared up please ask, its hard to explain/it makes sense to me.

Thanks!

PS. I messed up the topic, my apologies.
Advert #2
Posted 05 April 2012 - 04:55 AM
You might want to give your topic a title.

I'm not 100% sure how you have this set up, but I'm assuming something like this:


C1 -> [bundle]
C2 -> [bundle]
...
C5 -> [bundle]

F1 <- [bundle]
F2 <- [bundle]
...
F5 <- [bundle]

You can check if another color is on, and turn off the output, or use one computer to deal with the bundle, and connect it to the others with rednet.

Both methods will require the use of os.pullEvent, which you can read about by typing help events and help os; you can find examples around the forum.

Some notes on your code:
You are doing a few things that make it hard to read:
1) Pass what could be arguments around in the global table

function menu()
...
        user=io.read()
...
end

function menuselect()
...
	if user == "1" then
...
end
2) Using globals everywhere (this doesn't make it hard to read, but makes it more likely to introduce bugs)
3) You're using shell.run("menu"), from what I assume you're trying to do is just start the program again.
You can use return menusleect() to do a tail call – this will not increase the stack, and your program won't crash.
4) Your code has a lot of repetition, you could use table(s) to store the data associated with the possible menu options, and what color to set the redstone output to.
TheWeeIrish #3
Posted 05 April 2012 - 08:40 AM
My set up is elevator rails going up 5 floors, behind the rail is a bundled cable with the floor colored cable sticking around the side to charge the rail for the floor. Along with a computer on each floor to have access to the elevator.

I am just trying to figure out if its possible to have each computer remove all current outputs on the bundle cable line that other computers put into it. For example, use floor 3 computer to go to floor 4, then have the floor 4 go down to floor 1. Whenever I try this the cable doesn't reset and go down to floor one it stays at floor 4.

I will look into pullEvents and such, I haven't really poked around with to complex of scripting yet… trying to wrap my head around it still.

Thank you for pointing things out of my poor scripting attempt, never taken a class just going off what I see others doing. Not really sure about global tables or how they work, but Ill poke around and the return menuSelect() worked! :)/>/>

If this doesn't work out I will just use rednet, thought I'd try this way first didn't know that it would be an issue turning off the bundled cable from another computer.

Thanks!
Luanub #4
Posted 05 April 2012 - 08:48 AM
You should be able to do what you are wanting to do. You will probably have to setup a "server" that keeps track of the current state and updates the computers on each floor(keep them all in sync). I had to do something similar in one of my scripts and ended up having to using some reverse logic to get it to function properly. When I get home I'll take a look at my script to jog my memory on how I did it and see if I can't give you an example.
TheWeeIrish #5
Posted 05 April 2012 - 08:51 AM
That would be amazing, examples are my bestests friends.
Luanub #6
Posted 05 April 2012 - 09:49 AM
Okay here is what I used for my harvesting setup. Basically the client would send a rednet message telling the server what it wanted to do, the server then acted upon the request and sends out a broadcast to all computers telling them what was done.

Both of these are ran in a while true loop. There's allot more to my code, the menu's etc.. But that's stuff you will want to do in your own style, this should give you an idea on how to handle the server/client communications while keeping all of the systems in sync.

Server

local event, par1, par2 = os.pullEvent("rednet_message")
    if par2 == "harvest1on" then
        c = colors.combine(c, colors.red )
        rs.setBundledOutput(sSide, c )
        sleep (0.25)
        rednet.broadcast( "harvest1on" )
    elseif par2 == "harvest1off" then
        c = colors.subtract(c, colors.red )
        rs.setBundledOutput(sSide, c )
        sleep (0.25)
        rednet.broadcast( "harvest1off" )
end


Clients

local event, k1, k2 = os.pullEvent()
if event == "key" then
    if k1 == 2 or k1 == 79 then
        term.clear()
        term.setCursorPos(1,7)
        textutils.slowPrint "Turning Tunnel #1 Harvester On..."
        sleep (1)
        rednet.send( 14 , "harvest1on" )
    elseif k1 == 3 or k1 == 80 then
          term.clear()
        term.setCursorPos(1,7)
        textutils.slowPrint "Turning Tunnel #1 Harvester Off..."
        sleep (2)
        rednet.send( 14 , "harvest1off" )
    end
elseif event == "rednet_message" then
    if k2 == "harvest1on" then
        harv1 = ("true")  -- used in the status display in my menu
    elseif k2 == "harvest1off" then
        harv1 = ("false")  -- used in the status display in my menu
    end
end
Advert #7
Posted 05 April 2012 - 10:40 AM
I've made an example, too! Have fun with it.

Spoiler
TheWeeIrish #8
Posted 05 April 2012 - 11:09 AM
My good sir, you are a wizard.

Thanks to both of you, I will be smashing my face into this until I understand.

For the most part I get your script Advert but, my lack of knowledge on terms kind of hinders me from knowing what the frick is going on at all times.
I will definetly be looking into this!


Also, why did they add a modem range?! I got everything working with another script, but it can't reach it! :'(
Luanub #9
Posted 05 April 2012 - 11:25 AM
You can change the range of your modems in the CC config file
TheWeeIrish #10
Posted 05 April 2012 - 11:44 AM
Im in the config file and it only shows me block ID's im on tekkit bundle so I more than likely have an older version that doesn't have it yet.

Or I am just blind.
Luanub #11
Posted 05 April 2012 - 08:22 PM
Ya if your using tekkit there is a pretty high chance you are running an older version and dont have that option yet.
TheWeeIrish #12
Posted 05 April 2012 - 11:11 PM
Yea its version 1.3, I think it came out in 1.31. Any clue how to build a repeater? People in the forums say they were posted on here but when I searched for them I could only find one that didn't work along with people saying that there are more on the forums.

Edit: Alright, everything seems to work now.. thanks everyone :)/>/>
Luanub #13
Posted 06 April 2012 - 12:21 PM
For the repeater check out Cookieballs Skynet API http://www.computercraft.info/forums2/index.php?/topic/20-cookiebals-programs-cookiesystem-turtle-advanced-movement-skynet-networking-project-world-eater-and-a-lot-more/