Jump to content

Identify if the vehicle is occupied?


ksTakor

Recommended Posts

I want to identify if the vehicle is occupied, then if it is do nothing.

Here is my code:

function magnetVehicleCheck ( player ) 
  
    local veh = getPedOccupiedVehicle ( player ) 
    if veh then 
        if getElementData ( veh, "magnetic" ) then 
            if getElementData ( veh, "hasmagnetactivated" ) then 
                setElementData ( veh, "hasmagnetactivated", false ) 
                detachElements ( getElementData ( veh, "magneticVeh" ) ) 
            else 
                local magnet = getElementData ( veh, "magnet" ) 
                local x, y, z = getElementPosition ( magnet ) 
                local magpos = getElementData ( veh, "magpos" ) 
                local marker = createColSphere ( x , y , z, 2 ) 
                local vehs = getElementsWithinColShape ( marker, "vehicle" ) 
                destroyElement ( marker ) 
                grabveh = false 
                for key, vehitem in ipairs(vehs) do 
                    if vehitem ~= veh then 
                        local grabveh = vehitem 
                        attachElements ( grabveh, magnet, 0, 0, -1, 0, 0, getVehicleRotation(grabveh) )  
                        setElementData ( veh, "hasmagnetactivated", true ) 
                        setElementData ( veh, "magneticVeh", grabveh ) 
                        break 
                    end 
                end 
            end 
        end 
    end 
end 

Thanks in Advance

Link to post
  
addEventHandler( "onVehicleStartEnter", root, 
    function( player, seat ) 
        if seat==0 then 
        local driver = getVehicleOccupant ( source ) 
        local veh = getPedOccupiedVehicle(driver)  
        local occupants = getVehicleOccupants(veh) -- Get all vehicle occupants 
            local seats = getVehicleMaxPassengers(veh) -- Get the amount of passenger seats 
  
         
        local occupant  
        local occupantname 
            for sea = 0, seats do  
            if sea == 0 then  
                        occupant = occupants[sea] -- Get the occupant 
  
                        if occupant and getElementType(occupant)=="player" then -- If the seat is occupied by a player... 
                            occupantname = getPlayerName(occupant) -- ... get his name 
                        end 
                    end 
        end 
  
        if driver and occupant and getElementType(occupant)=="player"  and veh and getElementType( driver ) == "player"  then 
            if seat == 0 and player ~= occupant then 
                cancelEvent( ) 
            end 
        end 
        end 
    end 
) 
  
  

Link to post

@Citizen

You ever used the next function?

if next(getVehicleOccupants(veh)) then 

It doesn't matter what the index(strings tables userdata,functions is, it can tell us if a table is empty or not.

To get back on the topic, when "onVehicleStartEnter" got triggered, the getVehicleOccupants function will return also the players that are trying to enter the vehicle. I don't know why and I also don't know it is a bug, but it is doing it. :(

Link to post

sorry, I can't understand how to do it.

That's what I want to do:

function magnetVehicleCheck ( player ) 
  
    local veh = getPedOccupiedVehicle ( player ) 
    if veh then 
        if getElementData ( veh, "magnetic" ) then 
            if getElementData ( veh, "hasmagnetactivated" ) then 
                setElementData ( veh, "hasmagnetactivated", false ) 
                detachElements ( getElementData ( veh, "magneticVeh" ) ) 
            else 
                local magnet = getElementData ( veh, "magnet" ) 
                local x, y, z = getElementPosition ( magnet ) 
                local magpos = getElementData ( veh, "magpos" ) 
                local marker = createColSphere ( x , y , z, 2 ) 
                local vehs = getElementsWithinColShape ( marker, "vehicle" ) 
                destroyElement ( marker ) 
                grabveh = false 
                for key, vehitem in ipairs(vehs) do 
                    if vehitem ~= veh then 
                        local grabveh = vehitem --I wanna check if is grabveh is empty then allowed him to be grabbed 
                        attachElements ( grabveh, magnet, 0, 0, -1, 0, 0, getVehicleRotation(grabveh) ) 
                        setElementData ( veh, "hasmagnetactivated", true ) 
                        setElementData ( veh, "magneticVeh", grabveh ) 
                        break 
                    end 
                end 
            end 
        end 
    end 
end 

Sorry for being a noob

Link to post

Just use this to check if the vehicle is empty:

  
if not next(getVehicleOccupants(veh)) then 
-- empty 
else 
-- not empty 
end 
  

As simple as that, even a lua beginner can use this and put it at the right place.

Else this beginner didn't begin at all.

Link to post
@Citizen

You ever used the next function?

if next(getVehicleOccupants(veh)) then 

It doesn't matter what the index(strings tables userdata,functions is, it can tell us if a table is empty or not.

Already saw it in some loops but I never worked with it. By the way your statement isn't correct, it should be:

if next( getVehicleOccupants( veh ) ) ~= nil then 

This way it will really check if the vehicle is empty because if nil then and if 0 then will do the same effect.

Check this example I juste made:

http://ideone.com/yEhkY9

Link to post

nil isn't false and false isn't nil.

I prefer using not, then you will never have trouble with it.

True, I had already corrected myself in my last post.

Link to post
nil isn't false and false isn't nil.

I prefer using not, then you will never have trouble with it.

True, I had already corrected myself in my last post.

Why are you doing a comparison between nil and false ?? I never talked about false and next returns nil if there is no value in the table after the index.

By the way you were right since the begining because I just discovered that not 0 returns false

In Lua:

print( not nil ) -- prints true 
print( not false )    -- prints true 
print( not 0 )         -- prints false 

In C++ (11):

#include <iostream> 
using namespace std; 
  
int main() { 
    printf( !NULL ? "True" : "False" ); //prints True 
    printf( !false ? "True" : "False" ); //prints True 
    printf( !0 ? "True" : "False" );      //prints True 
    return 0; 
} 

So yeah your code is working because 0 is considered as a true value in Lua.

Link to post

"Why are you doing a comparison between nil and false ??"

You said it yourself, you don't have to mind if it is returning nil or false and that is one worry less.

I only shared my prefer. :)

Link to post
You said it yourself

No, I didn't, I just said that

if not false then 
-- or 
if not nil then 
-- or 
if not 0 then 

would have the same behaviour than

if true then 

But I was wrong about

if not 0 then 

because it's actually the same as:

if not true then 
--or 
if false then 

Link to post

ok, well it is working. That is the point.

The condition will see it as a equal result.

We know the facts so I stop posting else I am ruining somebody his topic.

@ksTakor

Your answer.

if not next(getVehicleOccupants(veh)) then 
-- empty 
else 
-- not empty 
end 

Link to post

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