LuaJit vs number size & doubts about my game

Hi all!

I have a question related to LuaJit and numbers: how does Lua in Defold represent numbers? Are all numbers 64 bit double values with a 52 bit mantissa like it is the case in Lua 5.1?

The reason I’m asking is that I need about 45 bits to represent some integer values (hashes of my own design). I want to know if LuaJit in Defold uses 32 bit integers or not. I would assume when you build for Windows 64 bits then we have 64 bit wide values but I’m asking anyway.

I am also having doubts with respect to my current game. It uses a lot of screen real estate. I tried a HTML5 build on my phone and performance is great with the dictionary and all, but my letter tiles are tiny and the game is unplayable because of this. It would work on a tablet though.

Another doubt I have is that I can hardly localize the game. I have found MIT licensed word dictionaries for English, French and Spanish, maybe I could find other languages too but the game would be niche. I would have to distribute it as HTML5 also as Steam users are not that much into word games.

So… all this to say I am currently debating whether or not I will continue the project. If not, I might distribute the 3 dictionaries with Lua modules for interacting with them in the Defold Assets store so that someone interested in building a word game can use the package.

Thoughts?

Integers are 32 or 64 bits depending on build target. HTML5 is the spoiler, as there’s no 64 bit target for it. Please be aware that HTML5 uses vanilla Lua 5.1 while the other platforms uses LuaJIT (i.e. different Lua implementations).

Numbers in Lua (as configured by Defold) will always be 64 bit doubles, with however double is implemented on the platform (e.g. are they IEEE 754, and are they rounded up or down). In C++ extensions, you will get and set these as lua_Number. It’s defined like this:

#define LUA_NUMBER double
typedef LUA_NUMBER lua_Number;

Unfortunately, this means that in Lua code you don’t have access to the full 64 bit integer range, even on 64 bit platforms, since double cannot represent the full range. Additionally, if you plan to release on web, you may want to avoid integer values greater than 32 bit in C++ extensions when possible, since Lua’s own integer type will be 32 bit.

If you want to do integer operations in C++, you can call lua_tointeger to convert a lua_Number to a lua_Integer, which is defined like this:

#define LUA_INTEGER	ptrdiff_t // will be 32 or 64 bit depending on platform
typedef LUA_INTEGER lua_Integer;

To assign a Lua variable a lua_Integer value, you can call lua_pushinteger which will convert the integer to a lua_Number before assigning it. You can convert lua_Number to int and lua_Integer with lua_number2int and lua_number2integer respectively.

If you want to do bit operations to hash strings, you may be able to use the builtin bit module and make sure you work with integer values no greater than 32 bit. The bit module converts between number and integer internally. Alternatively, you can write a C++ extension, allowing you to more freely operate on integer values before converting back to number/double so that they can be passed to Lua code.

5 Likes

Thanks for the thorough reply. I think I didn’t express myself as I wanted to : I am ok with using doubles. All I need is enough bits in the mantissa - in my case 45 to 50 let’s say - double values have 51 bit mantissa so I am ok if the platform supports those. If I understand your point, in HTML5, Defold uses Lua 5.1 where numbers are all doubles. So on HTML5 I would be fine. And so should I on Windows 64.

My hash function computes an integer, yes, but I don’t need any bit manipulation function (e.g. the bit module). I compute an integer value but it is stored in a double. What I am doing is simply something like:

hash =   (part3 * 64 + part2) * 8 + part1

here, part1 is a double but its value is in {0, 1, 2, 3, 4, 5, 6, 7}, a 3 bit maximum integer (thus why I multiply by 8, I use multiplications as opposed to shifts), part2 is 6 bit value maximum (multiplication by 64) and part 3 is any length. So in fact I always manipulate double values only they have no fractional part. So they are really integers.

So as long as I can use doubles (64 bits) I am ok.

1 Like

Yes, you’re correct — you will be fine. I forgot to directly answer your question while writing all that. :sweat_smile:

Regarding the game itself, I have never made a game with a dictionary. But it seems like it would be difficult to localize to any language I don’t know myself. It can be frustrating as a player to enter a word, and then to find out that the game’s dictionary doesn’t include it. Without knowing the language, I imagine it’s hard to evaluate the quality of the dictionary. But maybe it’s possible to rely on something standardized, like the set of dictionaries that are legal in competitive Scrabble.

For your project, I would think about what you want to get out of it. Is it for learning Defold, to finish and release a game, to make a commercial product, develop for fun/satisfaction etc. Maybe that will help you figure out if you should continue with this project or go with another.

1 Like

Thanks for another kind reply. Your post was super informative, I learnt a lot about how Defold uses integers, I think I didn’t express myself clearly in the first place :grin: .

You are correct it is difficult to evaluate a foreign language dictionary. But one metric we can always use is the number of words in it. The English dictionary I found has 275,000 words; the Spanish one 636,000 and the French one 336,000. The counts would be less than these since we would likely have restrictions on word length, but 200,000+ words is a solid base to build on. I agree using a correct word that is not in the dictionary would be frustrating - especially since languages are dynamic and new common words are added every year so the likelihood is there - but that’s the best we can do. Unless we use an online dictionary and query it each time but that’s not ideal.

To come back to the reasons I started the project, I thought, in short, something like:

  1. code first Defold game
  2. ?
  3. profit

:joy: that’s a bit naïve. More seriously, I’m just starting out in Defold. I am liking it and Lua a lot and learnt a ton. Part of the reasons why I started the project is that it’s an experiment. I’d like to see what it is to release a game and have people hopefully enjoying it. I would not say no to a few bucks too but I am realistic in that the genre I chose (and especially if I go web) is not exactly the one with the greatest potential for profit.

I am questioning myself now because I also found out coding a game of a reasonable size is A. Lot. Of. Work. And if my niche is too niche then maybe my efforts are a bit vain. But then I tell myself not a lot of people code word games and there are nonetheless very popular ones. You mention Scrabble. There are a lot of Scrabble fanatics. And Scrabble can hardly be played on a phone too - at least I don’t think so. Because of screen size. Another thing I thought of was to reuse the work I did on the dictionary for other word games that are tailored to phones. That’s another possibility.

I will be thinking this over. In the meantime I can always clean up the code I wrote to prepare for its introduction in Defold Assets store in the case I decide to upload it.

Thanks again for taking the time to write!

1 Like