IM.GetVariable: Difference between revisions

From Intrigues Wiki
(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...")
 
No edit summary
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
== <code>GetVariable</code> Method in IM Class ==
== <code>GetVariable</code> and <code>GetVariable<T></code> Methods in IM Class ==


=== Overview ===
=== Overview ===
The <code>GetVariable</code> method in the IM (Intrigue Manager) class retrieves a public variable based on its name or ID.
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 either the name or the ID of the desired variable.
* <code>variableNameOrId</code>: A string representing the name or ID of the desired variable.


=== Functionality ===
=== Functionality ===


* Searches the game's variable collection for a variable that matches the provided name or ID.
* GetVariable (Non-generic):
* Returns the corresponding <code>NVar</code> object if found.
** Searches for a variable that matches the provided name or ID.
* If no matching variable is found, returns a new <code>NString</code> object with a placeholder indicating the variable's absence.
** 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 ===
Used for dynamic access to public variables within the game, enabling retrieval and manipulation of game data and logic based on variable identifiers.
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 string variableKey;
     public void DisplayVariableValue() {
        // Non-generic usage
        NVar variable = IM.GetVariable("GameMode");
        Debug.Log($"Value of variable GameMode: {variable.value}");


    public void DisplayVariableValue() {
        // Generic usage
         NVar variable = IM.GetVariable(variableKey);
         NEnum intVariable = IM.GetVariable<NEnum>("GameMode");
         Debug.Log($"Value of variable '{variableKey}': {variable.value}");
         if (intVariable != null) {
            Debug.Log($"Enum value of variable GameMode: {intVariable.value}");
        }
     }
     }
}
}
Line 36: Line 45:
==== Description ====
==== Description ====


* <code>DisplayVariableValue</code>: Demonstrates fetching a variable using <code>IM.GetVariable</code> and logging its value.
* <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 ===


* Essential in games using a data-driven approach for controlling game behavior or storing state.
* These methods are vital in games that employ a variable-driven approach for controlling game behavior or storing game state.
* Ensures effective access and utilization of variables for enhanced game mechanics.
* 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 new NString 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 new NString object cast to T if the variable is not found.

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