Jump to content

onColShapeHit with a table of colRectangles


Mojko

Recommended Posts

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
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
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

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 by Tails
Link to comment
  • Discord Moderators

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.

  • Like 1
Link to comment

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 by Mojko
Link to comment

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

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...