Davidobot / love.js

LÖVE ported to the web using Emscripten, updated to the latest Emscripten and LÖVE (v11.5)
MIT License
605 stars 27 forks source link

Source:setPitch crashes game when pitch value less than one. #51

Closed anatolykopyl closed 2 years ago

anatolykopyl commented 2 years ago

As described in the title, game crashes with the following message:

Error: monkey.lua:107: Pitch has to be non-zero, positive, finite number.

Referenced line 107:

monkey[n].walkingSound[1]:setPitch(math.random(0.8, 0.9))

Built using compatability version.

alexjgriffith commented 2 years ago

For Lua 5.1 the use of floats as arguments to math.random is undefined behaviour.

It looks like in luajit the return value defaults to using the lower bound (in this case 0.8) and in Lua 5.1 PUC (normal non jit lua) it defaults to 0. This is why you don't get an error when you run it on windows or linux, but would if you ran it on the web or on mac.

We have to use Lua 5.1 PUC for the web and mac targets as there is no luajit support. Much of luajit is written in platform specific assembly.

Using one of the three defined behaviours for math.random fixes this problem. My recommendation for generating a range from 0.8 - 0.9 is:

monkey[n].walkingSound[1]:setPitch(math.random() / 10 + 0.8)