Scripting Moderators ds1-e Posted June 3, 2019 Scripting Moderators Share Posted June 3, 2019 (edited) 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 June 3, 2019 by majqq Link to comment
Moderators IIYAMA Posted June 3, 2019 Moderators Share Posted June 3, 2019 The keyword break does not stop the code block from executing. It only stops the next loops. 1 Link to comment
Scripting Moderators ds1-e Posted June 3, 2019 Author Scripting Moderators Share Posted June 3, 2019 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
Moderators IIYAMA Posted June 3, 2019 Moderators Share Posted June 3, 2019 4 hours ago, majqq said: It's clean. So what i should do in that case? Additional condition doesn't help. The else statement does not help? Link to comment
Scripting Moderators ds1-e Posted June 3, 2019 Author Scripting Moderators Share Posted June 3, 2019 (edited) 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 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 June 3, 2019 by majqq Link to comment
Moderators IIYAMA Posted June 3, 2019 Moderators Share Posted June 3, 2019 @majqq hmm I just wondering how your code works. Shouldn't this make more sense? -- 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 1 Link to comment
Scripting Moderators ds1-e Posted June 3, 2019 Author Scripting Moderators Share Posted June 3, 2019 (edited) 2 hours ago, IIYAMA said: @majqq hmm I just wondering how your code works. Shouldn't this make more sense? -- 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 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 June 3, 2019 by majqq Link to comment
Moderators IIYAMA Posted June 4, 2019 Moderators Share Posted June 4, 2019 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) 1 Link to comment
Scripting Moderators ds1-e Posted June 4, 2019 Author Scripting Moderators Share Posted June 4, 2019 (edited) 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 June 4, 2019 by majqq Link to comment
Moderators IIYAMA Posted June 4, 2019 Moderators Share Posted June 4, 2019 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. This code will search for the actual col. Trigger: Matching Action: move to the next one. @majqq 1 Link to comment
Scripting Moderators ds1-e Posted June 4, 2019 Author Scripting Moderators Share Posted June 4, 2019 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. This code will search for the actual col. Trigger: Matching Action: move to the next one. @majqq Works, thank you so much 1 Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now