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

[Lua][Error]Aptemp to call string

Started by Teraminer, 25 July 2012 - 08:38 AM
Teraminer #1
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.
Noodle #2
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 :)/>/>
Teraminer #3
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 :)/>/>
MysticT #4
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).
Teraminer #5
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.
Cranium #6
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.
Cranium #7
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
MysticT #8
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
Cranium #9
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 :)/>/>
Noodle #10
Posted 25 July 2012 - 11:55 PM
The "== true" part is unnecessary
Cranium #11
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?
Teraminer #12
Posted 26 July 2012 - 11:17 AM
Thanks you guys checking right now how it goes :)/>/>.
Teraminer #13
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.
Lyqyd #14
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.
Noodle #15
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.
Lyqyd #16
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.
Noodle #17
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?
Lyqyd #18
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.
BigSHinyToys #19
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
Lyqyd #20
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.