IM.GetVariable
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.