Jump to content

attachElements


ViRuZGamiing

Recommended Posts

Hi I'm trying to create a colshape to all existing vehicles but it seems to not be there.

no errors, showcol and development mode are active. first time using attachElements.

vehicles = getElementsByType ("vehicle") 
for key,vehicle in ipairs(vehicles) do 
    local x, y, z = getElementPosition(vehicle) 
    vehCol = createColRectangle(x, y, 5, 5) 
    attachElements(vehCol, vehicle) 
end 

regards billy

Link to comment

Note from createColRectangle:

Attaching a rectangle colshape to another element may give unexpected results as the origin is not at the rectangle centre. Try using a collision circle for attaching instead.

Anyway, also try the following code:

vehicles = getElementsByType ("vehicle") 
for key,vehicle in ipairs(vehicles) do 
    local x, y, z = getElementPosition(vehicle) 
    w,h = 5,5 
    x,y = x+w/2,y+h/2 
    vehCol = createColRectangle(x, y, w, h) 
    attachElements(vehCol, vehicle) 
end 

Link to comment
vehMarker = createMarker(x, y, z, "cylinder", 2.0, 255, 0, 0, 255) 
attachElements(vehMarker, vehicle, 0, -3, -1) 

I use this to attach a marker to every vehicle (vehicle is the result from looping through all vehicle elements)

I want to call the vehicle my marker is attached to so I tried using getElementAttachedTo but It didn't work.

Is there any other way of calling the vehicle.

Link to comment
setTimer(function () 
    vehicles = getElementsByType ("vehicle") 
    for key,vehicle in ipairs(vehicles) do 
        if not (getElementData(vehicle, "marker")) then 
            local x, y, z = getElementPosition(vehicle) 
            vehMarker = createMarker(x, y, z, "cylinder", 2.0, 255, 0, 0, 255) 
            attachElements(vehMarker, vehicle, 0, -3, -1) 
            setElementData(vehicle, "marker", vehMarker) 
             
            addEventHandler("onMarkerHit", vehMarker, function(hitElement) 
                if (getElementType(hitElement) == "player") then 
                    toggleControl(hitElement, "enter_exit", false) 
                    bindKey(hitElement, "f", "down", funcName) 
                end 
            end) 
  
            addEventHandler("onMarkerLeave", vehMarker, function(hitElement) 
                if (getElementType(hitElement) == "player") then 
                    toggleControl(hitElement, "enter_exit", true) 
                    unbindKey(hitElement, "f", "down", funcName) 
                end 
            end) 
        end 
    end 
end, 6000, 0) 

Here's the code, Vehicle which the marker is attached to should be accessible from function 'funcName'

Link to comment

Working I tested it by myself,

local vehMarker = {} 
  
setTimer(function () 
    for index, vehicle in pairs (getElementsByType("vehicle")) do 
        if not (getElementData(vehicle, "marker")) then 
            local x, y, z = getElementPosition(vehicle) 
            vehMarker[vehicle] = createMarker(x, y, z, "cylinder", 2.0, 255, 0, 0, 255) 
            attachElements(vehMarker[vehicle], vehicle, 0, -3, -1) 
            setElementData(vehicle, "marker", vehMarker[vehicle]) 
            
            addEventHandler("onMarkerHit", vehMarker[vehicle], function(hitElement) 
                if (getElementType(hitElement) == "player") then 
                    toggleControl(hitElement, "enter_exit", false) 
                    bindKey(hitElement, "f", "down", funcName) 
                end 
            end) 
  
            addEventHandler("onMarkerLeave", vehMarker[vehicle], function(hitElement) 
                if (getElementType(hitElement) == "player") then 
                    toggleControl(hitElement, "enter_exit", true) 
                    unbindKey(hitElement, "f", "down", funcName) 
                end 
            end) 
        end 
    end 
end, 6000, 0) 

Link to comment

What I eventually try to do is passing the 'vehicle' onto the 'bindFunc' function.

setTimer(function () 
    for index, vehicle in pairs (getElementsByType("vehicle")) do 
        if not (getElementData(vehicle, "marker")) then 
            local x, y, z = getElementPosition(vehicle) 
            vehMarker = createMarker(x, y, z, "cylinder", 2.0, 255, 0, 0, 255) 
            attachElements(vehMarker, vehicle, 0, -3, -1) 
            setElementData(vehicle, "marker", vehMarker) 
            
            addEventHandler("onMarkerHit", vehMarker, bindFunc) 
            addEventHandler("onMarkerLeave", vehMarker, unbindFunc) 
        end 
    end 
end, 6000, 0) 

Link to comment

Use this function to check if the element "marker" is attached or else

if (isElementAttached(vehMarker)) then     
    toggleControl(hitElement, "enter_exit", false) 
    bindKey(hitElement, "f", "down", funcName) 
end 

and get the funcName from bind like that, if that what you means

function funcName() 
 --[[your code.]] 
end 

full code

