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

I suck at LUA. need some assistance with colored wires/bundled cables

Started by D3athbysp0rk, 20 December 2015 - 09:23 AM
D3athbysp0rk #1
Posted 20 December 2015 - 10:23 AM
the system (not CC part of this system uses steve's factory manager) first checks if it's a receiving a redstone signal (let's call then channel 1) to determine it's first steps. if ch.1 is true, then it will supply a redstone signal to red. if false then it will check for more conditions. which all end up with 5 different results.

here's a picture of it. left side is the UI right side is the heart of the spawning.

key:
on/off is a redstone condition
wither spawn is a mob counter checking if there's withers in the chambers being killed.
12 skulls? is a inventory condition in which is checks if there's enough skulls to spawn 4 withers.




So now to the CC part of it. I'm using CC as a more informative UI and i plan on using bundled cables with the same color codes as above to display on a monitor with text what is occurring.

redstone cable colors
Purple = Wither (pulses on/off every 10 ticks)
red = off
green = on
yellow = out of witherskulls (pulses on/off every 10 ticks)

I'm trying to get that cordinate with colored redstone cables and bundled cables. to then display the text in the center of the screen.

this also can get complicated as multiple colors can be on at once. such as it just spawned withers, and is out of heads to spawn more. so yellow and purple are both flashing. but, green is also on. indicating that it's currently toggled on.

so i also need it to be able to tell me that withers are being killed, it's out of heads and toggled online all at the same time.

redstone is all setup for it. just the script isn't.

and here's a image of the UI




here is the script to the layout. (wasn't too hard to figure out)


mon = peripheral.wrap("top")
mon.clear()
mon.setTextScale(.7)
mon.setCursorPos(23, 24)
mon.setTextColor(colors.purple)
mon.write("Wither")
mon.setTextColor(colors.white)
mon.write(" Auto Spawner Ver 2.1")
mon.setCursorPos(1, 23)
mon.setTextColor(colors.white)
mon.write("--------------------------------------------------------------------------------------------------------------")
mon.setCursorPos(1, 5)
mon.setTextColor(colors.white)
mon.write("<-")
mon.setTextColor(colors.green)
mon.write(" on")
mon.setTextColor(colors.white)
mon.write("/")
mon.setTextColor(colors.red)
mon.write("off")
mon.setTextColor(colors.purple)
mon.setCursorPos(1, 1)
mon.write("Wither Spawn")
mon.setCursorPos(23, 1)
mon.setTextColor(colors.yellow)
mon.write("Wither Heads")
mon.setCursorPos(50, 1)
mon.setTextColor(colors.red)
mon.write("Off")
mon.setCursorPos(70, 1)
mon.setTextColor(colors.green)
mon.write("On")
Edited on 20 December 2015 - 09:17 PM
Lupus590 #2
Posted 20 December 2015 - 02:47 PM
First off, your indenting is a bit confusing.

May I suggest that you use functions to draw the GUI/Screen? It should make your code more manageable.

A bit more information on the current setup and what the new system must do will be helpful.
D3athbysp0rk #3
Posted 20 December 2015 - 09:55 PM
First off, your indenting is a bit confusing.

May I suggest that you use functions to draw the GUI/Screen? It should make your code more manageable.

A bit more information on the current setup and what the new system must do will be helpful.

sorry about how all confusing it is, thing is, i don't know really how to write lua script. so could you possibly explain in more detail what you mean by drawing functions? i"m a LUA noobie.

so with the setup. i'll try to explain it. it's a little complex, and my thoughts are always scattershot, so it makes it rather difficult for me to explain things in general.

I'll edit my post with more information.
KingofGamesYami #4
Posted 20 December 2015 - 10:11 PM
Lupus is suggesting you create a function (or multiple functions) that draws the things you need to, then call that function when you want those things drawn.
TheOddByte #5
Posted 20 December 2015 - 10:23 PM
To elaborate to what those above me has said, it makes it much easier to manage everything if you have a function that draws everything, and then use that function in a loop. It would also be wise to have a function that handles all events aswell.

Here's an example

--# Define a new function that draws everything
local function draw()
	term.clear()
	term.setCursorPos( 1, 1 )
	print( "Hello World!" )
	print( "Foo bar" )
end


--# Define a function that handles events and such
local function handle()
    local e = { os.pullEvent() }
end

while true do
    draw() 
    handle
end
Edited on 20 December 2015 - 09:24 PM
D3athbysp0rk #6
Posted 20 December 2015 - 10:26 PM

--# Define a new function that draws everything
local function draw()
	term.clear()
	term.setCursorPos( 1, 1 )
	print( "Hello World!" )
	print( "Foo bar" )
end


--# Define a function that handles events and such
local function handle()
	local e = { os.pullEvent() }
end

while true do
	draw()
	handle
end

okay I can see kinda what's going on there. not sure about some things though. and sorry if i have all these questions.

so in the script. what does the

    local e = { os.pullEvent() }
do?

i know while true do creates a loop. so i'm guessing the first part is where i'd put my layout. where you have draw? and handle would be where i set up the redstone part?
Edited on 20 December 2015 - 09:35 PM
KingofGamesYami #7
Posted 20 December 2015 - 10:41 PM
os.pullEvent pulls an event

{} creates a new table

{thing1, thing2, thing3} creates a new table where tablename[ 1 ] equals thing1, tablename[ 2 ] equals thing2, and so on.

e = {os.pullEvent()} creates a new table (e) where e[ 1 ] is the first return of os.pullEvent (the event type, eg "mouse_click"), etc.

It's a fairly common practice to pull events this way when you expect multiple types of events (eg "mouse_click" and/or "redstone")
D3athbysp0rk #8
Posted 20 December 2015 - 11:07 PM
I'm rather confused. Thank you for taking the time to explain this though.

not sure what os.pullEvent does. or why you need to "pull an event"

so e is a varible in this right? that you can call apon? before i found some script on cables like this. i changed them some, it doesn't seem to work though.


while true do
   local event = os.pullEvent("redstone")
   local Red=rs.testBundledInput("front",colors.red)
   local Yellow=rs.testBundledInput("front",colors.yellow)
   local Green=rs.testBundledInput("front",colors.green)
   local purple=rs.testBundledInput("front",colors.purple)
   if Red then
   mon.setCursorPos(25, 25)
   mon.write "Red"
   elseif Yellow then
   mon.setCursorPos(25, 25)
   mon.write "Yellow"
   elseif Purple then
   mon.setCursorPos(25, 25)
   mon.write "Purple"
   mon.setCursorPos(25, 25)
   elseif Green then
   mon.setCursorPos(25, 25)
   mon.write "Green"
   end
end

in here I noticed it calls apon e as a color, if it is that color being signaled to then write something.
Edited on 20 December 2015 - 10:08 PM
TheOddByte #9
Posted 20 December 2015 - 11:30 PM
I'm rather confused. Thank you for taking the time to explain this though.

not sure what os.pullEvent does. or why you need to "pull an event"
You usually do this to handle games, menus etc. And this function basically waits for an event and then returns it to you, and then you use that event to do something, like for example moving your character in a game or navigating in a menu. And that's also a reason why you put it in a loop, otherwise it'll only happen once and then nothing more will happen.

Another thing with the os.pullEvent function that you might want to know is that you can supply it with a filter as a parameter, this filter will wait until the specified event has been pulled, like in the code you just posted.

os.pullEvent( "redstone" )
^ Nothing will occur until a redstone event has been passed to the computer by doing this, and this could be useful in cases where you have a program that only needs to do one thing, like handling a redstone event or a rednet event.

so e is a varible in this right? that you can call apon? before i found some script on cables like this. i changed them some, it doesn't seem to work though.


while true do
   local event = os.pullEvent("redstone")
   local Red=rs.testBundledInput("front",colors.red)
   local Yellow=rs.testBundledInput("front",colors.yellow)
   local Green=rs.testBundledInput("front",colors.green)
   local purple=rs.testBundledInput("front",colors.purple)
   if Red then
   mon.setCursorPos(25, 25)
   mon.write "Red"
   elseif Yellow then
   mon.setCursorPos(25, 25)
   mon.write "Yellow"
   elseif Purple then
   mon.setCursorPos(25, 25)
   mon.write "Purple"
   mon.setCursorPos(25, 25)
   elseif Green then
   mon.setCursorPos(25, 25)
   mon.write "Green"
   end
end

in here I noticed it calls apon e as a color, if it is that color being signaled to then write something.
I don't understand what the problem here is, but one thing I can tell you is that the event function isn't really doing anything here, except that it waits until a redstone event has been pulled. It is the rs.testBundledInput function that checks whether or not a bundled color input is on or off.
So can you elaborate a little more on what you mean by "it doesn't work", because that information is a little vague. Are you getting any errors? Unexpected results?
D3athbysp0rk #10
Posted 20 December 2015 - 11:38 PM
oh you know what it is, i don't have any mon.clear also forgot to set the string for the monitor wrapping. :P/> bout it i understand about CC. let me see if adding that will help.

but i want it to write on the monitor the color. (testing the script) to make sure it works. to then implement it.

edit: yea, added the mon.clear() and wrapping. not seeing anything when i flick the lever to the colored cables.
Edited on 20 December 2015 - 10:52 PM
KingofGamesYami #11
Posted 21 December 2015 - 01:39 AM
A bit of explanation about os.pullEvent

An event is queued whenever something happens, such as a mouse click, or a key press. You can find a complete list of events here. There's a great tutorial (with links to appropriate pre-requisites) here.

In short, anything that 'pauses' your script's execution is going to pull an event. All user input is an event, excepting control+s and control+r (these are handled by the mod itself, not the computer). Even placing a disk in a (connected) disk drive generates an event.
TheOddByte #12
Posted 21 December 2015 - 12:17 PM
edit: yea, added the mon.clear() and wrapping. not seeing anything when i flick the lever to the colored cables.
One question, are you clearing the monitor after or before you're writing to the monitor? If it's after then there you have your problem, but anyway, please post your current code, it makes it so much easier to help you