#1
Posted 27 November 2016 - 04:27 AM
#2
Posted 27 November 2016 - 09:53 AM
http://computercraft...i/Redstone_(API)
#3
Posted 27 November 2016 - 04:26 PM
TheRockettek, on 27 November 2016 - 09:53 AM, said:
http://computercraft...i/Redstone_(API)
#5
Posted 28 November 2016 - 06:58 PM
Bomb Bloke, on 27 November 2016 - 11:52 PM, said:
This code causes a not enough arguments fault on line 36 but there are only 35 lines to the code. Also haven't been able to test it so I'm not sure its what i want even.
function getStates()
curColors = rs.getBundledInput("left")
states =
{
[1] = colors.test(curColors, colors.red),
[2] = colors.test(curColors, colors.grey),
}
return state
end
print "online"
pstate = getStates()
function rsEvent()
cstate = getStates()
if (pstates[1] == false and cstate[1] == true) then
print("red on")
elseif (pstate[1] == true and cstate[1] == false) then
print("red off")
elseif (pstate[2] == false and cstate[2] == true) then
print("grey on")
end
pstate = cstate;
end
repeat
local event,p1 = os.pullEvent()
if event == "redstone" then
rsEvent()
end
until event == "char" and p1 == "x"
#6
Posted 29 November 2016 - 03:29 AM
#7
Posted 02 December 2016 - 12:30 AM
Bomb Bloke, on 29 November 2016 - 03:29 AM, said:
awesome. Thank you. Dam spelling gets me every time.
Still has errors though. Now i'm getting a nil value in the function rsEvents() i can make the fault go away by taking out the "[" around the numbers in the if, then statements but then the program does nothing. That means that nothing is assigned to [1] from what i can tell but i did assign a value to it in the table at the top. Probably just a small mistake somewhere but I'm not seeing it yet.
Edited by nuceng, 02 December 2016 - 12:54 AM.
#8
Posted 02 December 2016 - 02:24 AM
pstate = getStates() function rsEvent() cstate = getStates() if (pstates[1] == false and cstate[1] == true) then print("red on") elseif (pstate[1] == true and cstate[1] == false) then print("red off") elseif (pstate[2] == false and cstate[2] == true) then print("grey on") end pstate = cstate; end
In the first 'if' statement, you use pstates, instead of pstate, causing the attempt to index a nil value error I suspect you are getting.
Edit: Also, the parentheses '()' are not required for if statements, and only serve to group separate terms in the statement, eg:
if ( x and y ) or ( a and b ) then --# Either both or one of the conditions grouped in parentheses was true. end if x and y then --# x and y are truthy end if x == true and y == false then --# x is true, y is false end --# so on
Your if statements don't do that, as only one operator is being used. This is just a design choice, but I prefer to avoid parentheses when I can.
Same applied to the print calls, if only a string is being printed, you can remove the parentheses, eg:
print "Hello World" --# Okay print "Hello".." world" --# No print aVariable --# No
Edited by Hbomb_79, 02 December 2016 - 02:30 AM.
#9
Posted 02 December 2016 - 03:44 AM
#10
Posted 02 December 2016 - 03:50 AM
function getStates()
curColors = rs.getBundledInput("left")
states =
{
[1] = colors.test(curColors, colors.red),
[2] = colors.test(curColors, colors.gray),
}
return state
end
print "online"
pstate = getStates()
function rsEvent()
cstate = getStates()
if pstate[1] == false and cstate[1] == true then
print "red on"
elseif pstate1 == true and cstate1 == false then
print "red off"
elseif pstate2 == false and cstate2 == true then
print "gray on"
end
pstate = cstate;
end
repeat
local event,p1 = os.pullEvent()
if event == "redstone" then
rsEvent()
end
until event == "char" and p1 == "x"
#11
Posted 02 December 2016 - 04:00 AM
if pstate[1] == false and cstate[1] == true then
Here you're attempting to index into pstate and cstate. These are set by the getStates() function, which defines a table "states" and returns the nil value "state".
#12
Posted 02 December 2016 - 04:41 AM
Bomb Bloke, on 02 December 2016 - 04:00 AM, said:
if pstate[1] == false and cstate[1] == true then
Here you're attempting to index into pstate and cstate. These are set by the getStates() function, which defines a table "states" and returns the nil value "state".
awesome that fixed the errors.
Program works now, however it doesn't do what I want it to. Did some testing on it and the program will display "red on" when it is supposed to but will not display "red off" or "gray on"
Ultimately I want this program to display the status of 16 or more redstone signals when they change and specifically what those signals mean. The program as written is just for testing. Once I get it to do what I want it Wont say "red on/off" it will say " Reactor 1 On/off" for example. The key is to make each signal completely independent from the others for example if I have 16 total signals and 7 are true and 9 are false and one of them goes from true to false that needs to be displayed without re-displaying any previous signals.
I mean am I even on the right track here or do i have the completely wrong format for what I'm trying to accomplish here?
Edited by nuceng, 02 December 2016 - 04:41 AM.
#13
Posted 04 December 2016 - 03:33 AM
You refer to variables "pstate1", "pstate2", etc where you meant to index into the pstate table (pstate[1], pstate[2], etc). Ditto for cstate.
Just for kicks, here's how I'd write the script, taking advantage of the fact that each colour is represented by a power of two:
local oldState = rs.getBundledInput("left") local colName = {"White", "Orange", "Magenta", "Light Blue", "Yellow", "Lime", "Pink", "Grey", "Light Grey", "Cyan", "Purple", "Blue", "Brown", "Green", "Red", "Black"} print("Online") repeat local event, p1 = os.pullEvent() if event == "redstone" then local curState, checkCol = rs.getBundledInput("left"), 1 for i = 1, 16 do local curOn, oldOn = colours.test(curState, checkCol), colours.test(oldState, checkCol), if curOn and not oldOn then print(colName[i] .. " turned on.") elseif not curOn and oldOn then print(colName[i] .. " turned off.") end checkCol = checkCol * 2 end oldState = curState end until event == "key" and p1 == keys.x
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users