Jump to content

weapon attach question


thund3rbird23

Recommended Posts

If I'm attach the weapon with bone_attach then I can't attach the weapon to the player's back?

Because I attach the weapon in player's hand with this:

 local playerInterior = getElementInterior(player)
 local playerDimension = getElementDimension(player)
 local x, y, z = getElementPosition(player) 
 ak[player] = createObject(2965,0,0,0)
 setElementInterior(ak[player], playerInterior)
 setElementDimension(ak[player], playerDimension)
 exports['bone_attach']:attachElementToBone(ak[player], player, 12, 0, 0, 0, 0, -90, 0)

And using THIS resource to attach the weapon to player's back then it's not moved to anywhere.

Link to comment

One more thing, I changed the models to:

function obtenerObjeto(arma)
local defaultak = getElementData(localPlayer, "defaultak")
local goldak = getElementData(localPlayer, "goldak")
	local m
	if arma == 30 and defaultak == 1 then
		m = 1254
	elseif arma == 30 and goldak == 1 then
		m = 2965
	end
	return m
end

and got a lot warning messages in debugscript which cause lagging.

carmas.Lua:14: Bad argument @ 'createObject' [Expected number at argument 1, got nil]

carmas.Lua:15 Bad argument @ 'setElementCollisionsEnabled' [Expected element at argument 1, got boolean]

carmas.Lua: 19 Bad argument @ 'destroyElement' [Expected element at argument 1, got boolean]

These 3 functions are:

jugadores[jug][slot] = createObject(model,0,0,0) -- line 14 error
setElementCollisionsEnabled(jugadores[jug][slot],false) -- line 15 error
destroyElement(jugadores[jug][slot]) -- line 19 error

If I'm using just arma == 30 like this:

if arma == 30 then
m = 1254

then I get no errors but then I can't seperate/specify objects just only one... and I create every skins to an existing object which I set invisible before... so I must specify the defaultak and the goldak.

Can I get this working somehow?

Link to comment

why didnt u use bone_attach? its better.

for example: 

 

weapons = {
{"NEWAK",30,2222},--wep,id,object
{"NEWM4",31,3333},
}


function weaponback(previousWeaponID, currentWeaponID)
 for k,arma in ipairs(weapons) do
 local wep = getElementData(source,arma[1])
 local x,y,z = getElementPosition(source)
 if previousWeaponID == arma[2] then --wep not hand
 armaobject = createObject(arma[3],x,y,z) -- object created 
 attachElementToBone(armaobject,source,3,0.19,-0.31,-0.1,0,270,-90)-- here back coords obj attached 
end
if currentWeaponID == arma[2] then --wep hand
destroyElement(armaobject) -- destroyed object
end
end
end

addEventHandler ( 'onPlayerWeaponSwitch', getRootElement ( ), weaponback)

idk if it will work is just a example to u understand how to do

Edited by Furzy
Link to comment
  • Scripting Moderators
7 hours ago, Furzy said:

why didnt u use bone_attach? its better.

for example: 

 


weapons = {
{"NEWAK",30,2222},--wep,id,object
{"NEWM4",31,3333},
}


function weaponback(previousWeaponID, currentWeaponID)
 for k,arma in ipairs(weapons) do
 local wep = getElementData(source,arma[1])
 local x,y,z = getElementPosition(source)
 if previousWeaponID == arma[2] then --wep not hand
 armaobject = createObject(arma[3],x,y,z) -- object created 
 attachElementToBone(armaobject,source,3,0.19,-0.31,-0.1,0,270,-90)-- here back coords obj attached 
end
if currentWeaponID == arma[2] then --wep hand
destroyElement(armaobject) -- destroyed object
end
end
end

addEventHandler ( 'onPlayerWeaponSwitch', getRootElement ( ), weaponback)

idk if it will work is just a example to u understand how to do

Using ipairs is bad idea.

local playerWeapons = {
	["AK-47"] = 2222, -- example id, not sure if correct
	["M4A1"] = 3333, -- example id, not sure if correct
}

