SCRMaps
/
Sign in

Reading a counter as a number

Conditions give you true or false. What if you need the number?

3 min read

Deaths() only answers questions like “is it at least 100?”. When you need the actual number you narrow it down with a binary search.

var level = 0;
if(level + 512 <= 1000 && Deaths(CurrentPlayer, AtLeast, level + 512, "Level")){ level = level + 512; }
if(level + 256 <= 1000 && Deaths(CurrentPlayer, AtLeast, level + 256, "Level")){ level = level + 256; }
if(level + 128 <= 1000 && Deaths(CurrentPlayer, AtLeast, level + 128, "Level")){ level = level + 128; }
// ... 64, 32, 16, 8, 4, 2, 1

A maximum of 1000 takes ten lines. The count grows logarithmically, so even two billion is 31 lines.

Writing this by hand is tedious but entirely mechanical - SCRMaps' own save/load generator emits exactly this pattern for every value you choose to store.

You do not have to write it by hand either. EUDBinaryMax does this search for you: give it a condition and it returns the largest value for which the condition holds. The ten lines above become one.

const level = EUDBinaryMax(function(x) {
    return l2v(Deaths(CurrentPlayer, AtLeast, x, "Level"));
}, 0, 1000);
Always pass the ceiling: the default range is 0 to 0xFFFFFFFF and takes 32 steps, where a bound of 1000 takes ten. l2v is needed because a condition is not a value.
Sign in to track your progress