User talk:Braindawg

From Valve Developer Community
Revision as of 17:01, 5 April 2024 by Braindawg (talk | contribs)
Jump to navigation Jump to search

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 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)

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.