John Smith Posted November 1, 2014 Share Posted November 1, 2014 hello i got my own nametag system and it works similiar to the wiki example of nametag above ped's bone position however when calculating lots of variables 60 times per second it causes lots of cpu usage using alternative(timers) to update ped bone positions and element positions is not my choice because it would look laggy is there alternative how i could do these things(e.g calculate somethings position) to make it as smooth as onClientRender? Link to comment
John Smith Posted November 2, 2014 Author Share Posted November 2, 2014 please help me Link to comment
Mr_Moose Posted November 2, 2014 Share Posted November 2, 2014 You can't use any other technique than onClientRender and still make it smooth, however you can do a lot to optimize, for example, passing the root element to onClientRender could increase the CPU usage a lot, using bone position might not be the most optimal solution either, you could try getElementPosition and just use it's z value to determine at which height the nametag should be drawn. You could try this and see if there's any difference: local x, y = guiGetScreenSize() function PlayerNameTags() local players = getElementsByType("player") for k,v in ipairs(players) do if v ~= localPlayer and getPlayerTeam(v) then local r,g,b = getTeamColor(getPlayerTeam(v)) local x1,y1,z1 = getElementPosition(localPlayer) local x2,y2,z2 = getElementPosition(v) local visibleto = getDistanceBetweenPoints3D(x1,y1,z1,x2,y2,z2) if visibleto < 14 then local sx,sy = getScreenFromWorldPosition ( x2,y2,z2+0.3 ) if sx and sy and not getElementData( v, "anon" ) and getElementInterior(v) == getElementInterior(localPlayer) and getElementDimension(v) == getElementDimension(localPlayer) then local nWidth = string.len(getPlayerName(v)) or 20 dxDrawRectangle( sx-((nWidth*14)/2),sy-7,(nWidth*14),30, tocolor(0,0,0,100)) dxDrawText( string.gsub( getPlayerName(v), "#%x%x%x%x%x%x", "" ).."", sx,sy,sx,sy, tocolor(r,g,b,255), 0.5, "bankgothic", "center","top",false,false,false ) end end end end end addEventHandler("onClientRender",root,PlayerNameTags) It's a modified version of the nametag system in the race resource which doesn't seems to use much CPU at all, also have you tried performance browser? http://yourseverip:yourserverhttpport/performancebrowser, that's a great tool to detect "CPU leaks". Link to comment
MTA Team botder Posted November 2, 2014 MTA Team Share Posted November 2, 2014 When talking about optimization, then don't use ipairs (it's even slower than pairs) and don't use (i)pairs at all - google that please. You could also avoid grabbing every player (getElementsByType) on each frame render and do that with a table and events. 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