#Is there a better way to display player's current speed with TF2 Vscript?

1 messages · Page 1 of 1 (latest)

halcyon herald
#

i'm trying to display the player's current horizontal speed in tf2 on the beta vphysics branch currently. I have it working using the following code:

function updateSpeedoAll()
{
    local ply = null
    while (ply = Entities.FindByClassname(ply, "player")) {
        local vel = ply.GetVelocity().Length2D()
        local info = floor(vel).tostring() + " u/s"
        // print using game_text entity
        local output = Entities.FindByName(null, "text_output")
        output.__KeyValueFromString("message", info)
        EntFireByHandle(output, "Display", "", 0.0, ply, ply)
    }
}

... where text_output is a game_text entity in the map. this is called from a logic_timer with refire of 0.015s since tf2 is 66.66... ticks per second outputting OnTimer -> MyScript -> RunScriptCode -> updateSpeedoAll().

this does work, but i'm wondering a few things.

  • 1, will this be multiplayer-safe? I do think the game_text is only going to show on a specific player's screen but I'm not sure if updating the "message" keyvalue will be quick enough to always show player's only their speed.
  • 2, is there perhaps a better way to do this than looping through all player entities every tick? maybe an event that can be listened to or something? my only coding experience is bodging bash/powershell scripts so i don't really know what i'm doing with regards to vscript and proper technique :^)
short moat
#

message keyvalue is updated instantly so this is multiplayer safe

#

you dont need to loop through every entity, player entities are always at entity index 1 to maxplayers+1

#

so you can just have a normal loop from 1 to 34 (tf2's maxplayers is 33) and then use EntIndexToHScript to retrieve the ply handle (be sure to check if ply is not null)

#

local output = Entities.FindByName(null, "text_output") you should store this off too rather than trying to find it every loop

halcyon herald
#

from printl(ply + " does exist!") I'm seeing entindex 1 / 2 alternating, I'm guessing one of those is just sourcetv (which is enabled) since i'm the only player in the server?

([1] player) does exist!
([2] player) does exist!
function updateSpeedoAll()
{
    local output = Entities.FindByName(null, "text_output")
    local ply = null
    for (local i = 1 ; i < 34 ; i++) {
        ply = EntIndexToHScript(i)
        if (!ply || !ply.IsPlayer())
        {
            continue
        }
        printl(ply + " does exist!")

        local vel = ply.GetVelocity().Length2D()
        local info = floor(vel).tostring() + " u/s"

        // print using game_text entity
        output.__KeyValueFromString("message", info)
        EntFireByHandle(output, "Display", "", 0.0, ply, ply)
   }
}
short moat
#

yea sourcetv counts as a player

#

its why the maxplayers is 33 and not 32