Cant get UTF-8 plugin to work

Hey, I am trying to get my game to accept UTF-8 strings but my code wont work anymore after I added it.

I am making a simple hangman game and it wont recognize UTF-8 letters, as I am using utf-8.match etc. I also cannot combine UTF and “normal letters”, for example “B” and “Ø” would give me an error like this:

“bad argument #2 to ‘match’ (string expected, got nil)”

The game works like this. I type my hangman word, game splits the letters using utf8:sub and stores them in an table. I then use a for loop to utf8:match the table to a letter I type in.

I am not used to posting on forums, sorry in advance if I am missing something. I am not sure what code I shall post.

Please help a clueless coder! :smiley:

Without a code sample it’s hard to truly diagnose the issue, but the solution may be as simple as replacing the colons with periods when calling utf8 methods. Try swapping out utf8:match with utf8.match, for example. In lua, using the colon automatically passes in the object in front of the colon as the first argument. It’s a fairly common mistake.

Now that *may* solve your problem, but if it doesn’t, reply with a small code sample that reproduces the issue. Don’t just copy/paste your whole project or file - try to recreate the issue with as little code as possible, in a way that can be run by anybody without access to the rest of your app’s code. Also, use the <code> button in the comment composer to format your code in a block for easier readability.

if utf8.match (event.target.text, myLibrary.hangmanLetter[i]) then

this is where I got my main problem now, I got it to detect the letters( I can print my input(utf8 characters), couldnt do that earlier). But still when I try to compare I am told my string is nil. 

I think it has something to do with the special letters taking more space so it throws off my table. Does that make any sense? 

Hi Simon,

Well, unfortunately I can’t really test that bit of code because it has dependencies like your myLibrary.hangmanLetter table as well as the event.target.text, so it’s hard for me to say for sure what’s going on. But if you’re getting a error stating that a required argument to utf8.match is nil, then there’s probably some anomaly in your data where you are expecting a string but none exists.

As a troubleshooting measure, assuming that you are looping through the myLibrary.hangmanLetter table with index i, add this statement to your loop to try and get some more details on what might be causing the error:

if event.target.text == nil or myLibrary.hangmanLetter[i] == nil then print("UNEXPECTED NIL ON INDEX " .. tostring(i)) if event.target.text == nil then print("event.target.text is nil!") elseif myLibrary.hangmanLetter[i] == nil then print("myLibrary.hangmanLetter[" .. tostring(i) .. "] is nil!" end break end

If you put that directly above your if utf8.match… statement, it should help identify which table element is an unexpected nil.

Hope this helps - let us know if/when you sort it out.

I pasted it above my code and this is what I got when I typed in the word “Søppel”.  I dont quite understand this as everything worked before I decided to add utf8 :frowning:

EDIT

Okay so for some reason your code made it work now, it still prints out UNEXPECTED NIL ON INDEX 7.

Keeps on breaking if I remove your code so it must be doing some magic…

Maybe I should have used utf8.codepoint as a way to translate the letters? And thanks alot for all the help so far,  I really appreciate it! 

EDIT 2

I discovered that I did not close correctly your code posted above, it made my code run x amount of times based on how many letters it contains. That makes it works but I cant figure out why so far.

Nov 08 09:32:02.912 S Nov 08 09:32:02.944 UNEXPECTED NIL ON INDEX 7 Nov 08 09:32:02.944 myLibrary.hangmanLetter[7] is nil! Nov 08 09:32:04.939 Ø Nov 08 09:32:04.960 UNEXPECTED NIL ON INDEX 7 Nov 08 09:32:04.960 myLibrary.hangmanLetter[7] is nil! Nov 08 09:32:06.222 P Nov 08 09:32:06.244 UNEXPECTED NIL ON INDEX 7 Nov 08 09:32:06.244 myLibrary.hangmanLetter[7] is nil! Nov 08 09:32:06.919 P Nov 08 09:32:06.940 UNEXPECTED NIL ON INDEX 7 Nov 08 09:32:06.940 myLibrary.hangmanLetter[7] is nil! Nov 08 09:32:07.909 E Nov 08 09:32:07.932 UNEXPECTED NIL ON INDEX 7 Nov 08 09:32:07.932 myLibrary.hangmanLetter[7] is nil! Nov 08 09:32:08.261 L Nov 08 09:32:08.283 UNEXPECTED NIL ON INDEX 7 Nov 08 09:32:08.283 myLibrary.hangmanLetter[7] is nil!