Drakath Posted November 3, 2014 Share Posted November 3, 2014 I have a script that attaches weapons to players using bone_attach but sometimes the weapons hang in the air: 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
Moderators IIYAMA Posted November 7, 2014 Moderators Share Posted November 7, 2014 and when does it hang in the air? Link to comment
Saml1er Posted November 7, 2014 Share Posted November 7, 2014 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
Drakath Posted November 8, 2014 Author Share Posted November 8, 2014 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
Moderators IIYAMA Posted November 9, 2014 Moderators Share Posted November 9, 2014 what if you disable streaming? Link to comment
Drakath Posted November 9, 2014 Author Share Posted November 9, 2014 I don't think I could. There can be up to 200 of those objects and in MTA wiki it says that you shouldn't disable streaming on too many elements. Maybe I should create those objects client-side? Link to comment
Moderators IIYAMA Posted November 9, 2014 Moderators Share Posted November 9, 2014 Yea, try clientside. Link to comment
Drakath Posted November 11, 2014 Author Share Posted November 11, 2014 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
Drakath Posted February 21, 2015 Author Share Posted February 21, 2015 Alright, I rewrote the whole script to client-side but I still have the same problem I'm pretty sure it's something with the bone_attach Link to comment
#RooTs Posted February 21, 2015 Share Posted February 21, 2015 try to implement these functions, ( is just an idea for your solution ) onPlayerQuit onPlayerSpawn removeEventHandler Link to comment
Drakath Posted February 21, 2015 Author Share Posted February 21, 2015 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
#RooTs Posted February 21, 2015 Share Posted February 21, 2015 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 Link to comment
Drakath Posted February 25, 2015 Author Share Posted February 25, 2015 So is this a MTA 1.4 bug? It worked fine in the earlier versions. Link to comment
JR10 Posted February 25, 2015 Share Posted February 25, 2015 Your added conditionals suppressed the warnings, but, when the condition is false, it returns and no code is ran. So, obviously the problem is the bone attach resource. I will try to experiment with it tonight. Link to comment
#RooTs Posted February 25, 2015 Share Posted February 25, 2015 So is this a MTA 1.4 bug? It worked fine in the earlier versions. can be any function removed, or updated Link to comment
Drakath Posted February 27, 2015 Author Share Posted February 27, 2015 Your added conditionals suppressed the warnings, but, when the condition is false, it returns and no code is ran.So, obviously the problem is the bone attach resource. I will try to experiment with it tonight. Any luck so far? Link to comment
Drakath Posted February 28, 2015 Author Share Posted February 28, 2015 Seems like many other people had the same problem: https://bugs.multitheftauto.com/view.php?id=8346 http://bugs.mtasa.com/view.php?id=8482 They said that it is some sort of mathematical problem with MTA 1.4, I'll remove my back weapons script. Perhaps it will be fixed in the next MTA version. Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now