User talk:Braindawg: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
(Created page with "{{tf2}} This page contains examples of VScript performance tips and tricks for {{tf2|3}}. All of these results come from [https://cdn.discordapp.com/attachments/1...")
 
(Fix lonk)
 
(22 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{tf2}} This page contains examples of [[Vscript|VScript]] performance tips and tricks for {{tf2|3}}.


All of these results come from [https://cdn.discordapp.com/attachments/1039243316920844428/1147882067749765210/vscript_benchmark_fix.zip?ex=661bd67b&is=6609617b&hm=d118eefea969169e55f54c57b61c46a7e0dfc953e46158ef239327939461b900& This benchmarking tool] '''You must launch the game in -insecure before using this!'''
<small>(using [[Template:Message]])</small>&nbsp; Hello Braindawg I have moved all content from here, to [[User:Braindawg/performance|this page]] as this page is meant for discussions about users. Feel free to revert if you dislike this change.&nbsp;--[[User:Seal Enthusiast|Seal Enthusiast]] ([[User talk:Seal Enthusiast|talk]]) 18:09, 15 Apr 2024 (UTC)
 
== Folding functions ==
 
 
 
== Iterating through players ==
 
The most common method of iterating over all players in a map is like so:
 
<source lang=js>
::MaxPlayers <- MaxClients().tointeger();
 
for (local i = 1; i <= MaxPlayers ; i++)
{
    local player = PlayerInstanceFromIndex(i)
    if (player == null) continue
    printl(player)
}
</source>
 
While this solution is simple and efficient enough for most use cases, the fastest way to iterate over all players is to collect them in a separate array when the player has fully loaded into the server, then iterate over this array when needed.
 
<source lang=js>
::playerarray <- []
 
ClearGameEventCallbacks()
 
//player is activated and loaded into the server
function OnGameEvent_player_activate(params)
{
    local player = GetPlayerFromUserID(params.userid)
 
    //check if we're already in the array
    if (playerarray.find(player) != null) return
 
    //add player to the array if they don't already exist
    playerarray.append(player)
}
 
//player has left the server
function OnGameEvent_player_disconnect(params)
{
    local player = GetPlayerFromUserID(params.userid)
 
    //cache player length value
    local playerarray_len = playerarray.len()
   
    //reverse through the player array and remove invalid players
    for (local i = playerarray_len - 1; i >= 0; i--)
        if (playerarray[i] == null || playerarray[i] == player)  
            playerarray.remove(i)
}
__CollectGameEventCallbacks(this)
 
//iterate over array
foreach (player in playerarray)
    printl(player)
 
</source>

Latest revision as of 07:24, 30 August 2024

(using Template:Message)  Hello Braindawg I have moved all content from here, to this page as this page is meant for discussions about users. Feel free to revert if you dislike this change. --Seal Enthusiast (talk) 18:09, 15 Apr 2024 (UTC)