for i=0,i<10,1 do if dset(i, variable) exists dset(i+1,variable); elseif dset(i,variable) doesnt exist dset(i,variable); end end |
I want to do something like the code above.
I know the code isn't right (somehow user must break the for loop)but it's just an idea.
but I don't know if it is possible to do so.
scores should be saved everytime user finishes the game but to implement that feature, I should check if previous value in dset returns something.
If user finished game 10 times, 10 scores should be saved
It can never return nothing. The cartdata is initialized with 0 as a value for all indexes.
Also, your code wouldn't work even if it weren't pseudocode: As soon as you find the first index with a value, you overwrite the following value.
You can read a value with dget(). So maybe just something like this?
for i=0,63 do --or 10? if dget(i) == 0 then dset(i, variable) break end end |
And then decide what should happen if all 64 (or 10?) score slots are full, and handle that case accordingly.
tobiasvl) yes I think your code is that code which I want.
but I am not sure. does dget(i)==0 work?
What do you mean? Work how? It will work, but what are you worried about exactly? Should be pretty easy to test this. It will not distinguish between an empty save slot or a saved score of 0, though, if that's possible to get in your game (and you want to save those runs too).
I just was unsure with that grammatically
I thought if dget(i)==0 means if
value in dget(i) equals to 0
or if dget(i) doesn't exist
Yes, that's correct. Like I said in my first reply, the cartdata is all initialized to 0. There is no way for the cartdata to not exist once cartdata() has been called, since it's a bunch of bytes in memory. They can't be "nil"; you can consider them to be int variables that have already been initialized, if you want. And like I said in my second reply, this means you can't see the difference between an uninitialized value and a value that has been set to 0.
Thank you!
Didn't know that.
So, according to your answer, I guess if I want to reset all my dset data, just do as the code under?
for i=0,10 do if(dget(i)>0) then dset(i,0); end end |
right?
Yes. Or just:
for i=0,10 do dset(i,0) end |
I assume doing an if test and a dget() every time isn't faster than the dset() it might skip. I haven't benchmarked it though.
The solution described so far is absolutely fine in context, but I wanted to suggest something for other contexts where (nearly) any number is valid.
One thing you could do is to initially fill the space with a value you'd expect never to see in your save data. In this case, you're content to assume 0 would not be there unless the entry had never been used, but if you knew 0 might be a valid entry in your save data, then you'd want to pick something else which is either impossible or very unlikely outside of a bug.
For me, the most unlikely value is usually 0x8000, which on pico-8 represents -32768, the most negative number.
The main reason I choose that number, even though technically it's a valid number, is that it's actually as special, in twos-complement binary number format, as zero is. Just as -0 == 0, -0x8000 == 0x8000. It's just a side effect of how numbers are encoded--you'd need one more bit to differentiate between positive and negative versions of this number. It's only deemed negative by convention. But it also means that this number can be troublesome to use and thus might be a good choice as one you would never expect/want to use for valid data.
(More detail: In any two's-complement system, the number that is all zeros except its top bit works this way. So for 32-bit integers, it'd be 0x80000000. It's sort of the complement of 0x00000000, the value you get at the other location where incrementing or decrementing changes the sign. Personally, I've started calling this the nadir value, since it's the lowest-possible value.)
[Please log in to post a comment]