Get height/width of default font throws errors during runtime.

ShivaFang

  • Posts: 248
Using the default font, I get runtime errors in the flixel/flash debug window with the 'get height for current font' 'get height for [select font] font' and the equivalent width functions.

I'm trying to create a button where the text is centered in the middle of it.  I've attached my logs and the code block in the 'draw' section that I'm currently working with.

(currently the block is in a print statement, which was done to try to figure out why my code wasn't working as expected - but I've determined it's not my code, but the block itself and I have no control over that.)

Edit:  After poking around, I discovered the error only happens with the 'default font', not a font that's actually created, which is probably why this bug hasn't been caught before.

« Last Edit: December 11, 2011, 12:18:03 pm by ShivaFang »
Justin "ShivaFang" White
Aquamentos Games - The origin of challenging Strategy and Role-Playing Flash gaming!
Visit our Developer Blog and Google+ Page!

ShivaFang

  • Posts: 248
 Because it seems to me from a quick glance at the logs that they only show console logs and not the flixel logs, I'm attaching the error I get.

I moved the 'Set init to <true>' block to the top of my if statement so that the error only prints once, and I used the 'get height using current font' block, to show that the 'null reference' is not any of my attributes.  (if the 'font' attribute was null it would have errored earlier in the code).

Disabling 'get height using current font' the code runs as expected.

I believe I have sufficiently examined this to the point where this is a Stencyl bug and not a user error.
Justin "ShivaFang" White
Aquamentos Games - The origin of challenging Strategy and Role-Playing Flash gaming!
Visit our Developer Blog and Google+ Page!

captaincomic

  • *
  • Posts: 6108
I had the same error messages before. Are you sure it is the 'get height for current font' etc functions that throw the error? I thought that the error comes from using the "set current font to" with a null-attribute.

The code in the attachment seems to work fine for me (for default font and for custom font alike).

But I would consider it a bug that setting the font to "default font" throws an error unless you check if font is not null. Apperently selecting "default font" means the attribute is null, so setting the font to null should be just ignored, instead of raising an error.

ShivaFang

  • Posts: 248
As I said "Print [get height for current font]" was the line that gave me errors (no user attributes that could cause the null variable.)  Disabling that line the code ran as expected AND drew the text (if the font was null, you wouldn't expect to see text appearing at all, would you, yet my screenshot clearly shows the text underneath the log window when I skip the print statements after the first draw pass in the 2nd post.)

I DID however notice that when I changed it to a 'made' font, that the behaviour fonts were blank, when I'm certain I set them (but maybe forgot to save changes), so maybe the bug occurs when there is a null font, it will set a null font and draw a null font, but won't get the properties of a null font.  So there might be some aspect of user error, and Stencyl handles it as a default font, but not completely.  To be honest I'd rather Stencyl throw an error right away, rather than try to handle it and fail for some obscure reason, because this way it'd be harder to debug why it's not working.

Edit:    Switched back to the default text, and it turns out, default text causes the lines to be blank, so it seems to me that default and null font are the same thing.  Regardless, the following code works with a 'made' font, but not with the default font for me.

If it was the set font that was causing the error, text won't appear at all (because that happens before the if statement) anything in the if statement only happens once, so if the error were happening inside the if statement, the text will appear at 0,0 - which is the behaviour I'm seeing.

2nd attachment - the top button is what happens with the default font (draws at 0,0 - the set lines throw an error on the first pass and so they stay at 0), the bottom 2 buttons are with a made font and right in the middle (yes, the button is too small for 'Campaign Mode', but that's not the issue here)


« Last Edit: December 11, 2011, 02:24:57 pm by ShivaFang »
Justin "ShivaFang" White
Aquamentos Games - The origin of challenging Strategy and Role-Playing Flash gaming!
Visit our Developer Blog and Google+ Page!

captaincomic

  • *
  • Posts: 6108
Hmmm, I'm confused, maybe there is more than one issues here.
One thing I can say for sure now: "Default Font" means that the attribute is null.
So whenever you use a font attribute it's probably a good idea to check if it's not null.

Does it help if you put your "set font to Font"-block in a "if Font has value"-block?

ShivaFang

  • Posts: 248
Well see if you are checking if font is not null then you aren't using the default font.  That's not fixing the bug, that's just ignoring it (which you can do by making sure you always use created fonts and not the default)

Why have a default at all at that point?
Justin "ShivaFang" White
Aquamentos Games - The origin of challenging Strategy and Role-Playing Flash gaming!
Visit our Developer Blog and Google+ Page!

captaincomic

  • *
  • Posts: 6108
Put only the "set font to"-block in the "if Font has value"-block, all the other blocks wouldn't be inside the if block.
Not setting the font is the same as using the default font.

So if the Font attribute is not null, you set the font with the "set font to"-block.
But if the Font attribute is null, you don't set the font to anything (i.e. use the default font).
Then you do the drawing, and you can use "get height/get width"-blocks. (at least it works for me)

ShivaFang

  • Posts: 248
That works, but only if you use ONLY default fonts in your project.  Since the current font is a global, setting any font will change all objects that skip null in this way.

What this tells me is that the starting font when you load the game is not actually null, but some other value, and when you set the font to null, it half-works (hence, this is still a bug that needs to be addressed)

To illustrate my point, I made my font tiny, and only set the bottom-most button to my font, and left the other two 'default'.  I then changed the behaviour the way you suggested (if font has value then change the font).  As you can see, this causes problems, because the sizes are calculated with the default font, but since it dosn't change the font each draw, they are not in the right position (I only calculate the position on the first pass, to save processing time and improve framerates).

This proves that;
a)  You are right, not setting the font to null works (sort of)
b)  The 'current font' is global, and by not changing the font each draw, you open yourself up to it being a different font if you wind up setting a font anywhere else in your game.
c)  That Stencyl tries to use the default font when the font is set to null, but somehow that doesn't work for properties like getting the width and height, and therefore throws errors instead of working the way you'd expect it to.  If I set 'default font' in a behaviour, I expect it to use the default font without silly work arounds ESPECIALLY if I were a beginner and didn't understand any of this.

