Page 1 of 1

Can you scan a table to find specific values?

Posted: Sun May 28, 2017 12:55 am
by icekiller8002
So I have two values:

banned_ic = {1,3,5,10,13,17,22,24}
ic = 0

Every time I press SPACE or ENTER, the ic adds itself by 1:
ic = ic + 1

However, if I press SPACE or ENTER and the ic is already at one of the banned ic values, it's not supposed to add itself. How would I make something like this?

Re: Can you scan a table to find specific values?

Posted: Sun May 28, 2017 1:09 am
by zorg
You actually have two variables, and you have 9 values in total.

In any case, there's two solutions:
- Not modifying what you have now, write a loop checking whether ic + 1 is in the table, and not incrementing it if it is.
- Modifying the banned_ic table a tiny bit, namely swapping the keys and the values... let me elaborate this one with code:

Code: Select all

-- Fast
banned_ic = {[1] = true, [3] = true, [5] = true, [10] = true, [13] = true, [17] = true, [22] = true, [24] = true}
ic = 0

-- Your key detection code:
if not banned_ic[ic + 1] then
    ic = ic + 1
end
The above way is faster than if you needed to iterate over the whole table:

Code: Select all

-- Slower
banned_ic = {1,3,5,10,13,17,22,24}
ic = 0

-- Your key detection code:
for i,v in ipairs(banned_ic) do
    if ic + 1 ~= v then
        ic = ic + 1
        break
    end
end


Re: Can you scan a table to find specific values?

Posted: Sun May 28, 2017 1:19 am
by icekiller8002
I hate to say this, but neither of those work.

The faster version never lets me continue the dialog with SPACE or ENTER.

And the slower version always lets me continue the dialog with SPACE or ENTER, even if the ic value is banned.

EDIT: Normally, it'd be fine if the slower version was bugged. However, some players looking for glitches might discover that you're allowed to continue the dialog even if the ic value is banned.

Re: Can you scan a table to find specific values?

Posted: Sun May 28, 2017 1:28 am
by MrFariator
Can you post your code with either or both of the approaches zorg wrote about?

Re: Can you scan a table to find specific values?

Posted: Sun May 28, 2017 1:32 am
by icekiller8002
UPDATE: So I used a really cheap method and it somehow works?

Method:
if ic ~= 1 and ic ~= 3 and ic ~= 5 and ic ~= 10 and ic ~= 13 and ic ~= 17 and ic ~= 22 and ic ~= 24 then
ic = ic + 1
end

The weird thing: I've used this method before it suddenly broke. Now I retype it, and it works just fine? I have no idea.

Re: Can you scan a table to find specific values?

Posted: Sun May 28, 2017 1:46 am
by zorg
Actually, the only issue i can see with my code is that they should check ic, not ic+1, since you did write that it should not add itself if the ic is already at one of the banned values; i misread that.

Re: Can you scan a table to find specific values?

Posted: Sun May 28, 2017 3:33 am
by Sir_Silver
Yup zorg's solution looks like the best way to handle this.