Sign in to follow this  
Piorun

Name tag at the top of the player.

Recommended Posts

I've got problem with my script, so i need help. I was trying to create a nice code, but this code doesn't work.

1. How to create a script - when the player join into the server his nametag shows at the top of the head for all player, but his health bar was hide (i'm thinking i must use dxDrawText function).

Please, help. I'm really noob of lua :D.

Share this post


Link to post

Basically you will have to do something like this on every frame

1)getCameraMatrix

2) for loop through players

2.1)getElementPosition

2.2)isLineOfSightClear

3.1)getPedBonePosition

3.2)getScreenFromWorldPosition

3.3)dxDrawText

Share this post


Link to post

Yhm, okey. I'll try.

local rootElement = getRootElement()
 
function test ( )
local px, py, pz = getElementPosition ( rootElement )
local x, y, z = getScreenFromWorldPosition( px, py, pz+1 )
if x then
     example = dxDrawText( "Imie Nazwisko", x, y )
end
end
 
function nick ( )
addEventHandler("onClientRender", rootElement, test)
end
 
addEventHandler ( "onClientResourceStart", rootElement, nick )

This is my code but it doesn't work.

What i'm doing wrong?

Share this post


Link to post
local px, py, pz = getElementPosition ( rootElement )

this is totally wrong.

in your test function do

local players = getElementsByType("player")
for key,val in ipairs(players) do
local px, py, pz = getElementPosition (val)
local x, y, z = getScreenFromWorldPosition( px, py, pz+1 )
if x then
     example = dxDrawText( "Imie Nazwisko", x, y )
end
end

not tested

Share this post


Link to post

addEventHandler("onClientRender",getRootElement(),
function()
local px,py,pz,tx,ty,tz,dist
	px,py,pz = getCameraMatrix()
for k,v in ipairs(getElementsByType("player")) do
		tx,ty,tz = getElementPosition(v)
		dist = math.sqrt((px-tx)^2 + (py-ty)^2 + (pz-tz)^2)
if dist < 30.0 then
if isLineOfSightClear(px,py,pz,tx,ty,tz,true,false,false,true,false,false,getLocalPlayer()) then
local sx,sy,sz = getPedBonePosition(v,5)
local x,y = getScreenFromWorldPosition(sx,sy,sz+0.3)
if x then
dxDrawText(getPlayerName(v),x,y,x,y,tocolor(150,50,0),0.85+(15-dist)*0.02,"bankgothic")
end
end
end
end
end
)

This is random example, should work.

Share this post


Link to post

The_Ex thanks. It works really nice :).

I've got one more question. How to hide this text for local player, but shows for all players?

Share this post


Link to post
addEventHandler("onClientRender",getRootElement(),
function()
local px,py,pz,tx,ty,tz,dist
     px,py,pz = getCameraMatrix()
for k,v in ipairs(getElementsByType("player")) do
if (v~=getLocalPlayer()) then
           tx,ty,tz = getElementPosition(v)
           dist = math.sqrt((px-tx)^2 + (py-ty)^2 + (pz-tz)^2)
if dist < 30.0 then
if isLineOfSightClear(px,py,pz,tx,ty,tz,true,false,false,true,false,false,getLocalPlayer()) then
local sx,sy,sz = getPedBonePosition(v,5)
local x,y = getScreenFromWorldPosition(sx,sy,sz+0.3)
if x then
dxDrawText(getPlayerName(v),x,y,x,y,tocolor(150,50,0),0.85+(15-dist)*0.02,"bankgothic")
end
end
end
end
end
end
)

Share this post


Link to post

Ok and the last question. How to center this text?

When i join into the server i saw my nickname at the right side. I include an image:

Image

Share this post


Link to post

Use http://wiki.multitheftauto.com/wiki/DxGetTextWidth to get the text's width and draw the text half the width the text to the left of the head. Replace your dxDrawText with these 4:

local scale =  0.85+(15-dist)*0.02;
local playerName = getPlayerName( v );
local w = dxGetTextWidth( playerName, scale, "bankgothic" );
dxDrawText(playerName,x-w/2,y,x,y,tocolor(150,50,0),scale,"bankgothic")

Share this post


Link to post
Or just set two more parameters, alignX and alignY, to "center" in dxDrawText.

This will not give him the results that he wants.. He'll still have to use dxGetTextWidth.

Share this post


Link to post
Or just set two more parameters, alignX and alignY, to "center" in dxDrawText.

This will not give him the results that he wants.. He'll still have to use dxGetTextWidth.

If you turn clipping off, then set identical left & right, and top & bottom parameters, you can use alignment easily.

Share this post


Link to post

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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.