IM.GetVariable: Difference between revisions

From Intrigues Wiki
No edit summary
No edit summary
 
Line 31: Line 31:
     public void DisplayVariableValue() {
     public void DisplayVariableValue() {
         // Non-generic usage
         // Non-generic usage
         NVar variable = IM.GetVariable("Coin");
         NVar variable = IM.GetVariable("GameMode");
         Debug.Log($"Value of variable '{variableKey}': {variable.value}");
         Debug.Log($"Value of variable GameMode: {variable.value}");


         // Generic usage
         // Generic usage
         NInt intVariable = IM.GetVariable<NInt>("Coin");
         NEnum intVariable = IM.GetVariable<NEnum>("GameMode");
         if (intVariable != null) {
         if (intVariable != null) {
             Debug.Log($"Integer value of variable '{variableKey}': {intVariable.Value}");
             Debug.Log($"Enum value of variable GameMode: {intVariable.value}");
         }
         }
     }
     }

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.