Jump to content

Loop table problem


ds1-e

Recommended Posts

  • Scripting Moderators

Hey. I need a little help with int loop. So basically, i need to insert colshape (which shouldn't exist in table), with table.insert, however it doesn't work as it should, most likely it's due of my mistake, but i can't figure it out.

local saved_cols = #playerTable.player.savedcols
if saved_cols == 0 then -- in case if table is empty, otherwise loop wouldn't run
	table.insert(playerTable.player.savedcols, colshape)
	outputChatBox(saved_cols) -- output: 0
end

-- loop with problem
for i = 1, #playerTable.player.savedcols do
	local saved_col = playerTable.player.savedcols[i]
	if saved_col == colshape then
		outputChatBox("saved_col == colshape, break")
		break -- uhm, should break and not add colshape to table?
	end
	table.insert(playerTable.player.savedcols, colshape) -- in case of any questions, i tried to add condition here but that doesn't help
end

So what's wrong:

1. Spawning car with attached colshape, script trigger onClientElementColShapeHit event with my function.
2. First colshape adds successfully.
3. Spawning car once again, and table.insert add 2x same colshape instead of 1.
4. Condition works good only for first colshape added in table.

 

Edited by majqq
Link to comment
  • Scripting Moderators
8 hours ago, salh said:

what debugscript say?

It's clean.

4 hours ago, IIYAMA said:

The keyword break does not stop the code block from executing. It only stops the next loops.

So what i should do in that case? Additional condition doesn't help.

Link to comment
  • Scripting Moderators
3 hours ago, IIYAMA said:

The else statement does not help?

Yes, even with else. But i noticed something, for some reason it only happens, after entering 3rd colshape or more.

EDIT: I changed conditions a bit, and it's working, if something will change i will give u answer :P Thank you for your time.

Fixed [?] code:

for i = 1, #playerTable.player.savedcols do
	local saved_col = playerTable.player.savedcols[i]
	if colshape == saved_col then
		break
	end
	if colshape ~= saved_col and i == #playerTable.player.savedcols then
		table.insert(playerTable.player.savedcols, colshape)
	end
end

 

Edited by majqq
Link to comment
  • Moderators

@majqq

hmm I just wondering how your code works.

Shouldn't this make more sense? :robot:

-- loop with problem
local insertCol = true

for i = 1, #playerTable.player.savedcols do
	local saved_col = playerTable.player.savedcols[i]
	if saved_col == colshape then
		outputChatBox("saved_col == colshape, break")
		insertCol = false
		break -- uhm, should break and not add colshape to table?
	end
end

if insertCol then
	table.insert(playerTable.player.savedcols, colshape) -- in case of any questions, i tried to add condition here but that doesn't help
end

 

  • Like 1
Link to comment
  • Scripting Moderators
2 hours ago, IIYAMA said:

@majqq

hmm I just wondering how your code works.

Shouldn't this make more sense? :robot:


-- loop with problem
local insertCol = true

for i = 1, #playerTable.player.savedcols do
	local saved_col = playerTable.player.savedcols[i]
	if saved_col == colshape then
		outputChatBox("saved_col == colshape, break")
		insertCol = false
		break -- uhm, should break and not add colshape to table?
	end
end

if insertCol then
	table.insert(playerTable.player.savedcols, colshape) -- in case of any questions, i tried to add condition here but that doesn't help
end

 

I will need your advice once again :P

So i will need to switch colshapes between them, all are in table. This code allows to switch only between 1st and 2nd in table.

for i = 1, #playerTable.player.savedcols do
	local saved_col = playerTable.player.savedcols[i]
	if saved_col ~= playerTable.player.colshape then -- if colshape in table it's different from actual player col
		playerTable.player.colshape = saved_col -- set new col
		refreshLootInventory(playerTable.player.colshape)
		break
	end
end

For example, 10 colshapes in table. Switch from 1st, to 10th. If it reach 10th, start over.

Edited by majqq
Link to comment
  • Moderators
8 hours ago, majqq said:

For example, 10 colshapes in table. Switch from 1st, to 10th. If it reach 10th, start over. 

Do you mean moving the first colshape to last position in the table? And cycle that.

 

Or do you mean going over the index from any given index: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, etc.

 

Or start cycle the index at the point where they match? 1, 2, 3, 4 = matching, 5, 6, 7, 8 ,9 10

Starting the cycle at index 4.

5, 6, 7, 8, 9, 10, 1, 2, 3 (stop)

 

  • Like 1
Link to comment
  • Scripting Moderators
2 hours ago, IIYAMA said:

Do you mean moving the first colshape to last position in the table? And cycle that.

 

Or do you mean going over the index from any given index: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, etc.

 

Or start cycle the index at the point where they match? 1, 2, 3, 4 = matching, 5, 6, 7, 8 ,9 10

Starting the cycle at index 4.

5, 6, 7, 8, 9, 10, 1, 2, 3 (stop)

 

Going over and over, colshape don't need to be moved, i just need to set new one:

-- with
playerTable.player.colshape = saved_col -- set new col

For example:

1 (actual player colshape) -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> 1 -> 2 -> 3 and so on.

3 (actual player colshape) -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> 1 -> 2 -> 3 and so on.

To make things more understandable, let's say i have a lot of colshapes in one place, after i click button, it switch to next, without moving from place (standard colshape system isn't good enough.)

Edited by majqq
Link to comment
  • Moderators
for i = 1, #playerTable.player.savedcols do
	local saved_col = playerTable.player.savedcols[i]
  
	if saved_col == playerTable.player.colshape then -- finding it
    	
		playerTable.player.colshape = playerTable.player.savedcols[(i + 1) <= #playerTable.player.savedcols and (i + 1) or 1] -- set new col
		break
	end
end

 

I still not understand why you use the ~= operator. So I left that one out for now.

 

  1. This code will search for the actual col.
  2. Trigger: Matching
  3. Action: move to the next one.

@majqq

 

  • Like 1
Link to comment
  • Scripting Moderators
26 minutes ago, IIYAMA said:

for i = 1, #playerTable.player.savedcols do
	local saved_col = playerTable.player.savedcols[i]
  
	if saved_col == playerTable.player.colshape then -- finding it
    	
		playerTable.player.colshape = playerTable.player.savedcols[(i + 1) <= #playerTable.player.savedcols and (i + 1) or 1] -- set new col
		break
	end
end

 

I still not understand why you use the ~= operator. So I left that one out for now.

 

  1. This code will search for the actual col.
  2. Trigger: Matching
  3. Action: move to the next one.

@majqq

 

Works, thank you so much :D

  • Like 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...