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

Is A Turtle or a Computer

Started by CoderJohn, 14 November 2012 - 12:03 PM
CoderJohn #1
Posted 14 November 2012 - 01:03 PM
Is there a way to determine in lua if the computer running the program is a turtle or a static computer?

NEVERMIND < I figured out:
if(turtle~=nil)then
–is turtle
else
–is computer
end
Cloudy #2
Posted 14 November 2012 - 01:10 PM
if turtle then
– is turtle
else
– is not turtle
end
Orwell #3
Posted 14 November 2012 - 01:10 PM

local isTurtle = turtle ~= nil
Edit: ninja'd :)/>/> :P/>/>
Kingdaro #4
Posted 14 November 2012 - 01:23 PM
if (turtle)
– is turtle
else
– is not turtle
end
I know you were just making an example, but the missing "then" is really bothering me :P/>/>
Cranium #5
Posted 14 November 2012 - 01:26 PM
*GASP*
Cloudy made a noob mistake leaving out that 'then'.
Heh, I make that mistake all the time though.
GopherAtl #6
Posted 14 November 2012 - 01:30 PM
give him a break, cloudy's busy doing java code all day, working on CC and CCSensors. In java, that was perfectly fine. :P/>/>
Cranium #7
Posted 14 November 2012 - 01:34 PM
Well, when you put it that way, I guess I can give him some slack. Workin' hard to make the rest of us happy. GO CLOUDY, YOU CAN DO IT!
KaoS #8
Posted 14 November 2012 - 01:44 PM
nothing like people going psycho on the forums to cheer you up :P/>/> lets all scream random stuff lol -rolls eyes-
Cloudy #9
Posted 14 November 2012 - 10:05 PM
Damn it! Edited :P/>/>
TCE #10
Posted 23 November 2012 - 06:57 AM

computerScreenSize = {51,19}
turtleScreenSize = {39,13}
actualSize = {term.getSize()}
if actualSize == computerScreenSize then
actualTerminal = "computer"
elseif actualSize == turtleScreenSize then
actualTerminal = "turtle"
else
actualTerminal = "monitor"
print("This terminal is a "..actualTerminal)

Cannot detect if run on a monitor (will print "This terminal is a monitor")
Kingdaro #11
Posted 23 November 2012 - 07:41 AM
Cannot detect if run on a monitor (will print "This terminal is a monitor")
First off, it's missing an end.


computerScreenSize = {51,19}
turtleScreenSize = {39,13}
actualSize = {term.getSize()}
if actualSize == computerScreenSize then
actualTerminal = "computer"
elseif actualSize == turtleScreenSize then
actualTerminal = "turtle"
else
actualTerminal = "monitor"
end --## added the end here

print("This terminal is a "..actualTerminal)

Secondly, tables don't really work like that. They have the same values, but are entirely different objects, and won't come out as the same when compared. You'll probably just have to compare the tables' individual values. I would personally make a function for doing so for convenience.


function compare(first, second)
  for k,v in pairs(first) do
    if first[k] ~= second[k] then
      return false
    end
  end
  return true
end

computerScreenSize = {51,19}
turtleScreenSize = {39,13}
actualSize = {term.getSize()}
if compare(actualSize, computerScreenSize) then
  actualTerminal = "computer"
elseif compare(actualSize, turtleScreenSize) then
  actualTerminal = "turtle"
else
  actualTerminal = "monitor"
end --## added the end here

print("This terminal is a "..actualTerminal)
KaoS #12
Posted 23 November 2012 - 08:00 AM
is there no way to see if the term has been redirected? can we not access the redirect stack? that would make everything sooo much easier
Lyqyd #13
Posted 23 November 2012 - 09:19 AM
No, we can't access the redirect stack. One trick we can use is this:


if turtle and (term.getSize()) == 39 then
  --is a turtle.
elseif not turtle and (term.getSize() == 51 then
  --is a computer.
else
  --is a monitor. 
end