SpriteFont work around... [SOLVED]

This forum is currently in read-only mode.
From the Asset Store
_______ Huge collection of metal fixtures ________
  • I've been having a lot of trouble with apparent bugs in SpriteFont recently, to make a long story short, and I think it's time to find an alternate way to accomplish the same effect using events.

    I was using it for my Mario game to represent the number of lives and coins you have, as well as your score and time remaining. These numbers (score in particular) can get pretty large, so I'm looking for a way to change sprites according to the value of a global variable that can get as big as, say, a billion.

    I'm... not even sure how to ask the question, so hopefully someone knows what I mean and can help me out with it.

  • Well, I've been staring at the non-Python one for a good ten minutes now and I think I got a decent handle on it. Not really sure how the hash table thing works, since I never worked with those before... It's mostly this line I don't get:

    imageFont: Set 'index' to HashTable(Uppercase(mid(Function.Param(1), LoopIndex, 1)))-1

    And the parts where you "destroy" imageFont, yet it's still there... What's all this about, if you don't mind explaining a bit?

  • It's mostly this line I don't get:

    imageFont: Set 'index' to HashTable(Uppercase(mid(Function.Param(1), LoopIndex, 1)))-1

    The hash table is an array where you access its content by keys instead of an index. So instead of index-value-pairs you have key-value-pairs. ROJOhound cleverly used the characters as keys and made the values being the index ('A' = 1, 'B' = 2, etc.)

    Every 200ms the text box changes its content, like so:

    0ms -> "T"

    200ms -> "Th"

    400ms -> "Thi"

    600ms -> "This"

    ...

    Now he destroys every instance of 'imageFont' and calls the function. The function determines the current length of the text in the text box and in a loop creates one 'imageFont'-object per character.

    He now needs to get the value of a key that corresponds to the actual character in the loop. These characters can be lowercase (e.g. 'h'), but the keys he generated are all uppercase (see event 2). Whatever character he gets, he needs to make sure it is uppercase. The expression 'convert to uppercase', => uppercase(string), does exactly that.

    mid returns a substring of a string at position p and with the length l: mid(text, p, l). this is 1-based. Something like mid("Four", 4, 1) returns 1 character starting at position 4, which is "r".

    For example, at 600ms, the text of the text box is "This". The loop starts at 1 and ends at len(text), which is 4 in this case. In the first iteration of the loop, mid(Function.Param(1), LoopIndex, 1) equals mid("This", 1, 1) and therefore returns "T". The second iteration equals mid("This", 2, 1) which returns "h", etc. As mentioned earlier, that string is converted to uppercase, which returns "T", "H", etc.

    He now has exactly one character of the text, and that character is uppercase. He can now use this character as a key of the hash table to get its value (which is the index of that char). For example, HashTable("T") was set to the value 20 in event 2.

    mid (which he used to create the key-value-pairs in event 2) is 1-based, while image offset is 0-based. That's why 1 is substracted from the value of the key.

    And the parts where you "destroy" imageFont, yet it's still there...

    The whole concept of this is to not have one imageFont, but one instance of imageFont per character. These are rebuilt every 200ms, that's why all of them need to be destroyed before creating them again.

  • Superb explanation tulamide, much better than what I would have written.

    *tips hat*

  • Okay, after fixing a handful of other issues, I can finally get around to doing this. And, uhh... that's quite an explanation.

    I more or less understand most of it now... The way I'm doing it, though, is to have it set to display the contents of a variable constantly rather than every 200 ms. Would I need to destroy and recreate each digit every tick?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • The example I posted probably does more than what you need. Like letters and writing one letter at a time.

    Here is another example that is simpler and easier to set up:

    It uses a 10 frame sprite for the digits and a text object to store the origin and value.

    http://dl.dropbox.com/u/5426011/examples4/simpledigits.cap made in 0.99.97

    Just change the text and the number will change.

  • Awesome. Altered a bit for what I need, and it works perfectly. Thanks!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)