Jump to content

Improving function


ds1-e

Recommended Posts

  • Scripting Moderators

Hey. Need some help from you guys. I have function which basically, should hide attached to bone model whenever player aims. But it's not perfect.

Here are my questions:

- It's possible to make it more efficient/optimized?

- There's any way to improve it? One and only problem is that this function sometimes don't set element alpha to 0, so the weapon it's visible even if player aiming. It's rarely, but i would like to fix it.

 

function toggleWeaponsVisibility(key, keyState)
	if getPedWeaponSlot(getLocalPlayer()) == 6 then
		local alpha = 255
		if keyState == "down" then
			alpha = 0
		elseif keyState == "up" then
			alpha = 255
		end
		local objects = getElementsByType("object", getRootElement(), true)
		for i = 1, #objects do
			if isElementAttachedToBone(objects[i]) and getElementModel(objects[i]) == 2763 or getElementModel(objects[i]) == 1820 or getElementModel(objects[i]) == 2708 or getElementModel(objects[i]) == 15036 then
				setElementAlpha(objects[i], alpha)
			end
		end
	end
end
bindKey("aim_weapon", "both", toggleWeaponsVisibility)

 

Link to comment
  • Moderators

While creating elements:

-- on top of the script
local boneAttachContainer = createElement("boneAttachContainer", "boneAttachContainer")



-- while making objects
local object = createObject(...)
setElementParent(object, boneAttachContainer)

 

 

-- on top of the script
local validBoneAttachModels = {[2763] = true, [1820] = true, [2708] = true, [15036] = true}

-- while pressing key
local boneAttachContainer = getElementByID("boneAttachContainer")
if boneAttachContainer then
	local objects = getElementsByType("object", boneAttachContainer, true)
  	
	for i = 1, #objects do
		local object = objects[i]
		local model = getElementModel(object)	
		if validBoneAttachModels[model] then
			setElementAlpha(object, alpha)
		end
	end
end

 

 

1 hour ago, majqq said:

- There's any way to improve it? One and only problem is that this function sometimes don't set element alpha to 0, so the weapon it's visible even if player aiming. It's rarely, but i would like to fix it. 

You might want to consider using a dirty timer or find an event that will occur at that time.

 

 

Edited by IIYAMA
  • Thanks 1
Link to comment
  • 1 month later...
  • Scripting Moderators
13 minutes ago, Alraaayts said:

Can you send me the whole script please??

It's in first post. But:

function toggleWeaponsVisibility(key, keyState)
	if getPedWeaponSlot(getLocalPlayer()) == 6 then
		local alpha = 255
		if keyState == "down" then
			alpha = 0
		elseif keyState == "up" then
			alpha = 255
		end
		local objects = getElementsByType("object", getRootElement(), true)
		for i = 1, #objects do
			if isElementAttachedToBone(objects[i]) and getElementModel(objects[i]) == 2763 or getElementModel(objects[i]) == 1820 or getElementModel(objects[i]) == 2708 or getElementModel(objects[i]) == 15036 then -- change id of models here
				setElementAlpha(objects[i], alpha)
			end
		end
	end
end
bindKey("aim_weapon", "both", toggleWeaponsVisibility)

You need to change ids of objects.

Link to comment
3 minutes ago, majqq said:
  1. function toggleWeaponsVisibility(key, keyState)
  2. if getPedWeaponSlot(getLocalPlayer()) == 6 then
  3. local alpha = 255
  4. if keyState == "down" then
  5. alpha = 0
  6. elseif keyState == "up" then
  7. alpha = 255
  8. end
  9. local objects = getElementsByType("object", getRootElement(), true)
  10. for i = 1, #objects do
  11. if isElementAttachedToBone(objects[i]) and getElementModel(objects[i]) == 2763 or getElementModel(objects[i]) == 1820 or getElementModel(objects[i]) == 2708 or getElementModel(objects[i]) == 15036 then -- change id of models here
  12. setElementAlpha(objects[i], alpha)
  13. end
  14. end
  15. end
  16. end
  17. bindKey("aim_weapon", "both", toggleWeaponsVisibility)

