Policy.onPolicyNameChanged

From Intrigues Wiki
Revision as of 09:15, 5 January 2024 by Tayfunwiki (talk | contribs) (Created page with "=== Policy Class - <code>onPolicyNameChanged</code> Event === ==== Overview ==== The <code>onPolicyNameChanged</code> event in the Policy class is triggered when the name of a policy is changed. It's useful for updating UI elements or other game components that depend on the policy name. ==== Syntax ==== <syntaxhighlight lang="c#"> public event Action<string> onPolicyNameChanged; </syntaxhighlight> ==== Description ==== This event is attached to a specific policy and...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Policy Class - onPolicyNameChanged Event

Overview

The onPolicyNameChanged event in the Policy class is triggered when the name of a policy is changed. It's useful for updating UI elements or other game components that depend on the policy name.

Syntax

public event Action<string> onPolicyNameChanged;

Description

This event is attached to a specific policy and gets triggered when its name is altered. The event passes the old name of the policy as a parameter.

Usage

This event can be used to synchronize the policy's name across different parts of the game, like updating UI elements to reflect the new name.

Example of Usage

public class PolicyManager : MonoBehaviour {
    private Policy currentPolicy;

    void Start() {
        // Fetching the 'Economy' policy from the game's policy manager
        currentPolicy = IM.GetPolicy("Economy");

        if (currentPolicy != null) {
            // Subscribing to the policy name change event
            currentPolicy.onPolicyNameChanged += OnPolicyNameChanged;
        }
    }

    private void OnPolicyNameChanged(string oldName) {
        // Handling the policy name change event
        Debug.Log($"Policy name changed from {oldName} to {currentPolicy.PolicyName}");
    }

    void OnDestroy() {
        if (currentPolicy != null) {
            // Unsubscribing from the policy name change event
            currentPolicy.onPolicyNameChanged -= OnPolicyNameChanged;
        }
    }
}

Remarks

  • The event handler OnPolicyNameChanged is called when the name of the currentPolicy changes.
  • It's important to unsubscribe from the event in the OnDestroy method to avoid memory leaks and ensure proper cleanup.
  • The IM.GetPolicy method is used to get the specific policy instance from the game's policy manager.