← Back to all addons

AutoGenerate Flags (monsters, chests, any)

Posted by Mr.Titan on Oct 6, 2024 at 12:24 AM

ExperimentalFeature
💡 4

Just add [ExecuteInEditMode] on top of the class, and create a function called OnValidate which the unity executes automatically.

`csharp

using UnityEngine;

namespace Gyvr.Mythril2D
{
    [ExecuteInEditMode]
    public class YourClass:MonoBehaviour
    {
        [SerializeField] private string m_gameFlagID = "";

        // Only generate the flagId if it's empty or null
        private void OnValidate()
        {
            // Get the current date and time in the format yymmddhhmmss
            if(m_gameFlagID.IsNullOrEmpty())
              m_gameFlagID = "CLASS_NAME_OR_SOMETHING_CLEAR" + System.DateTime.Now.ToString("yyMMddHHmmss");
            // Make the flagId persist by marking the object as dirty
            UnityEditor.EditorUtility.SetDirty(this);
        }
    }
}

💬 Comments (3)

Gyvr Oct 06, 2024 at 04:08 AM
Very nice! I actually was thinking about implementing a proper system for persistence, like a “PersistanceSystem” with an associated MonoBehaviour you could attach to a GameObject to store its current state, which could then be used for chests and other things on the map (ex: a door, switch, breakable pot…)
BellBlitzKing Oct 07, 2024 at 04:55 PM
Hope you end up implementing that system one day! To make it easier to build persistent maps/towns over time.
👌 1
SvanDark Oct 17, 2024 at 10:38 AM
I might just be pepega, but which script should i add this into?

Want to continue the conversation?