Category:Interfaces: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
(explained interfaces)
mNo edit summary
 
(8 intermediate revisions by 5 users not shown)
Line 1: Line 1:
'''[[Wikipedia:Interface (computer science)|Interfaces]]''' are important C++ classes that are accessed by the engine. To use an interface you create or modify an inherited class, then cast back to the interface class in a global [[pointer]] with a predefined name.
{{lang|Category:Interfaces}}


{{warning|'''''Never, ever''''' modify an interface class itself. You should always inherit then cast back.}}
'''[[Wikipedia:Interface (computer science)|Interfaces]]''' are C++ classes that other parts of the engine access when your mod is running.
 
Some interface objects are very important, and are required for game DLLs to function; to use these you inherit from them for implementation, then cast back to the interface class in a global [[pointer]] with a predefined name.
 
{{note|If you try to construct an interface directly, your mod will crash. This is done deliberately.}}
 
{{warning|It is important to create ''all'' objects listed when exposing a vital interface, even if your mod can be compiled and run without one or another of them. If you try to cut corners you are likely to encounter weird, almost impossible to debug errors either today or after a future engine update!}}
 
{{warning|'''''Never, ever''''' modify an interface class itself. You must always inherit then cast back.}}


== Example ==
== Example ==


<source lang=cpp>IInput* input = new CInput;</source>
<syntaxhighlight lang=cpp>static CInput g_Input;
IInput* input = (IInput*)&g_Input;</syntaxhighlight>


This creates the <code>[[IInput]]* input</code> object the engine expects, placing behind it Valve's basic input code from <code>[[CInput]]</code>.
This creates the <code>[[IInput]]* input</code> and <code>g_Input</code> objects the engine expects, placing behind them Valve's basic input code from <code>[[CInput]]</code>.


You are free to modify <code>CInput</code> or to create your own inherited class and use that instead; just remember that only the members of <code>IInput</code> are known to the engine.
You are free to modify <code>CInput</code>, or to create your own inherited class and use that instead; just remember that only the members of <code>IInput</code> will be accessed by the engine.


[[Category:Programming]]
[[Category:C++]]

Latest revision as of 10:03, 24 January 2025

English (en)Deutsch (de)Русский (ru)Translate (Translate)

Interfaces are C++ classes that other parts of the engine access when your mod is running.

Some interface objects are very important, and are required for game DLLs to function; to use these you inherit from them for implementation, then cast back to the interface class in a global pointer with a predefined name.

Note.pngNote:If you try to construct an interface directly, your mod will crash. This is done deliberately.
Warning.pngWarning:It is important to create all objects listed when exposing a vital interface, even if your mod can be compiled and run without one or another of them. If you try to cut corners you are likely to encounter weird, almost impossible to debug errors either today or after a future engine update!
Warning.pngWarning:Never, ever modify an interface class itself. You must always inherit then cast back.

Example

static CInput g_Input;
IInput* input = (IInput*)&g_Input;

This creates the IInput* input and g_Input objects the engine expects, placing behind them Valve's basic input code from CInput.

You are free to modify CInput, or to create your own inherited class and use that instead; just remember that only the members of IInput will be accessed by the engine.

Subcategories

This category has the following 5 subcategories, out of 5 total.

I