Creating custom weapons - VScript: Difference between revisions
From TF2 Classified Wiki
More actions
→Table and collector method: Added info about stacking tables, though this could be technical and may need to be cleaned up |
|||
| (4 intermediate revisions by 3 users not shown) | |||
| Line 1: | Line 1: | ||
{{stub}} | {{stub}} | ||
{{todo| | {{todo|Make the page more beginner friendly: | ||
Start simple | |||
Give plenty of examples that each go through multiple new things | |||
Add explanations for detecting actions like attack, attack2 and reload | |||
}} | |||
TF2C extends TF2 and MapBase's VScripting, making anything possible in either of them mostly possible in TF2C. | TF2C extends TF2 and MapBase's VScripting, making anything possible in either of them mostly possible in TF2C. | ||
VScript in TF2C uses [https://en.wikipedia.org/wiki/Squirrel_(programming_language) Squirrel] <sup>{{tooltip|Note|as of writing the actual squirrel website is inaccessible for whatever reason, so the Wikipedia page will have to do for now I suppose}}</sup>, which is also used by TF2 and MapBase for their VScripting as well. | |||
= Hooks = | = Hooks = | ||
A hook in modding is when you get the game to run a function, so you can perform custom behavior or replace existing behavior. We can't actually replace TF2C's code directly of course, so instead we use a tool provided to us by Valve and the TF2C team called | A hook in modding is when you get the game to run a function, so you can perform custom behavior or replace existing behavior. We can't actually replace TF2C's code directly of course, so instead we use a tool provided to us by Valve and the TF2C team called VScript.<br> | ||
Hooking can be done in two ways, a table and collector (making a table of functions and collecting them) and a direct hook to the game's events. As of writing, the writer here has no idea what the difference is. | Hooking can be done in two ways, a table and collector (making a table of functions and collecting them) and a direct hook to the game's events. As of writing, the writer here has no idea what the difference is. | ||
== Table and collector method == | == Table and collector method == | ||
This is the recommended method to allow other VScript mods to coexist with each other so long as they have uniquely named tables.<br> | |||
Here's an example table that disables Medic's health regen when he's holding a weapon that has a custom, schema defined attribute.<br> | |||
<pre> | <pre> | ||
::AttributeTable <- { | ::AttributeTable <- { | ||
| Line 56: | Line 65: | ||
} | } | ||
</pre> | </pre> | ||
It also doesn't need an attribute class either, since the | It also doesn't need an attribute class either, since the VScript only uses the attribute's name, so ideally you shouldn't have an attribute class on vscript attributes.<br> | ||
In the instance that two scripts do come with their own uniquely named tables that happen to hook on the same GameEvent, assuming multiple scripts are imported that each have their own tables, these tables will be executed in reverse order to the way they were imported, assuming you're using an importer. For example say I have table1 and table2 which both use the same <code>OnGameEvent_</code> and later we run <code>__CollectGameEventCallbacks(Table1)</code> and then <code>__CollectGameEventCallbacks(Table2)</code> after that. Table2 runs first even though the collector function was last. It's unknown ''why'' this happens, but it is something to keep in mind if you need to control the order in which each table is handled (for example you need variables to be changed before a different table starts using them) | |||
== Direct hook method == | == Direct hook method == | ||
The direct hook method is the same way the | The direct hook method is the same way the official April Fools weapon pack does its VScript attributes.{{InlineTodo|should we link this?}} | ||
For example we're going to pick apart one of them, specifically the "mod shoot self" attribute. | For example we're going to pick apart one of them, specifically the "mod shoot self" attribute. | ||
<pre> | <pre> | ||
| Line 106: | Line 117: | ||
For a much simpler example, here's a one-line piece of code that prints to the log whenever a gun is fired. <code>::ListenToGameEvent( "player_shoot", function(event){printl("doing things")}, "didathing")</code> In this instance, the table of info is passed to "event", which isn't used but is still required because the game still calls the function with that table regardless, and can't just throw it out. | For a much simpler example, here's a one-line piece of code that prints to the log whenever a gun is fired. <code>::ListenToGameEvent( "player_shoot", function(event){printl("doing things")}, "didathing")</code> In this instance, the table of info is passed to "event", which isn't used but is still required because the game still calls the function with that table regardless, and can't just throw it out. | ||
will print "doing things" to the console whenever we shoot. | will print "doing things" to the console whenever we shoot. | ||
= See also = | = See also = | ||
[https://developer.valvesoftware.com/wiki/Team_Fortress_2/Docs/Scripting/Script_Functions TF2's VScript Functions]<br> | [https://developer.valvesoftware.com/wiki/Team_Fortress_2/Docs/Scripting/Script_Functions TF2's VScript Functions]<br> | ||