Jump to content

onClientPreRender


Drakath

Recommended Posts

I have a script that attaches weapons to players using bone_attach but sometimes the weapons hang in the air:

brSgB9U.jpg

Here's my bone_attach onClientPreRender part:

function putAttachedElementsOnBones() 
    for element,ped in pairs(attached_ped) do 
        if not isElement(element) then 
            clearAttachmentData(element) 
        elseif isElementStreamedIn(ped) then 
            local bone = attached_bone[element] 
            local x,y,z = getPedBonePosition(ped,bone_0[bone]) 
            local xx,xy,xz,yx,yy,yz,zx,zy,zz = getBoneMatrix(ped,bone) 
            local offx,offy,offz = attached_x[element],attached_y[element],attached_z[element] 
            local offrx,offry,offrz = attached_rx[element],attached_ry[element],attached_rz[element] 
            local objx = x+offx*xx+offy*yx+offz*zx 
            local objy = y+offx*xy+offy*yy+offz*zy 
            local objz = z+offx*xz+offy*yz+offz*zz 
            local rxx,rxy,rxz,ryx,ryy,ryz,rzx,rzy,rzz = getMatrixFromEulerAngles(offrx,offry,offrz) 
             
            local txx = rxx*xx+rxy*yx+rxz*zx 
            local txy = rxx*xy+rxy*yy+rxz*zy 
            local txz = rxx*xz+rxy*yz+rxz*zz 
            local tyx = ryx*xx+ryy*yx+ryz*zx 
            local tyy = ryx*xy+ryy*yy+ryz*zy 
            local tyz = ryx*xz+ryy*yz+ryz*zz 
            local tzx = rzx*xx+rzy*yx+rzz*zx 
            local tzy = rzx*xy+rzy*yy+rzz*zy 
            local tzz = rzx*xz+rzy*yz+rzz*zz 
            offrx,offry,offrz = getEulerAnglesFromMatrix(txx,txy,txz,tyx,tyy,tyz,tzx,tzy,tzz) 
             
        if (not tonumber(tostring(objx)) or not tonumber(tostring(objy)) or not tonumber(tostring(objz))) then return end 
         if (not tonumber(tostring(offrx)) or not tonumber(tostring(offry)) or not tonumber(tostring(offrz))) then return end 
            setElementPosition(element,objx,objy,objz) 
            setElementRotation(element,offrx,offry,offrz,"ZXY") 
        else 
            setElementPosition(element,getElementPosition(ped)) 
        end 
    end 
end 
addEventHandler("onClientPreRender",root,putAttachedElementsOnBones) 

Can someone help me fix it? There are no errors or warnings in the debugscript 3.

Link to comment

Try this:

  
function attachElementToBone(element,ped,bone,x,y,z,rx,ry,rz) 
    if not (isElement(element) and isElement(ped)) then return false end 
    if getElementType(ped) ~= "ped" and getElementType(ped) ~= "player" then return false end 
    bone = tonumber(bone) 
    if not bone or bone < 1 or bone > 20 then return false end 
    x,y,z,rx,ry,rz = tonumber(x) or 0,tonumber(y) or 0,tonumber(z) or 0,tonumber(rx) or 0,tonumber(ry) or 0,tonumber(rz) or 0 
    attached_ped[element] = ped 
    attached_bone[element] = bone 
    setElementParent (element, ped) --- set it's parent  
  

and this

function removeDataSentFlag() 
local elems =  getElementChildren(source)  
for _ ,v in pairs ( elems ) do  
detachElementFromBone( v )  
end 
    data_sent[source] = nil 
end 
addEventHandler("onPlayerQuit",root,removeDataSentFlag) 
  

Link to comment

I already destroy them when a Player leaves. The problem happens not when a player leaves, it's completely random.

Sam1er, are you saying that I should add setElementParent to attachElementToBone function? Because a function with the code you posted already exists:

function attachElementToBone(element,ped,bone,x,y,z,rx,ry,rz) 
    if not (isElement(element) and isElement(ped)) then return false end 
    if getElementType(ped) ~= "ped" and getElementType(ped) ~= "player" then return false end 
    bone = tonumber(bone) 
    if not bone or bone < 1 or bone > 20 then return false end 
    x,y,z,rx,ry,rz = tonumber(x) or 0,tonumber(y) or 0,tonumber(z) or 0,tonumber(rx) or 0,tonumber(ry) or 0,tonumber(rz) or 0 
    attached_ped[element] = ped 
    attached_bone[element] = bone 
    attached_x[element] = x 
    attached_y[element] = y 
    attached_z[element] = z 
    attached_rx[element] = rx 
    attached_ry[element] = ry 
    attached_rz[element] = rz 
    if setElementCollisionsEnabled then 
        setElementCollisionsEnabled(element,false) 
    end 
    if script_serverside then 
        triggerClientEvent("boneAttach_attach",root,element,ped,bone,x,y,z,rx,ry,rz) 
    end 
    return true 
end 

Link to comment

Ah, that's a lot of work. It worked before MTA 1.4

When MTA 1.4 was released I kept getting errors:

WARNING: bone_attach\bone_attach_c.lua:79: Bad argument @ 'setElementPosition' [Expected number, got NaN]

WARNING: bone_attach\bone_attach_c.lua:80: Bad argument @ 'setElementRotation' [Expected number, got NaN]

So I added:

if (not tonumber(tostring(objx)) or not tonumber(tostring(objy)) or not tonumber(tostring(objz))) then return end 
if (not tonumber(tostring(offrx)) or not tonumber(tostring(offry)) or not tonumber(tostring(offrz))) then return end 

This fixed the warnings but this problem appeared.

Link to comment
  • 3 months later...

I use a server-side event handler which triggers stuff in client-side so I can't remove that event handler. The other two functions you suggested are already there. Please note that these weapons do not hang in the air when a player leaves. They just get unattached for a few seconds and then come back to the player.

Link to comment
I use a server-side event handler which triggers stuff in client-side so I can't remove that event handler. The other two functions you suggested are already there. Please note that these weapons do not hang in the air when a player leaves. They just get unattached for a few seconds and then come back to the player.

understood friend :wink:

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