Jump to content

CPU usage , client


Recommended Posts

I have a question

What are the normal values for cpu usage for client?

For example, for something like this(text above npc head): Screen

Now 2.03% is used there but how good is that?

Maybe I can reduce the load in some way or are these normal numbers?

function testcpu()
	local x, y, z = getCameraMatrix()
	local dimension = getElementDimension(localPlayer)
			local px, py, pz = getElementPosition(getElementByID("ped1"))
			local distance = getDistanceBetweenPoints3D(px, py, pz, x, y, z)
			if distance <= 7 then
                local sx, sy = getScreenFromWorldPosition(px, py, pz + 1.2)		
    			if sx and sy then
						dxDrawText("Unknown", sx, sy, sx, sy, tocolor(3, 169, 252, 255), 2.02, "default-bold", "center", "center", false, false, false, false, false)
      			end
			end
end
addEventHandler("onClientRender", getRootElement(), testcpu)

 

Edited by oggygod
Link to comment
  • MTA Anti-Cheat Team

Script optimization is really important in MTA, and if you cannot avoid using events like onClientRender then special care should be taken for them (e.g many conditions, cancelling unnecesary execution early, and such). Always imagine that otherwise you're executing whatever is in it, between 60 and 100 times per second.

There's a bunch of most popular ways scripters on MTA mess up performance (and cause lag & crashes) for players on their servers, and that's one of them.

About avoiding render: imagine you do acceptable optimization when you use it, but then in the end.. multiple resources with 'acceptable optimization' still add up to a high total usage.

48 minutes ago, oggygod said:

Maybe I can reduce the load in some way

Anyways, for example, you could add / remove the render event when player comes close/leaves the proximity of a 'ped' in your case. Then you don't need to get all of those values or do the calculation between 60-100 times per second, all the time. And if the peds are static, then define a static location instead of getting their position constantly.. or if they aren't static but also not actively on the move, get (and store) it once.. instead of 60-100 times per second.

Edited @oggygod

  • Like 1
Link to comment
  • Scripting Moderators
13 hours ago, oggygod said:

I have a question

What are the normal values for cpu usage for client?

For example, for something like this(text above npc head): Screen

Now 2.03% is used there but how good is that?

Maybe I can reduce the load in some way or are these normal numbers?




function testcpu()
	local x, y, z = getCameraMatrix()
	local dimension = getElementDimension(localPlayer)
			local px, py, pz = getElementPosition(getElementByID("ped1"))
			local distance = getDistanceBetweenPoints3D(px, py, pz, x, y, z)
			if distance <= 7 then
                local sx, sy = getScreenFromWorldPosition(px, py, pz + 1.2)		
    			if sx and sy then
						dxDrawText("Unknown", sx, sy, sx, sy, tocolor(3, 169, 252, 255), 2.02, "default-bold", "center", "center", false, false, false, false, false)
      			end
			end
end
addEventHandler("onClientRender", getRootElement(), testcpu)

 

If you wanna keep script in this state, i will explain what could be done better. Start off by:
Saving an function call in:

local dimension = getElementDimension(localPlayer)

Currently, you are returning it every frame, while you could save it in main scope and avoid calling it. By using onClientElementDimensionChange to store localPlayer's current dimension.

local dimension = 0

function onClientElementDimensionChange(_, newDimension)
	dimension = newDimension
end
addEventHandler("onClientElementDimensionChange", localPlayer, onClientElementDimensionChange)

Saving an another function call:

local px, py, pz = getElementPosition(getElementByID("ped1"))

I'm assuming that custom element is created on script init, otherwise you would need to update it at the time it creates.

-- Main scope

local myPed = getElementByID("ped1")

-- Render scope

local px, py, pz = getElementPosition(myPed)

For next step, you could use custom implementation of getDistanceBetweenPoints3D which is faster from MTA (thanks @Sarrum)
 

local function getDistance3D(pFirstX, pFirstY, pFirstZ, pSecondX, pSecondY, pSecondZ)
	pFirstX, pFirstY, pFirstZ = pFirstX - pSecondX, pFirstY - pSecondY, pFirstZ - pSecondZ

	return (pFirstX * pFirstX + pFirstY * pFirstY + pFirstZ * pFirstZ) ^ 0.5
end

Another thing which most of scripters forget about is using tocolor in render, even if color doesn't change...
 

tocolor(3, 169, 252, 255) -- notice () - a function call which returns same color every frame

The solution will be to save it in local variable in main scope and later use it.
 

-- Main scope

local textColor = tocolor(3, 169, 252, 255)

-- Render scope

dxDrawText("Unknown", sx, sy, sx, sy, textColor, 2.02, "default-bold", "center", "center", false, false, false, false, false)

If color isn't static you could use, for example events or timers which will be ideal way to update it.


Other than that, you might use onClientElementStreamIn/onClientElementStreamOut to add/remove handler when ped appears or disappears from stream zone, as mentioned above by Dutchman101.

Edited by srslyyyy
  • Like 2
Link to comment
  • Discord Moderators

srslyyyy provided some great tips.

One thing I'd add is that you can compare the squared value, saving a sqrt operation, like this

local maxRenderDistance = 7^2
if (x-px)^2 + (y-py)^2 + (z-pz)^2 < maxRenderDistance then

end

 

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