Very basic problem with Font:getWidth/getHeight

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Nicholas Scott
Prole
Posts: 49
Joined: Sun Jun 07, 2015 9:12 am

Very basic problem with Font:getWidth/getHeight

Post by Nicholas Scott » Mon Mar 14, 2016 5:30 am

Anyone who can shine some quick light on this would be much appreciated.

So the problem is in this section of code:

Code: Select all

obj.type = "BoxButton"
	if not obj.font then obj.font = love.graphics.getFont() end
	local Font = love.graphics.getFont()
	obj.w = Font:getWidth(obj.text)
	obj.h = Font:getHeight(obj.text)
	table.insert(UserInterface.drawables, obj)
Now when I try to reference obj.w and obj.h it's saying they are nil values? obj.text is declared as "Testing" so the string is declared? any info?

Edit: Any help with this is still much appreciated but I'm using a workaround were it will set the width and height on love.update(), better for if the text changes, but thanks alot for any help!

User avatar
Kingdaro
Party member
Posts: 395
Joined: Sun Jul 18, 2010 3:08 am

Re: Very basic problem with Font:getWidth/getHeight

Post by Kingdaro » Mon Mar 14, 2016 6:15 am

I'm willing to bet there's no font set yet. You're going to want to make a new font instead.

Code: Select all

   local font = love.graphics.newFont(24)
   obj.w = font:getWidth(obj.text)
   obj.h = font:getHeight(obj.text)
   table.insert(UserInterface.drawables, obj)
However, make sure you only make it once and put it somewhere it can be reused, as the wiki states. https://love2d.org/wiki/love.graphics.newFont

Nicholas Scott
Prole
Posts: 49
Joined: Sun Jun 07, 2015 9:12 am

Re: Very basic problem with Font:getWidth/getHeight

Post by Nicholas Scott » Mon Mar 14, 2016 6:22 am

Kingdaro wrote:I'm willing to bet there's no font set yet. You're going to want to make a new font instead.
However, make sure you only make it once and put it somewhere it can be reused, as the wiki states. https://love2d.org/wiki/love.graphics.newFont
Thanks :D
Ye the font may or may not be set, I'm not sure, however it is working now. I'm not sure if it a problem with the font being set or not because I have it basically the same but declared before I pass obj to the function

boxButton.font = love.graphics.getFont()
and

Code: Select all

local function boxButtonUpdate(dt, obj)
obj.w = obj.font:getWidth(obj.text)
return obj
end
and it works now, I'm not sure

User avatar
zorg
Party member
Posts: 3003
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Very basic problem with Font:getWidth/getHeight

Post by zorg » Mon Mar 14, 2016 7:54 am

According to the wiki, since 0.9.x, in the event that there's no Font set, getFont will create a default one, and return that, so that shouldn't be an issue.
Only other thing would be that obj.text was not set, since that's the only line i didn't see in the first post's code block.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

Nicholas Scott
Prole
Posts: 49
Joined: Sun Jun 07, 2015 9:12 am

Re: Very basic problem with Font:getWidth/getHeight

Post by Nicholas Scott » Mon Mar 14, 2016 6:35 pm

zorg wrote: Only other thing would be that obj.text was not set, since that's the only line i didn't see in the first post's code block.
ye I could it's declared elsewhere, I'll put that code below, just didn't include it cause it was kind of irrelevant but here you go :D

Code: Select all

	local boxButton = {} --text, x, y, r, scaleX, scaleY, onClickEvent, onHoverEvent, offHoverEvent, color
	local function boxButtonToggleFollow(obj)
		obj.isFollowing = not obj.isFollowing
		return obj
	end
	local function boxButtonClickEvent(obj)
		local obj = obj.toggleFollow(obj) or obj
		return obj
	end
	local function boxButtonOnHoverEvent(obj)
		if obj.isFollowing then return obj end
		obj.color = {0,255,0}
		return obj
	end
	local function boxButtonOffHoverEvent(obj)
		if obj.isFollowing then return obj end
		obj.color = {255,0,255}
		return obj
	end
	local function boxButtonDestroy()
		UserInterface:destroyObj("NicksButtonTest")
	end
	local function boxButtonUpdate(dt, obj)
		obj.w = obj.font:getWidth(obj.text)
		obj.h = obj.font:getHeight(obj.text)
		obj.text = obj.text
		if obj.isFollowing then
			obj.x = love.mouse.getX() - obj.w/2
			obj.y = love.mouse.getY() - obj.h/2
			obj.color = {255,255,255}
		end
		return obj
	end
	boxButton.isFollowing = false
	boxButton.ident = "NicksButtonTest"
	boxButton.text = "Testing The Fucking Function"
	boxButton.x = 10
	boxButton.y = 10
	boxButton.w = 0
	boxButton.h = 0
	boxButton.r = 0
	boxButton.scaleX = 1
	boxButton.scaleY = 1
	boxButton.color = {255,0,255}
	boxButton.popout = false
	boxButton.popoutX = 0
	boxButton.popoutY = 0
	boxButton.font = love.graphics.newFont("gfx/MenuFont.ttf", 32)
	boxButton.onClickEvent = boxButtonClickEvent
	boxButton.onHoverEvent = boxButtonOnHoverEvent
	boxButton.offHoverEvent = boxButtonOffHoverEvent
	boxButton.destroy = boxButtonDestroy
	boxButton.update = boxButtonUpdate
	boxButton.toggleFollow = boxButtonToggleFollow
	UserInterface:addTextButton(boxButton)
Edit: I've made quite a few workarounds and if you notice in boxButtonUpdate() it's setting the w and h of the text, so that's a workout but I'm still curious as to why it didn't work before, it's almost identical code but works now? it's weird xD

User avatar
zorg
Party member
Posts: 3003
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Very basic problem with Font:getWidth/getHeight

Post by zorg » Mon Mar 14, 2016 8:08 pm

Might have been some : vs . or scope issues, can't tell sadly.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

Nicholas Scott
Prole
Posts: 49
Joined: Sun Jun 07, 2015 9:12 am

Re: Very basic problem with Font:getWidth/getHeight

Post by Nicholas Scott » Tue Mar 15, 2016 8:36 am

zorg wrote:can't tell sadly.
I backed the broken version up I might upload it to github later for you to have a look if you'd like

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 22 guests