## Can you scan a table to find specific values?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
icekiller8002
Prole
Posts: 39
Joined: Mon Jun 06, 2016 9:28 pm
Location: United States

### Can you scan a table to find specific values?

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?

Code: Select all

function love.draw()
love.graphics.print("obey")
end


zorg
Party member
Posts: 3003
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

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

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

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

for i,v in ipairs(banned_ic) do
if ic + 1 ~= v then
ic = ic + 1
break
end
end


Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

icekiller8002
Prole
Posts: 39
Joined: Mon Jun 06, 2016 9:28 pm
Location: United States

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

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.

Code: Select all

function love.draw()
love.graphics.print("obey")
end


MrFariator
Party member
Posts: 173
Joined: Wed Oct 05, 2016 11:53 am

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

Can you post your code with either or both of the approaches zorg wrote about?

icekiller8002
Prole
Posts: 39
Joined: Mon Jun 06, 2016 9:28 pm
Location: United States

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

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.

Code: Select all

function love.draw()
love.graphics.print("obey")
end


zorg
Party member
Posts: 3003
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

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

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.
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

Sir_Silver
Party member
Posts: 286
Joined: Mon Aug 22, 2016 2:25 pm
Contact:

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

Yup zorg's solution looks like the best way to handle this.

### Who is online

Users browsing this forum: No registered users and 35 guests