local weapon = getElementData(source, "currentweapon_1") -- basing on dayz, this is element data which keeps weapon which player hold in hand
local correct_weapon = playerWeapons[weapon]
if correct_weapon then
	-- do your stuff.
end

 

4 hours ago, thund3rbird23 said:

Thanks, but the destroyElement don't work.

Bad argument @ 'destroyElement' [Expected element at argument 1, got nil]

Simply add:

isElement(armaobject)

You should start reading carefully warning/error codes.

nil = empty/doesn't exist, this is also used for clearing variables, tables, etc.

Expected element got non-existing thing.

Edited by majqq
Link to comment
1 hour ago, majqq said:

Using ipairs is bad idea.


local playerWeapons = {
	["AK-47"] = 2222, -- example id, not sure if correct
	["M4A1"] = 3333, -- example id, not sure if correct
}

local weapon = getElementData(source, "currentweapon_1") -- basing on dayz, this is element data which keeps weapon which player hold in hand
local correct_weapon = playerWeapons[weapon]
if correct_weapon then
	-- do your stuff.
end

 

Simply add:


isElement(armaobject)

You should start reading carefully warning/error codes.

nil = empty/doesn't exist, this is also used for clearing variables, tables, etc.

Expected element got non-existing thing.

Thanks for your suggestions. I added if isElement(armaObject) then ... and get no errors, but the code does nothing or I doing something wrong. Because the weapon still attached in my hand.

I'm using an inventory system and triggering a server side event called "removeDefaultAK" if the player tuck in or putting away their weapon (I don't know which is the right word in English, sorry.)

So now the code looks like this:

weapons = {
{"DEFAULTAK",30,1254},--wep,id,object
{"GOLDAK",30,2965},
}
function removeDefaultAK(source)
for k,arma in ipairs(weapons) do
 local wep = getElementData(source,arma[1])
 local x,y,z = getElementPosition(source)
 if previousWeaponID == arma[2] then --wep not hand
 armaobject = createObject(arma[3],x,y,z) -- object created 
 attachElementToBone(armaobject,source,3,0.19,-0.31,-0.1,0,270,-90)-- here back coords obj attached 
end
if currentWeaponID == arma[2] then --wep hand
if isElement(armaobject) then
destroyElement(armaobject) -- destroyed object
end
end
end
end
addEvent("removealapAK", true)
addEventHandler("removealapAK", getRootElement(), removeDefaultAK)


By the way, if I want to use your suggestion instead of the ipairs then I need to set player's currentweapon_1 elementdata to true or to the weapon name in the table ex.: AK-47 when he take out their weapon if I'm right?!

Edited by thund3rbird23
Link to comment
1 minute ago, IIYAMA said:

@thund3rbird23

I am just bewondering, did you test this with other players? Or is that irrelevant?

Because this code as it is now doesn't looks like multiplayer compatible. Atleast if the armas effect is visible for remotePlayers(other players than yourself).

 

No, I'm just did test by myself. But this code doesn't even work locally I mean I can't see the weapons on my back. 

Link to comment
  • Moderators
18 minutes ago, thund3rbird23 said:

No, I'm just did test by myself. But this code doesn't even work locally I mean I can't see the weapons on my back. 

Hmmm The code is a bit odd.

 

But to fix it.

Do not destroy the weapon in the loop. Do it before the loop.

Just this:

if isElement(armaobject) then
destroyElement(armaobject) -- destroyed object
end

Because the weapon is directly destroyed after creating it. Or atleast I think that could happen.

 

 

 

 

 

 

 

 

Edited by IIYAMA
Link to comment
12 minutes ago, IIYAMA said:

Hmmm The code is a bit odd.

 

But to fix it.

Do not destroy the weapon in the loop. Do it before the loop.

Just this:


if isElement(armaobject) then
destroyElement(armaobject) -- destroyed object
end

Because the weapon is directly destroyed after creating it.

 

 

 

 

 

 

 

Did you mean this? I changed and still don't attach the weapon on my back it's in my hand:

