166 posts
Location
Don't look behind you..
Posted 25 July 2012 - 10:38 AM
Guys I wanted to make a program that tells me the IC2's reactor's heat. I used an addon to do that, But I am having some issues here is the code.
function check()
heat = 0
if rs.testBundledInput("back", colors.white ) == true
then
heat = 500
elseif rs.testBundledInput("back", colors.orange ) == true
then
heat = 1000
PS Could someone tell me how to make spoilers for the code?
And the code is not finished.
Thanks in advance.
864 posts
Location
Sometime.
Posted 25 July 2012 - 11:00 AM
function check()
heat = 0
if colors.test(rs.getBundledInput("back"), colors.white) then
heat = 500
elseif colors.text(rs.getBundledInput("back"), colors.orange) then
heat = 1000
end
end
1: You forgot end
2: Its colors.test as shown in the code.
3: + me if I helped :)/>/>
166 posts
Location
Don't look behind you..
Posted 25 July 2012 - 05:57 PM
that is not my whole code for some reason it did not show up lemme get to my PC where I have minecraft and I'll correct it.
and of course, thanks for helping me :)/>/>
1604 posts
Posted 25 July 2012 - 07:39 PM
2: Its colors.test as shown in the code.
rs.testBundledInput does the same, and I think it works better (since it doesn't have to get the input and then use the bit api to test it).
166 posts
Location
Don't look behind you..
Posted 25 July 2012 - 09:00 PM
This is all the code by now..
heat = "0"
while true do
term.clear()
sleep(0)
print("Reactor Heat |")
print(" |")
print( heat )
check()
function check()
if rs.testBundledInput("back", colors.white ) == true
then
heat = "500"
elseif rs.testBundledInput("back", colors.orange ) == true
then
heat = "1000"
end
end
end
And I will put other colors too.
3790 posts
Location
Lincoln, Nebraska
Posted 25 July 2012 - 09:23 PM
heat = "0"
while true do
term.clear() sleep(0)
print("Reactor Heat |")
print(" |")
print("tostring(heat)") –This should be the command to turn the variable heat into a string.
check()
function check()
if rs.testBundledInput("back", colors.white ) == true
then
heat = "500"
elseif rs.testBundledInput("back", colors.orange ) == true
then
heat = "1000"
end
end
end I might be a little off on the syntax of the tostring() command, but I know that's how you turn a variable into a string.
3790 posts
Location
Lincoln, Nebraska
Posted 25 July 2012 - 09:27 PM
Wow, that turned out weird… reposted here:
heat = "0"
while true do
term.clear()
sleep(0)
print("Reactor Heat |")
print(" |")
print("tostring(heat)") --This should be the command to turn the variable heat into a string.
check()
function check()
if rs.testBundledInput("back", colors.white ) == true
then
heat = "500"
elseif rs.testBundledInput("back", colors.orange ) == true
then
heat = "1000"
end
end
end
1604 posts
Posted 25 July 2012 - 10:04 PM
Wow, that turned out weird… reposted here:
heat = "0"
while true do
term.clear()
sleep(0)
print("Reactor Heat |")
print(" |")
print("tostring(heat)") --This should be the command to turn the variable heat into a string.
check()
function check()
if rs.testBundledInput("back", colors.white ) == true
then
heat = "500"
elseif rs.testBundledInput("back", colors.orange ) == true
then
heat = "1000"
end
end
end
The variable is already a string. Also, that would just print "tostring(heat)", not convert to string and print that, you need to remove the quotes.
local heat = 0
while true do
term.clear()
term.setCursorPos(1, 1) -- move the cursor to the top-left corner
print("Reactor Heat |")
print(" |")
print(heat)
if rs.testBundledInput("back", colors.white ) then
heat = 500
elseif rs.testBundledInput("back", colors.orange ) then
heat = 1000
end
os.pullEvent("redstone") -- wait for a change in redstone
end
3790 posts
Location
Lincoln, Nebraska
Posted 25 July 2012 - 10:10 PM
See? that's what I get for trying to sound smart… Thanks, that cleared things up for me anyway, and I might use tostring more often now that I know what it DOESN'T do…L :)/>/>
864 posts
Location
Sometime.
Posted 25 July 2012 - 11:55 PM
The "== true" part is unnecessary
3790 posts
Location
Lincoln, Nebraska
Posted 26 July 2012 - 12:21 AM
Yup, I see that now, thanks. That's because nothing can really absolutely equal true unless it is true anyway, right?
166 posts
Location
Don't look behind you..
Posted 26 July 2012 - 11:17 AM
Thanks you guys checking right now how it goes :)/>/>.
166 posts
Location
Don't look behind you..
Posted 26 July 2012 - 12:17 PM
The new code is this:
local heat = 0
while true do
term.clear()
term.setCursorPos(1, 1)
print("Reactor Heat |")
print(" |")
print(heat.." |")
if rs.testBundledInput("back", colors.white ) then
heat = 500
elseif rs.testBundledInput("back", colors.orange ) then
heat = 1000
elseif rs.testBundledInput("back", colors.lime ) then
heat = 1500
elseif rs.testBundledInput("back", colors.magenta ) then
heat = 2500
elseif rs.testBundledInput("back", colors.lightBlue ) then
heat = 4000
elseif rs.testBundledInput("back", colors.yellow ) then
heat = 6000
elseif rs.testBundledInput("back", colors.pink ) then
heat = 9000
end
os.pullEvent("redstone")
end
Unfortunly it gets up to 500 heat and then (even if the reactor is at 1500) it stays a 500.
8543 posts
Posted 26 July 2012 - 03:10 PM
You need to check not only that the current heat level is true, but also that the next heat level is false. As long as the reactor is at least 500 degrees, it'll never have reason to look at the other cases. Once you also check that the next one is false, it will be forced to use the correct one.
864 posts
Location
Sometime.
Posted 26 July 2012 - 03:46 PM
To fix the 500 problem
Do this
term.setCursorPos(1, 3)
term.clearLine()
print(heat .." |")
Code thing is a bit messed, but it works.
8543 posts
Posted 26 July 2012 - 03:49 PM
To fix the 500 problem
Do this
term.setCursorPos(1, 3)
term.clearLine()
print(heat .." |")
Code thing is a bit messed, but it works.
The problem is with the logic in the if-elseif conditionals, not the printed output.
864 posts
Location
Sometime.
Posted 26 July 2012 - 03:51 PM
Ohh I see..
Why is the os.pullEvent("redstone") necessary then?
Why not just get rid of that and have it sleep(1) to update?
8543 posts
Posted 26 July 2012 - 03:56 PM
There's no point having it constantly loop when there are no changes to make. That makes it wait until a redstone event is received. It'll be more responsive (won't take up to a second to update) and will not consume system resources while spinning uselessly.
992 posts
Posted 26 July 2012 - 04:02 PM
convert all elseif statements into if statements then it will work properly
Spoiler
local heat = 0
term.clear()
term.setCursorPos(1, 1)
print("Reactor Heat |")
print(" |")
print(heat.." |")
while true do
os.pullEvent("redstone")
heat = 0 -- to reset it
if rs.testBundledInput("back", colors.white ) then
heat = 500
end
if rs.testBundledInput("back", colors.orange ) then
heat = 1000
end
if rs.testBundledInput("back", colors.lime ) then
heat = 1500
end
if rs.testBundledInput("back", colors.magenta ) then
heat = 2500
end
if rs.testBundledInput("back", colors.lightBlue ) then
heat = 4000
end
if rs.testBundledInput("back", colors.yellow ) then
heat = 6000
end
if rs.testBundledInput("back", colors.pink ) then
heat = 9000
end
term.clear()
term.setCursorPos(1, 1)
print("Reactor Heat |")
print(" |")
print(heat.." |")
end
8543 posts
Posted 26 July 2012 - 07:00 PM
convert all elseif statements into if statements then it will work properly
Spoiler
local heat = 0
term.clear()
term.setCursorPos(1, 1)
print("Reactor Heat |")
print(" |")
print(heat.." |")
while true do
os.pullEvent("redstone")
heat = 0 -- to reset it
if rs.testBundledInput("back", colors.white ) then
heat = 500
end
if rs.testBundledInput("back", colors.orange ) then
heat = 1000
end
if rs.testBundledInput("back", colors.lime ) then
heat = 1500
end
if rs.testBundledInput("back", colors.magenta ) then
heat = 2500
end
if rs.testBundledInput("back", colors.lightBlue ) then
heat = 4000
end
if rs.testBundledInput("back", colors.yellow ) then
heat = 6000
end
if rs.testBundledInput("back", colors.pink ) then
heat = 9000
end
term.clear()
term.setCursorPos(1, 1)
print("Reactor Heat |")
print(" |")
print(heat.." |")
end
The solution I posted above is better. Yours also works, but it is better to use elseif when choosing between multiple exclusive options.