Yes, there are work arounds, and yes people really should be using made fonts instead of relying on the default, but if the option is there, it should work intuitively without errors (hence why this is the bug forum, not the 'ask a question forum'.  I'm reporting something the developers need to address)


« Last Edit: December 11, 2011, 04:50:46 pm by ShivaFang »
Justin "ShivaFang" White
Aquamentos Games - The origin of challenging Strategy and Role-Playing Flash gaming!
Visit our Developer Blog and Google+ Page!

captaincomic

  • *
  • Posts: 6108
Ok, I didn't notice that the current font is global (shared between actors/behaviors).
So using "default font" really just means "use the last font that has been set or if no font has been set, use some kind of default font, which cannot be set manually, after a different font has been used". That's really not very intuitive.

I completely agree with you that "default font" should "just work".

But I have no idea why getting width/height with the default font doesnt work for you. For me it does.

ShivaFang

  • Posts: 248
I can get the height width from the 'current' font, if I don't set it first (which, as far as I can tell is what you are doing also)

but if you set it to 'null' (which is what it does if you pull 'default font' from the behaviour) then it doesn't work.

What this means is, if I were to upload my behaviour to the Forge, there's two ways I can implement it.  My way (always us the font selected) and your way (only use the selected font if it's not null).  Either way, it breaks if a beginner downloads it and selects 'Default font'.  (My way it throws errors, your way, it will be overriden by any other font set in another behaviour)

In fact the ONLY way to truly bypass this bug is to never ever use 'default font' selection in a behaviour attribute.  (Which is good practice anyways, but still gotta make it work for Beginners or people who just want to prototype something without creating a font first - which is how I ran into it)

« Last Edit: December 11, 2011, 06:23:49 pm by ShivaFang »
Justin "ShivaFang" White
Aquamentos Games - The origin of challenging Strategy and Role-Playing Flash gaming!
Visit our Developer Blog and Google+ Page!

captaincomic

  • *
  • Posts: 6108
Ok, I see.

One way how this bug could be fixed would be to have a default font in the library->fonts section, that is always there and cannot be renamed or deleted.
And selecting "default font" would not mean the font attribute is null, but select this always available font.

If the behavior is desired that you can select the current font, I think it would be good if you could select "current font", but wouldn't have to check for not null.

ShivaFang

  • Posts: 248
that would be one way to do it, or when stencyl set the font, if it's null, set it to whatever it's set to when you start the program (instead of whatever it's doing now, which doesn't seem to quite work the same way)
Justin "ShivaFang" White
Aquamentos Games - The origin of challenging Strategy and Role-Playing Flash gaming!
Visit our Developer Blog and Google+ Page!