function removeDefaultAK(source)
for k,arma in ipairs(weapons) do
 local wep = getElementData(source,arma[1])
 local x,y,z = getElementPosition(source)
 if previousWeaponID == arma[2] then --wep not hand
 armaobject = createObject(arma[3],x,y,z) -- object created 
 attachElementToBone(armaobject,source,3,0.19,-0.31,-0.1,0,270,-90)-- here back coords obj attached 
end
if currentWeaponID == arma[2] then --wep hand
end
end
if isElement(armaobject) then
destroyElement(armaobject) -- destroyed object
end
end

 

Link to comment
7 minutes ago, IIYAMA said:

Yes, but BEFORE, not AFTER.

Also add some debuglines, I need those to help you.

But the armaobject is in the loop?!

--- debugs ---

k returns false

arma returns 
1 {"DEFAULTAK", 30, 1254}
2 {"GOLDAK", 30, 2965}

wep returns false(2x)

previousWeaponID returns nil

armaobject returns false

currentWeaponID returns nil
 

--- debugs ---

I'm not sure you wanted these debuglines or in this way but I hope you can understand something

 

It isn't be easier if I attach the weapon to the back like I attach to the hand?

Link to comment
  • Moderators
3 hours ago, thund3rbird23 said:

I'm not sure you wanted these debuglines or in this way but I hope you can understand something

It is most of the time not a matter of what they show, but which ones aren't shown.

Between the ones that are shown and the ones that are NOT shows is your problem.

 

Even if you think such useful information is useless, there will be a time you will think different about your previous reply.

 

 

 

 

Link to comment
19 minutes ago, IIYAMA said:

It is most of the time not a matter of what they show, but which ones aren't shown.

Between the ones that are shown and the ones that are NOT shows is your problem.

 

Even if you think such useful information is useless, there will be a time you will think different about your previous reply.

 

 

 

 

I see! I will do more debugs in the future. I already seen your debugging tutorial.

And what do you think about my last question? Can fix it somehow?

Link to comment
  • Moderators
14 minutes ago, thund3rbird23 said:

I see! I will do more debugs in the future. I already seen your debugging tutorial.

And what do you think about my last question? Can fix it somehow?

I have heard about this issue before, but not sure if people actually fixed it.

The object orientation is updated every frame. But for some unknown reason there is a little delay.

 

With other words: I don't know.

 

Maybe add an extra eventHandler in file bone_attach_c.Lua:

addEventHandler("onClientPreRender",root,putAttachedElementsOnBones) --[[ line 85 ]] 
addEventHandler("onClientRender",root,putAttachedElementsOnBones) --[[ NEW on line 86 ]] 

 

If you look at the event order:

Mta_game_proccess.png

https://wiki.multitheftauto.com/wiki/Game_Processing_Order

 

You can see that onClientPreRender event first fires and onClientRender fires as last.

Maybe if updates are applied on both moments it could be a little bit smoother.

Could could could > so not sure.

 

 

 

 

 

 

 

Edited by IIYAMA
  • Like 2
Link to comment
10 minutes ago, IIYAMA said:

I have heard about this issue before, but not sure if people actually fixed it.

The object orientation is updated every frame. But for some unknown reason there is a little delay.

 

With other words: I don't know.

 

Maybe add an extra eventHandler in file bone_attach_c.Lua:


addEventHandler("onClientPreRender",root,putAttachedElementsOnBones) --[[ line 85 ]] addEventHandler("onClientRender",root,putAttachedElementsOnBones) --[[ NEW on line 86 ]] 

 

If you look at the event order:

Mta_game_proccess.png

https://wiki.multitheftauto.com/wiki/Game_Processing_Order

 

You can see that onClientPreRender event first fires and onClientRender fires as last.

Maybe if updates are applied on both moments it could be a little bit smoother.

Could could could > so not sure.

 

 

 

 

 

 

 

I will try, thanks!

 

8 minutes ago, Furzy said:

I tried a lot of ways to fix that, and i didnt get success


What do you mean? The rotation thing?

 

 

Link to comment
Just now, thund3rbird23 said:

I will try, thanks!

 


What do you mean? The rotation thing?

 

 

Yes, but its not a problem for me cuz only happen if player is attach to vehicle in movement or if u aim fast with mouse

Edited by Furzy
  • Like 1
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...