Jump to content

Loop cars and check if they are in colshapes


zixxonx

Recommended Posts

Posted

Hello, I have a table with 5 cuboids:

cuboid[1] = {x,y,z,10,10,10}

cuboid[2] = {x,y,z,10,10,10}

etc

and I wanna check if there is any vehicle on these cuboids, if no then break the table i tried it

  
for _,v in ipairs(getElementsByType('vehicle')) do 
            outputChatBox('checking from ' .. getVehicleName(v) .. ' perspective') 
            for i=1, 5 do 
                if isElementWithinColShape(v, cuboid[i]) then 
                    outputChatBox(i .. ' taken by ' .. getVehicleName(v)) 
                else 
                    outputChatBox(i .. ' free') 
                end 
            end 
        end 
  

So i spawn 2 cars at 2 cuboids, infernus at 1st, cheetah at 2nd

checking from Cheetah perspective 
1 free 
2 taken by Cheetah 
3 free 
4 free 
5 free 
checking from Infernus perspective 
1 taken by Infernus 
2 free 
3 free 
4 free 
5 free 

the problem is that Cheetah sees that cuboid 1 is free while it's taken and infernus sees 1 is free :(

Could you help me?

Posted

this?:

function isInAnyFiveCuboid() 
local vehicles = getElementsByType("vehicle") 
for k,v in ipairs(vehicles)do 
if(isElementWithinColShape(v,cuboid[1]) or isElementWithinColShape(v,cuboid[2]) or 
isElementWithinColShape(v,cuboid[3]) or isElementWithinColShape(v,cuboid[4]) or 
isElementWithinColShape(v,cuboid[5])) then 
outputChatBox(getVehicleName(v).."is in cuboid") 
else 
outputChatBox(getVehicleName(v).."is out cuboid") 
end 
end 
end 

Posted

Looks like it partially works. Now I wanted to make it that If a vehicle is in cuboid it adds +1 to the variable but it counts all the cars I have on the server

So if I have 5 cuboids for 5 parking places

cuboid[1]

cuboid[2]

cuboid[3]

cuboid[4]

cuboid[5]

And i have something like it:

  
(at top of the script) count = 0 
local vehicles = getElementsByType("vehicle") 
for k,v in ipairs(vehicles)do 
if(isElementWithinColShape(v,cuboid[1]) or isElementWithinColShape(v,cuboid[2]) or 
isElementWithinColShape(v,cuboid[3]) or isElementWithinColShape(v,cuboid[4]) or 
isElementWithinColShape(v,cuboid[5])) then 
count =count +1 
else 
createVehicle(foo) 
end 
end 

And I spawn first car then count is 1, but when I spawn second count is 2, and... when I'm trying to spawn the third count is 4 because it's add +1 2x to variable.

It's hard to explain but I want it done so badly. :P

Posted (edited)
count = 0 
takenColShapes = {} 
for _,v in ipairs(getElementsByType('vehicle')) do 
    for i=1, 5 do 
        if isElementWithinColShape(v, cuboid[i]) then 
            takenColShapes[i] = v 
            count = count+1 
        end 
    end 
end 
for i = 1,#takenColShapes do 
    if takenColShapes[i] then 
        outputChatBox(i .. " is taken by " .. getVehicleName(takenColShapes[i])) 
    else 
        outputChatBox(i .. " is free") 
    end 
end 
outputChatBox(count) 

(Nevermind, this is wrong.)

Edited by Guest
Posted

Actually, Gr0x his code is alright, with slight modifications.

taken = {} 
  
for i, v in ipairs(getElementsByType"vehicle") do 
    for i=1, 5 do 
        if isElementWithinColShape(v, cuboid[i]) then 
            taken[i] = v 
            break 
        end 
    end 
end 
  
for i=1, 5 do 
    if taken[i] then 
        outputChatBox(i.." taken by "..getVehicleName(taken[i])) 
    else 
        outputChatBox(i.." is free") 
    end 
end 

If this doesn't work, give me the full code, I'll test it.

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