Mojko Posted July 9, 2017 Share Posted July 9, 2017 Hi so I have this table with colRectangles that I wanna check collision for. pickupCollision[index][COL_ID] = createColRectangle(x, y, 4, 4) This is how i define the collision rectangles, and now I'm trying to loop through pickupCollision to check collision. This is what I have at the moment: function playerCollisionWithPickup(player, dimension) outputChatBox("player entered the col shape", player) end for i = 1, #pickupCollision do addEventHandler("onColShapeHit", pickupCollision[i][COL_ID], playerCollisionWithPickup) end WARNING: pickupController.lua:93: Bad argument @ 'addEventHandler' [Expected element at argument 2, got nil] Link to comment
Moderators Citizen Posted July 9, 2017 Moderators Share Posted July 9, 2017 Is COL_ID a global string ? Are x and y valid everytime ? (during your creation loop) Copy that var_dump function and use it after the creation process to check the content of your table: var_dump("-v", pickupCollision) Link to comment
Mojko Posted July 9, 2017 Author Share Posted July 9, 2017 1 hour ago, #BrosS said: show us the tables local X, Y, Z, ID, TYPE, PICKUP_ID, DESCRIPTION, COL_ID = 1, 2, 3, 4, 5, 6, 7, 8, 3 --Start of initilizing 2d array local pickupCollision = {} for i = 1, maxPickups do pickupCollision[i] = {} for j = 1, 3 do pickupCollision[i][j] = 0 end end --End of initilizing 2D array Link to comment
pa3ck Posted July 10, 2017 Share Posted July 10, 2017 local X, Y, Z, ID, TYPE, PICKUP_ID, DESCRIPTION, COL_ID = 1, 2, 3, 4, 5, 6, 7, 8, 3 COL_ID = 8, you only have 1-3 in the table for i = 1, maxPickups do pickupCollision[i] = {} for j = 1, 3 do --> from 1 to 3, 8 will not be defined pickupCollision[i][j] = 0 end end Link to comment
Tails Posted July 10, 2017 Share Posted July 10, 2017 (edited) Not sure exactly what you're trying to do but, here's just a really quick example of what you could do. You really only need one event handler. local positions = { {x = 3, y = 3, w = 4, h = 4}, {x = 8, y = 8, w = 4, h = 4}, } local colshapes = {} for i=1, #positions do local pos = positions[i] local col = createColRectangle(pos.x, pos.y, pos.w, pos.h) colshapes[col] = {} -- you could attach more data here and get it within onColShapeHit end addEventHandler("onColShapeHit", root, function(player, dimension) if colshapes[source] then outputChatBox("player entered the col shape", player) end end) Hope this helps. Edited July 10, 2017 by Tails Link to comment
Discord Moderators Pirulax Posted July 11, 2017 Discord Moderators Share Posted July 11, 2017 I would change that root to just resourceRoot, because the eventHandler is in the same resource.You can save a little bit of cpu. addEventHandler("onColShapeHit", resourceRoot, function(player, dimension) if colshapes[source] then outputChatBox("player entered the col shape", player) end end) btw i think everything is working fine. 1 Link to comment
Mojko Posted July 12, 2017 Author Share Posted July 12, 2017 (edited) Thanks everyone for your answers, apparently the problem wasn't a problem at all. pa3ck highlighted a big error I made which I solved, but it wasn't the entire solution. The issue was that I looped too many times, so I looped more times than actual pickups existed. Clarification: I looped 4 times while I only had 2 pickups. Edited July 12, 2017 by Mojko Link to comment
Simple0x47 Posted July 12, 2017 Share Posted July 12, 2017 Do it like this: local colShapes = {} colShapes[ 1 ] = createColRectangle( ... ) -- INSERT THE ARGUMENTS colShapes[ 2 ] = createColRectangle( ... ) function onHit() if ( source ) then for i = 1, #colShapes do if ( colShapes[ i ] == source ) then -- DO WHATEVER YOU WANT break end end end end addEventHandler( "onColShapeHit", root, onHit ) 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