Finding an empty slot in array and using it -
so have problem have been messing while in squirrel. want create goes through inside array, e.g:
local array = [1, 2, 3, -1, 5, -1, 7, -1];
and -1, want use 1 of them, 1 comes first (which in case between 3 , 5).
in script, when destroys object, sets objects id -1 instead of regular number, because otherwise through out errors when trying use object. don't know if i'm doing job explaining this.
another example when players connect game server, have players id 0, 1, 3 , 4 connected, when next player connects id 2. thing i'm after arrays.
i have tried several things, thing closest this:
for(local i=1; < array.len(); i++){ if(array[i].id != -1){ count++; } else { count = i; } }
but problem 1 takes last id , replaces it, have objects 0, 1, 2 , 3 if destroy 3 , create new one, no problem. if destroy either 0, 1 or 2 instead , create new 1 not take slot, instead go number 4, not work.
thanks reading, out there can me this.
solved:
simple, else same problem did. follow along now, can tricky.
did added "break;" in else statement, code below:
for(local i=1; < array.len(); i++){ if(array[i].id != -1){ count++; } else { count = i; break; // } }
yes, did hit myself in head later...
array.find(-1)
from documentation:
performs linear search value in array. returns index of value if found null otherwise.
Comments
Post a Comment