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

Expected number but not seeking a number?

Started by andrewhopps, 16 March 2013 - 09:37 AM
andrewhopps #1
Posted 16 March 2013 - 10:37 AM

mon.write("Was that a hit?")
function hitormiss()
  local event, param1 = os.pullEvent("char")
  if event == "char" and param1 == "y" or param1 == "Y" then	--Expected number supposedly occurs on this line
	mon.setTextColor(colors.green)
	mon.setTextScale(5)
	mon.write("HIT!")
	os.sleep(3)
	mon.setTextColor(colors.white)
	mon.setTextScale(1)
  elseif event == "char" and param1 == "n" or param1 == "N" then
	mon.setTextColor(colors.red)
	mon.setTextScale(5)
	mon.write("Miss.")
	os.sleep(3)
	mon.setTextColor(colors.white)
	mon.setTextScale(1)
  else
	mon.write("Please enter Y or N.")
	os.sleep(2)
  end
end
hitormiss()

I am fairly new to lua, and have no idea why this keeps returning Expected Number every single time. Perhaps my code is jumbled after deleting and retyping things back and forth for the literal past 3 hours. Thank you.

** I am aware that my code doesn't loop properly or whatever, I am only worried about getting rid of the error preventing me from testing any further **
Lyqyd #2
Posted 16 March 2013 - 11:00 AM
Split into new topic.

I don't see where you define green, Red or white (and mixing case is always a bad idea for groups of variables like that). Perhaps you meant colors.green, colors.red, and colors.white?
andrewhopps #3
Posted 16 March 2013 - 11:07 AM
Split into new topic.

I don't see where you define green, Red or white (and mixing case is always a bad idea for groups of variables like that). Perhaps you meant colors.green, colors.red, and colors.white?

fixed to colors.green, colors.red, and colors.white no error, but the text doesn't change or even show after selecting an option

** edit: Was missing a couple mon.clear() so the text was pushed too far off the screen to notice. Thank you!
andrewhopps #4
Posted 16 March 2013 - 12:02 PM
Off topic, but I would hate to start another. Is there a way to set a variable for .setTextScale so I can change it depending on the statements outcome?
Czarified #5
Posted 16 March 2013 - 12:16 PM
Off topic, but I would hate to start another. Is there a way to set a variable for .setTextScale so I can change it depending on the statements outcome?


function textScaleSet(scale)
   mon.setTextScale(scale)
end

Like that? I'm no pro, but I think that would work.
andrewhopps #6
Posted 16 March 2013 - 12:35 PM
Off topic, but I would hate to start another. Is there a way to set a variable for .setTextScale so I can change it depending on the statements outcome?


function textScaleSet(scale)
   mon.setTextScale(scale)
end

Like that? I'm no pro, but I think that would work.

To further clarify, I am using code that I found on the interwebs for centering text on a monitor, but it only centers default scaled text and everything else gets pushed off screen I am assuming. So I guess what I am really asking is, can I add another variable to this code that lets me change scale accordingly. Or someone with proper code that automatically centers according to scale and length?

local function centerText(text)
  local w, h = mon.getSize()
  mon.setCursorPos(math.floor(w / 2 - text:len() /2 + .5), math.floor(h / 2 + .5))
  mon.write(text)
end
Lyqyd #7
Posted 16 March 2013 - 05:11 PM
If you set scale before getting the size, it should give you the size of the scaled monitor.