Jump to content

getElementsWithinColShape() function


Krom

Recommended Posts

Hello. The task is to check if I have any vehicles nearby and return their names, if any. Here's a code I use in Cient-Side script:

  
aPlayer = getLocalPlayer()  
bindKey("l","down", 
        function() 
            if isPlayerInVehicle(aPlayer) == false then 
                local playerX,playerY,playerZ = getElementPosition(aPlayer) 
                local col = createColSphere(playerX,playerY,playerZ,5) 
  
                --Main part begins just below 
  
                local vehicles_tab = getElementsWithinColShape(col,"vehicle") 
                if (vehicles_tab) then 
                    outputConsole("vehicles:") 
                    outputConsole(tostring(#vehicles_tab)) 
                    for k,v in ipairs(vehicles_tab) do 
                        local name = getVehicleName(v) 
                        outputConsole(name) 
                    end 
                end 
  
                -- End of check 
  
            end 
        end 
    ) 
  

Line 13 appears, line 14 returns 0 (Zero) to a console, so you may easy guess that line 17 won't get me anything. When run Server-side, the function works well, giving me a list of vehicle names, but I need this running on local client PC and wiki: http://development.mtasa.com/index.php?title=GetElementsWithinColShape says me that It's possible. A bug? Any ideas?

Link to comment

Nothing for "player" (but it should counts myself inside the sphere). When checked serverside, the lenght of table is 2 (me and vehicle), when clientside - 0. Nothing for "object" and "pickup", however I couldn't detect them serverside as well (both created dynamically via console command). I'm not too much experienced in everything connected to MTA, but if wiki shows "getElementsWithinColShape - Client and Server function" with one common example of how the function works, then I guess it all should be ok.

Link to comment
  • 2 years later...

I'd like to bump this bearded topic since I still have some questions left.

The reason of non-working was obvious: GetElementsWithinColShape() have some limitations client-side as said there. I don't mind being tolerant of that limitations, but how the heck can I make my custom/virtual/dummy/non-entity element to move so it could be detected by colshape?

And that's how I failed moving it:

addCommandHandler( "doCheck", 
    function( x, y, z, e, v, c ) 
        x, y, z = getElementPosition( getLocalPlayer() ) 
        c = createColSphere( x, y, z, 10 ) --// Create a colshape so it's there before elements to check 
        setTimer( --// Wait for a bit just to make sure =) 
            function() 
                e = createElement( "anElement", "anElementID" ) --// A custom element 
                setElementPosition( e, x, y + 3, z )  --// Move it to us from ( 0,0,0 ) 
                v = createVehicle( 400, x, y + 3, z )  --// A vehicle 
                 
                --// Check isElementWithinColShape() 
                outputChatBox( "\nvehicle: " .. tostring( isElementWithinColShape( v, c ) ) ) 
                outputChatBox( "custom: " .. tostring( isElementWithinColShape( e, c ) ) .. "\nlist:" ) 
                 
                --// Check getElementsWithinColShape() 
                for i, v in ipairs( getElementsWithinColShape( c ) ) do 
                    outputChatBox( i .. ": " .. getElementType( v ) ) 
                end 
                 
                --// Move custom element and check again 
                outputChatBox( "#one more time" ) 
                setElementPosition( e, x, y + 1, z ) --// Since this is not what means "to move" - I give up =/ 
                 
                --// Here goes the same result again 
                outputChatBox( "vehicle: " .. tostring( isElementWithinColShape( v, c ) ) ) 
                outputChatBox( "custom: " .. tostring( isElementWithinColShape( e, c ) ) .. "\nlist:" ) 
                for i, v in ipairs( getElementsWithinColShape( c ) ) do 
                    outputChatBox( i .. ": " .. getElementType( v ) ) 
                end 
            end 
        , 500, 1 ) 
    end 
) 

Any suggestions are welcome.

P.S. I know I can do the check myself, just want to know if collision functions suitable for use with custom elements ;)

Link to comment

I can't be 100% certain about this off the top of my head, but I believe dummy elements don't have a position. Only elements backed by an 'entity' do - something that exists in GTA.

So, obviously, assuming that's true, dummy elements can't be inside or outside a colshape.

Link to comment

from my experience colshapes can only detect peds and vehicles that are synced (isElementWithinColShape on non synced element will return a last synced state)

if you want to detect other types of elements, you need to write a set custom function to chcek if the element is at given coordinates

Link to comment

Wojak, then how is it called getElementsWithinColShape when it doesn't support anything except peds/vehs. Then it should be called getPedsOrVehsWithinColShape or something like that. It should detect all types of elements.

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