setTimer(function () 
    vehicles = getElementsByType ("vehicle") 
    for key,vehicle in ipairs(vehicles) do 
        if not (getElementData(vehicle, "marker")) then 
            local x, y, z = getElementPosition(vehicle) 
            vehMarker = createMarker(x, y, z, "cylinder", 2.0, 255, 0, 0, 255) 
            attachElements(vehMarker, vehicle, 0, -3, -1) 
            setElementData(vehicle, "marker", vehMarker) 
            
            addEventHandler("onMarkerHit", vehMarker, function(hitElement) 
                if (getElementType(hitElement) == "player") then 
                    if (isElementAttached(vehMarker)) then     
                        toggleControl(hitElement, "enter_exit", false) 
                        bindKey(hitElement, "f", "down", funcName) 
                    end 
                end 
            end) 
  
            addEventHandler("onMarkerLeave", vehMarker, function(hitElement) 
                if (getElementType(hitElement) == "player") then 
                    if (isElementAttached(vehMarker)) then 
                        toggleControl(hitElement, "enter_exit", true) 
                        unbindKey(hitElement, "f", "down", funcName) 
                    end     
                end 
            end) 
        end 
    end 
end, 6000, 0) 
  
function funcName() 
 --[[your code.]] 
end 

Link to comment

You don't seem to get it, I am sorry to be rude but it seems you don't understand what I try to do.

I want use (look at the bold text);

for key,vehicle in ipairs(vehicles) do

to be used inside 'bindFunc' from my last example.

addEventHandler("onMarkerHit", vehMarker, bindFunc)

So I can do this

function bindFunc ()

use vehicle here

end

Link to comment
function bindFunc(hitElement) 
    if (getElementType(hitElement) == "player") then 
        toggleControl(hitElement, "enter_exit", false) 
        bindKey(hitElement, "f", "down", funcName) 
    end 
end 
  
setTimer(function () 
    for index, vehicle in pairs (getElementsByType("vehicle")) do 
        if not (getElementData(vehicle, "marker")) then 
            local x, y, z = getElementPosition(vehicle) 
            vehMarker = createMarker(x, y, z, "cylinder", 2.0, 255, 0, 0, 255) 
            attachElements(vehMarker, vehicle, 0, -3, -1) 
            setElementData(vehicle, "marker", vehMarker) 
            
            addEventHandler("onMarkerHit", vehMarker, bindFunc) 
            -- I should need a way to pass the argument with this handler 
        end 
    end 
end, 6000, 0) 

Can I do this?

addEventHandler("onMarkerHit", vehMarker, bindFunc(vehicle)) 

Link to comment
function bindFunc(hitElement) 
    if (getElementType(hitElement) == "player") then 
        toggleControl(hitElement, "enter_exit", false) 
        bindKey(hitElement, "f", "down", funcName) 
    end 
end 
  
setTimer(function () 
    for index, vehicle in pairs (getElementsByType("vehicle")) do 
        if not (getElementData(vehicle, "marker")) then 
            local x, y, z = getElementPosition(vehicle) 
            vehMarker = createMarker(x, y, z, "cylinder", 2.0, 255, 0, 0, 255) 
            attachElements(vehMarker, vehicle, 0, -3, -1) 
            setElementData(vehicle, "marker", vehMarker) 
            
            addEventHandler("onMarkerHit", vehMarker, bindFunc) 
            -- I should need a way to pass the argument with this handler 
        end 
    end 
end, 6000, 0) 

Can I do this?

addEventHandler("onMarkerHit", vehMarker, bindFunc(vehicle)) 

Sadly enough, you can't just randomly parse your parameters in your eventHandler, this has to be done with the function call itself. However, since you do want that vehicle to be in that function.. you can try to actually gather the data which you've already set in the marker's hit and see if there is an actual vehicle linked to that.

function bindFunc ( hitElement ) 
    if ( getElementType ( hitElement ) == "player" ) then 
        local linkedVehicle = getElementData ( source, "marker" ); 
        if ( isElement ( linkedVehicle ) ) then 
            toggleControl ( hitElement, "enter_exit", false ); 
            bindKey ( hitElement, "f", "down", funcName ); 
        end 
    end 
end 
  
setTimer ( 
    function () 
        for index, vehicle in pairs ( getElementsByType ( "vehicle" ) ) do 
            if not ( getElementData ( vehicle, "marker" ) ) then 
                local x, y, z = getElementPosition ( vehicle ); 
                local vehMarker = createMarker ( x, y, z, "cylinder", 2.0, 255, 0, 0, 255 ); 
                attachElements ( vehMarker, vehicle, 0, -3, -1 ); 
                setElementData ( vehicle, "marker", vehMarker ); 
                
                addEventHandler ( "onMarkerHit", vehMarker, bindFunc ); 
            end 
        end 
    end, 6000, 0 
); 

Another thing that I would strongly suggest is keeping some kind of ID running on your vehicles so it's easier to obtain a vehicle as an element. By doing that you only have to save the vehicleId on the marker his butt and obtain the linked vehicle to it. If a vehicle currently explodes, the marker is still there.

I do wonder, what are you trying to do? Maybe there is another way that could solve your problem.

Link to comment

We'll I'm try to open the trunk of a vehicle by pressing F so when a player is standing behind the vehicle the enter gets toggled and a new function get's binded. I am planning on making so that a vehicle has a owner and an ID so I could use those but I was trying to make this work as a standalone first.

Link to comment

First assign an ID to them, after that you can try to use bindKey to bind your key, createColSphere to create a small sphere once a player hits the button. After that use getElementsWithinColShape to get the vehicles around him/her. If there are vehicles in his area, check the elementData to see if the vehicle is owned by him or locked ( not sure how you want to protect it ) and get the closest vehicle too. Once you've done that, get the elementBoundingBox and see if the user is at the end of the vehicle ( get the distance from the biggest value from the vehicle and the position from the player ). Then open it.

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