Where does i add this? Client side?

Then how about IIYAMA post? Where to add it?

Link to comment
  • Scripting Moderators
Just now, Alraaayts said:

Where does i add this? Client side?

It's client. But don't forget to change ids of objects in condition. And this function doesn't work all time, so you would need to improve it a bit, and make things by yourself.

Link to comment
  • Scripting Moderators
1 minute ago, Alraaayts said:

Is yours fixed?

No, i've fixed it by myself, but i wouldn't share it here. You still have function which will do the job. But it's possible to make it better, and work nearly all the time. Or you can use IIYAMA example. I'll probably do the same, when i'll rewrite my gamemode.

Link to comment
  • Scripting Moderators
1 hour ago, Alraaayts said:

Can you dm  and help me how you do it?

A friendly help?

IIYAMA post is in this topic. He explained well when to use it. And about how i did that. Use int loop instead - which is faster, + use this function when drawing a crosshair, should fix most of problems.

Edited by majqq
Link to comment
  • Scripting Moderators
2 hours ago, Alraaayts said:

then how to create the loop?

My mistake, this loop is ok. Are you sure that id of replaced objects in condition are correct?

Edited by majqq
Link to comment
  • Scripting Moderators
31 minutes ago, Alraaayts said:

Yes, all id objects are replaced by weapons properly....

Maybe you could check it again, and to be sure it should be client-side.

You can paste code again? I tested it by myself, and it work somehow, not perfectly but it did the basic job.

As far i remember you should also replace following objects with custom transparent model.

356
347
358
355
335
322
353
339
357
349

 

Scan: https://www.virustotal.com/#/file-analysis/MTAyNWY5MzQ5ZjA5YjcyMTZkNmNkNGFhNjI0ZGI5ZjU6MTU1NjExNzU3OA==

Download: https://www.mediafire.com/file/rk9bm7r2oo8gq6w/transparent.rar/file

Edited by majqq
Link to comment
  • 4 weeks later...
  • Scripting Moderators
32 minutes ago, Alraaayts said:

Bro i still didn't fix my problem

 

Then i don't know how can i help, i gave you all ways i used to fix it.

Link to comment
  • Scripting Moderators
12 minutes ago, Alraaayts said:

Bro do you still have your dayz GM?

And do you still manage dayz server??

I don't have dayz server, and i didn't had.

Edited by majqq
Link to comment
  • Scripting Moderators
17 minutes ago, Alraaayts said:

What gm do you manage?? Why u post like this codes?? This is codes for dayz

Not only for DayZ, it's custom gamemode. :P

Link to comment
On 05/03/2019 at 02:52, IIYAMA said:

While creating elements:


-- on top of the script
local boneAttachContainer = createElement("boneAttachContainer", "boneAttachContainer")



-- while making objects
local object = createObject(...)
setElementParent(object, boneAttachContainer)

 

 


-- on top of the script
local validBoneAttachModels = {[2763] = true, [1820] = true, [2708] = true, [15036] = true}

-- while pressing key
local boneAttachContainer = getElementByID("boneAttachContainer")
if boneAttachContainer then
	local objects = getElementsByType("object", boneAttachContainer, true)
  	
	for i = 1, #objects do
		local object = objects[i]
		local model = getElementModel(object)	
		if validBoneAttachModels[model] then
			setElementAlpha(object, alpha)
		end
	end
end

 

 

You might want to consider using a dirty timer or find an event that will occur at that time.

 

 

Like this code i am looking for... Can you help me how to place ur code on majqq code?

Link to comment
  • Moderators
2 hours ago, Alraaayts said:

Like this code i am looking for... Can you help me how to place ur code on majqq code?

Only if you can explain what the code does. It is bad practice to use code you do not understand.

 

For you, your server and your players.

Example:

What if my code contains something that would expose your password?

Steals your resources?

Or makes the performance even worse?

 

Edited by IIYAMA
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...