Build #1123: physics.rayCast() "hit order" resolved.

Would you mind elaborating a bit?

This is my function (the raycast bit anyways)

-- Casts a ray to check whether or not the enemy tanks have a clear firing path function ETfire( self, user\_tank )          EDray = physics.rayCast( self.x, self.y, user\_tank.x, user\_tank.y, "sorted" )              local hitFirst = EDray[1]     local hitX, hitY = hitFirst.position.x, hitFirst.position.y          if EDray then         -- There's at least one hit.         print( "Hit count: " .. tostring( #EDray ) )         -- Output all the results.          for i,v in ipairs(EDray)  do             print( "Hit: ", i, v.object.class, " Position: ", v.position.x, v.position.y )     end         print( "The first object hit is: ", EDray[1].object.class )         else         -- There's no hit.     end     if ( EDray[1].object.class == "PlayerTank" or EDray[1].object.class == "Uturret" or EDray[1].object.class == "bullet") then              print("PlayerTank in range! Fire!")             timer.performWithDelay(40, function() ShootEnemyBullet( self, user\_tank ) end, 1) --- ShootEnemyBullet function and bullet code --- end  

I guess I’m just confused on how to remove an object from the hit list…
According to the docs, when a ray is cast it returns an array of the objects hit - which in my case I’m assuming is EDray - and I’m guessing I’d just do something like if EDray.object.class == “Radar” then – remove object from hit list –
Would I just use table.remove(EDray, 1) or is there a different way I should do it?

– Sorry for my lack of knowledge, I’ve only just recently optimized my code to make use of tables. –

-Saer

 

-- Casts a ray to check whether or not the enemy tanks have a clear firing path function ETfire( self, user\_tank ) EDray = physics.rayCast( self.x, self.y, user\_tank.x, user\_tank.y, "sorted" ) local hitFirst = EDray[1] local hitX, hitY = hitFirst.position.x, hitFirst.position.y if EDray then for i =#EDray,1,-1 do if EDray[i].object.class == "radar" then table.remove(EDray,i) end end if #EDray~=0 then -- hit something so do your magic if ( EDray[1].object.class == "PlayerTank" or EDray[1].object.class == "Uturret" or EDray[1].object.class == "bullet") then print("PlayerTank in range! Fire!") timer.performWithDelay(40, function() ShootEnemyBullet( self, user\_tank ) end, 1) end end end end

Man I hate this forum’s formatting, anyway the above would remove ALL radars in the hitlist and only leave the stuff you wanna hit.

haha yeah, it can be a pain sometimes.

Thank you for your help, jacques1. :slight_smile:

It works perfectly!
Adding the ray’s overall hit count as an additional condition in the ‘if’ statement was what I could not figure out.

Cheers!

-Saer