Jump to content

Detecting the 'responsible resource' for an element?


LonelyRoad

Recommended Posts

local vehicles = getElementsByType ( "vehicle", getResourceRootElement ( getResourceFromName ( "vehicle-system" ) ) ) 
for _, vehicle in ipairs ( vehicles ) do 
    fixVehicle ( vehicle ) 
end 

That will fix all vehicles created by the "vehicle-system" resource.

Link to comment

That's kind of what I'm looking for, and has given me a good idea of another way in which that could be useful Snake. However the context I want to apply it to is as below:

  
function EngineToggle(src, key, state) 
    local occuVeh = getPedOccupiedVehicle(src) 
     
    if (occuVeh ~= false) then 
        local vehOwner = tonumber(getElementData(occuVeh, "veh:owner")) 
        local charID = tonumber(getElementData(src, "character:id")) 
         
        if (vehOwner == charID) then 
            local vehState = getVehicleEngineState(occuVeh) 
            setVehicleEngineState(occuVeh, not vehState) 
            setVehicleLocked(occuVeh, not vehState) 
        else 
            if (getPedOccupiedVehicleSeat(src) == 0) then 
                outputChatBox("This ain't your car!", src) 
                if (isPedInVehicle(src)) then 
                    local x, y, z = getElementPosition(src) 
                     
                    removePedFromVehicle(src) 
                    setElementPosition(src, x, y, z+5) 
                    local x, y, z = nil, nil, nil 
                end 
            end 
        end 
    end 
end 
  

Notice line 9 I check if the owner info matches, and then on lines 14-23 I eject the guy from the car if he doesn't own it. I want to only do that on vehicles created by the vehicle-system resource.

Link to comment
function isValidVehicle ( vehicle ) 
    if isElement ( vehicle ) then 
        local parent = getElementParent ( vehicle ) 
        if ( parent ) then 
            local parent = getElementParent ( parent ) 
            if ( parent ) then 
                return ( parent == getResourceRootElement ( getResourceFromName ( "vehicle-system" ) ) ) 
            else 
                return false 
            end 
        else 
            return false 
        end 
    else 
        return false 
    end 
end 
  
  
function EngineToggle ( src, key, state ) 
    local occuVeh = getPedOccupiedVehicle ( src ) 
    if ( occuVeh ~= false ) then 
        if ( not isValidVehicle ( occuVeh ) ) then 
            return 
        end 
  
        local vehOwner = tonumber ( getElementData ( occuVeh, "veh:owner" ) ) 
        local charID = tonumber ( getElementData ( src, "character:id" ) ) 
        if ( vehOwner == charID ) then 
            local vehState = getVehicleEngineState ( occuVeh ) 
            setVehicleEngineState ( occuVeh, not vehState ) 
            setVehicleLocked ( occuVeh, not vehState ) 
        else 
            if ( getPedOccupiedVehicleSeat ( src ) == 0 ) then 
                outputChatBox ( "This ain't your car!", src ) 
                if isPedInVehicle ( src ) then 
                    local x, y, z = getElementPosition ( src ) 
                    removePedFromVehicle ( src ) 
                    setElementPosition ( src, x, y, z + 5 ) 
                end 
            end 
        end 
    end 
end 

Try it.

Link to comment

That worked perfectly, I didn't think that getElementParent would behave like that, based on the example. If it intrigues you, I changed the behaviour of the EngineToggle function slightly:

  
function EngineToggle(src, key, state) 
  
    local occuVeh = getPedOccupiedVehicle(src) 
    local vehState = getVehicleEngineState(occuVeh) 
     
    if ( not isValidVehicle ( occuVeh ) ) then 
        setVehicleEngineState(occuVeh, not vehState) 
        return 
    end 
     
    if (occuVeh ~= false) then 
        local vehOwner = tonumber(getElementData(occuVeh, "veh:owner")) 
        local charID = tonumber(getElementData(src, "character:id")) 
         
        if (vehOwner == charID) then 
            setVehicleEngineState(occuVeh, not vehState) 
            setVehicleLocked(occuVeh, not vehState) 
        else 
            if (getPedOccupiedVehicleSeat(src) == 0) then 
                outputChatBox("This ain't your car!", src) 
                if (isPedInVehicle(src)) then 
                    local x, y, z = getElementPosition(src) 
                     
                    removePedFromVehicle(src) 
                    setElementPosition(src, x, y, z+5) 
                    local x, y, z = nil, nil, nil 
                end 
            end 
        end 
    end 
end 
  

Either way, thank you!

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