IM.GetVariable: Difference between revisions
Tayfunwiki (talk | contribs) (Created page with "== <code>GetVariable</code> Method in IM Class == === Overview === The <code>GetVariable</code> method in the IM (Intrigue Manager) class retrieves a public variable based on its name or ID. === Syntax === <syntaxhighlight lang="c#"> public static NVar GetVariable(string variableNameOrId) </syntaxhighlight> === Parameters === * <code>variableNameOrId</code>: A string representing either the name or the ID of the desired variable. === Functionality === * Searches th...") |
Tayfunwiki (talk | contribs) No edit summary |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
== <code>GetVariable</code> | == <code>GetVariable</code> and <code>GetVariable<T></code> Methods in IM Class == | ||
=== Overview === | === Overview === | ||
The <code>GetVariable</code> | The <code>GetVariable</code> methods in the IM class are designed to retrieve public variables based on their names or IDs. The methods are overloaded to allow for a variable to be retrieved either as a generic <code>NVar</code> object or as a specific type <code>T</code> derived from <code>NVar</code>. | ||
=== Syntax === | === Syntax === | ||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
public static NVar GetVariable(string variableNameOrId) | public static NVar GetVariable(string variableNameOrId) | ||
public static T GetVariable<T>(string variableNameOrId) where T : NVar | |||
</syntaxhighlight> | </syntaxhighlight> | ||
=== Parameters === | === Parameters === | ||
* <code>variableNameOrId</code>: A string representing | * <code>variableNameOrId</code>: A string representing the name or ID of the desired variable. | ||
=== Functionality === | === Functionality === | ||
* Searches | * GetVariable (Non-generic): | ||
* Returns the corresponding <code>NVar</code> object if found. | ** Searches for a variable that matches the provided name or ID. | ||
* | ** Returns the corresponding <code>NVar</code> object or a new <code>NString</code> object with a placeholder if the variable is not found. | ||
* GetVariable<T> (Generic): | |||
** Similar to the non-generic version, but casts the found variable to the specified type <code>T</code>. | |||
** Returns the variable as type <code>T</code> or a new <code>NString</code> object cast to <code>T</code> if the variable is not found. | |||
=== Usage === | === Usage === | ||
These methods are used for dynamic access to public variables within the game. They enable the retrieval and manipulation of game data and logic based on variable identifiers. | |||
=== Example of Usage === | === Example of Usage === | ||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
public class VariableManager : MonoBehaviour { | public class VariableManager : MonoBehaviour { | ||
public | public void DisplayVariableValue() { | ||
// Non-generic usage | |||
NVar variable = IM.GetVariable("GameMode"); | |||
Debug.Log($"Value of variable GameMode: {variable.value}"); | |||
// Generic usage | |||
NEnum intVariable = IM.GetVariable<NEnum>("GameMode"); | |||
Debug.Log($" | if (intVariable != null) { | ||
Debug.Log($"Enum value of variable GameMode: {intVariable.value}"); | |||
} | |||
} | } | ||
} | } | ||
Line 36: | Line 45: | ||
==== Description ==== | ==== Description ==== | ||
* <code>DisplayVariableValue</code>: Demonstrates | * <code>DisplayVariableValue</code>: Demonstrates both the non-generic and generic usage of <code>IM.GetVariable</code> methods. It shows how to fetch a variable and log its value, as well as how to fetch and handle a typed variable. | ||
=== Remarks === | === Remarks === | ||
* | * These methods are vital in games that employ a variable-driven approach for controlling game behavior or storing game state. | ||
* | * Proper usage ensures effective access and utilization of variables, enhancing flexibility in game mechanics. | ||
* They are especially relevant in strategy games, simulation games, or any genre involving complex data management. |
Latest revision as of 15:26, 4 January 2024
GetVariable
and GetVariable<T>
Methods in IM Class
Overview
The GetVariable
methods in the IM class are designed to retrieve public variables based on their names or IDs. The methods are overloaded to allow for a variable to be retrieved either as a generic NVar
object or as a specific type T
derived from NVar
.
Syntax
public static NVar GetVariable(string variableNameOrId)
public static T GetVariable<T>(string variableNameOrId) where T : NVar
Parameters
variableNameOrId
: A string representing the name or ID of the desired variable.
Functionality
- GetVariable (Non-generic):
- Searches for a variable that matches the provided name or ID.
- Returns the corresponding
NVar
object or a newNString
object with a placeholder if the variable is not found.
- GetVariable<T> (Generic):
- Similar to the non-generic version, but casts the found variable to the specified type
T
. - Returns the variable as type
T
or a newNString
object cast toT
if the variable is not found.
- Similar to the non-generic version, but casts the found variable to the specified type
Usage
These methods are used for dynamic access to public variables within the game. They enable the retrieval and manipulation of game data and logic based on variable identifiers.
Example of Usage
public class VariableManager : MonoBehaviour {
public void DisplayVariableValue() {
// Non-generic usage
NVar variable = IM.GetVariable("GameMode");
Debug.Log($"Value of variable GameMode: {variable.value}");
// Generic usage
NEnum intVariable = IM.GetVariable<NEnum>("GameMode");
if (intVariable != null) {
Debug.Log($"Enum value of variable GameMode: {intVariable.value}");
}
}
}
Description
DisplayVariableValue
: Demonstrates both the non-generic and generic usage ofIM.GetVariable
methods. It shows how to fetch a variable and log its value, as well as how to fetch and handle a typed variable.
Remarks
- These methods are vital in games that employ a variable-driven approach for controlling game behavior or storing game state.
- Proper usage ensures effective access and utilization of variables, enhancing flexibility in game mechanics.
- They are especially relevant in strategy games, simulation games, or any genre involving complex data management.