User talk:Braindawg: Difference between revisions
No edit summary |
|||
Line 3: | Line 3: | ||
Folding functions in the context of VScript means folding them into the root table. It is recommended that you do this for functions that are commonly used in expensive operations, and for functions that have unique names that are not shared by other classes. Not only is this more readable and easier to write, but it also skips the extra step where the game needs to first find the function in a table before executing it. | Folding functions in the context of VScript means folding them into the root table. It is recommended that you do this for functions that are commonly used in expensive operations, and for functions that have unique names that are not shared by other classes. Not only is this more readable and easier to write, but it also skips the extra step where the game needs to first find the function in a table before executing it. | ||
The following example folds all NetProp related functions into the root table. for example, <code>NetProps.GetPropString(...)</code> would simply become <code>GetPropString(...)</code>. This can yield performance improvements of up to 20%{{confirm}} | The following example folds all NetProp and Entity related functions into the root table. for example, <code>NetProps.GetPropString(...)</code> would simply become <code>GetPropString(...)</code>. This can yield performance improvements of up to 20%{{confirm}} | ||
<source lang=js> | <source lang=js> | ||
Line 9: | Line 9: | ||
if (k != "IsValid" && !(k in ROOT)) | if (k != "IsValid" && !(k in ROOT)) | ||
ROOT[k] <- ::NetProps[k].bindenv(::NetProps) | ROOT[k] <- ::NetProps[k].bindenv(::NetProps) | ||
foreach(k, v in ::Entities.getclass()) | |||
if (k != "IsValid" && !(k in ROOT)) | |||
ROOT[k] <- ::Entities[k].bindenv(::Entities) | |||
local gamerules = FindByClassname(null, "tf_gamerules") // "Entities." prefix is no longer necessary | |||
local gamerules_targetname = GetPropString(gamerules, "m_iName") // "NetProps." prefix is no longer necessary | |||
</source> | </source> | ||
Revision as of 11:15, 6 April 2024
Folding Functions
Folding functions in the context of VScript means folding them into the root table. It is recommended that you do this for functions that are commonly used in expensive operations, and for functions that have unique names that are not shared by other classes. Not only is this more readable and easier to write, but it also skips the extra step where the game needs to first find the function in a table before executing it.
The following example folds all NetProp and Entity related functions into the root table. for example, NetProps.GetPropString(...)
would simply become GetPropString(...)
. This can yield performance improvements of up to 20%[confirm]
foreach(k, v in ::NetProps.getclass())
if (k != "IsValid" && !(k in ROOT))
ROOT[k] <- ::NetProps[k].bindenv(::NetProps)
foreach(k, v in ::Entities.getclass())
if (k != "IsValid" && !(k in ROOT))
ROOT[k] <- ::Entities[k].bindenv(::Entities)
local gamerules = FindByClassname(null, "tf_gamerules") // "Entities." prefix is no longer necessary
local gamerules_targetname = GetPropString(gamerules, "m_iName") // "NetProps." prefix is no longer necessary
Constants
Folding Constants
Similar to folding functions, folding pre-defined Constant values into the constant table (or the root table) makes referencing them less cumbersome, as well as increasing performance.
::ROOT <- getroottable();
if (!("ConstantNamingConvention" in ROOT)) // make sure folding is only done once
{
foreach (a,b in Constants)
foreach (k,v in b)
ROOT[k] <- v != null ? v : 0;
}
You may have noticed that we are folding our constants into the root table instead of the constant table. This will be explained below
Root table vs Constant table
Unlike values inserted into the root table, values inserted into the constant table are cached at the pre-processor level. What this means is, while accessing them can be faster (up to 1.5x faster to be more precise), it may not be feasible to fold your constants into the constant table
If you intend to insert values into the constant table, you must do this before any other scripts are executed, otherwise your script will not be able to read any values from it.