Page 1 of 1

Parameters Changing Types

Posted: Tue Apr 23, 2013 1:11 pm
by kylewatson98
Hey, I am trying to make a global textbox class to use in my games but I have ran into a problem :/ When I pass numbers through my parameters of a function they switch to booleans :/
This is the class:

Code: Select all

textbox = {}
textbox.__index = textbox

function textbox.new(focus, xl, yl, width, height)
	local txtbox = {}
	setmetatable(txtbox,textbox)
	txtbox.text = "Enter Text"
	txtbox.hasFocus = focus
	txtbox.x = x
	txtbox.y = y
	txtbox.width = width
	txtbox.height = height
	return txtbox
end

function textbox:draw()
	love.graphics.setColor(255,255,255,255)
	love.graphics.rectangle("fill", self.x, self.y, self.width, self.height)
	love.graphics.setColor(0,0,0,255)
	love.graphics.rectangle("line", self.x, self.y, self.width, self.height)
	love.graphics.printf(self.text, self.x + 5, self.y, self.width - 5, "left")
end

function textbox:keypress(k)
	if self.hasFocus then
		if k == "backspace" then
			self.text = string.sub(self.text, 1, string.len(self.text) - 1)
		elseif k == "return" then
			self.text = self.text .. "\n"
		else
			self.text = self.text .. k
		end
	end
end

function textbox:mousepress(b)
	if b == "l" then
		if love.mouse.getX() > self.x and self.x + self.width > love.mouse.getX() and  love.mouse.getY() > self.y and self.y + self.width > love.mouse.getY() then
			self.hasFocus = true
		else
			self.hasFocus = false
		end
	end
end
And this the main lua file:

Code: Select all

function love.load()
	love.graphics.setBackgroundColor(210,255,255,255)
	require "globalTextbox"
	txt = textbox:new(false, 10, 10, 200, 50)
end

function love.draw()
	txt:draw()
end

function love.keypressed(k)
	txt:keypress(k)
end

function love.mousepressed(b)
	txt:mousepress(b)
end
I tried passing strings through but they changed to tables :s please help.
Thanks in advance, Kyle.

Re: Parameters Changing Types

Posted: Tue Apr 23, 2013 4:49 pm
by LuaWeaver
It does not switch to a boolean, you just never use it. Change:

Code: Select all

   txtbox.x = x
   txtbox.y = y
to

Code: Select all

   txtbox.x = x1
   txtbox.y = y1
because you use x1 and y1, not x and y. As for string->tables, I think you're just doing something very wrong.

Re: Parameters Changing Types

Posted: Tue Apr 23, 2013 6:01 pm
by Boolsheet
The issue that LuaWeaver spotted is actually an additional problem to something else.

You forgot the colon when you created your new function. When you call it, textbox gets assigned to the variable focus instead of self.

Code: Select all

function textbox.new(focus, xl, yl, width, height)
                ^