- Jun 10, 2017
- 1,581
- 1,994
- 407
I realized that_personsin the code I quoted is actually an array of integers and not objects, so my point about your code is invalid and that's not the reason for the game freezing.
EDIT 1
After yet another look, I believe that the problem is with this part of the code:
Using something likeJavaScript:setup.getRandomPersonIds = function(persons, limit = 2) { var randomIds = []; var randomPersonList = clone(persons); while (randomIds.length < limit && randomPersonList.length) { var randomIndex = Math.floor(Math.random() * randomPersonList.length); if (!randomIds.includes(randomIndex)) { randomIds.push(randomIndex); randomPersonList.splice(randomIndex, 1); } } return randomIds; };SugarCube.setup.getRandomPersonIds([3, 5], 2)can cause an infinite loop. In the code,randomIndexis an integer that's either 0 or 1 (sincepersonsis a 2 element array here). SorandomIdsis going to be filled with either 0 or 1 (which is not what you want, since you actually want it to be 3 or 5 in this example, but that's a separate issue).
The infinite loop happens if the first time through the loop,randomIndexis 0. NowrandomPersonListhas a length of 1 and so afterwards,randomIndexwill always be 0 and thus doesn't get pushed intorandomIds. SorandomIdsstays with length 1 and the loop repeats.
Side note, but SugarCube's creator added many custom methods to the Array class, so that whole function could be written like so:
JavaScript:setup.getRandomPersonIds = function(persons, limit = 2) { return [].concatUnique(persons).randomMany(limit); }
EDIT 2
Changing that function seems to have fixed the problem for me.
Problem actually was not with the getRandomPersonIds BUT getRandomPersons as there wasn't check in while on original array limit. So without any elements inside it it still tried to find another person.
But as decker666 mentioned. It has changed in 0.27 and that shouldn't be problem there anymore.