- Home /
Match strings from an array to fire specific UnityEvents?
I am trying to find/create a system that will allow different things to happen on a single object at different times. That's a simplification but its the basic idea.
I want to be able to compare strings and fire specific Unity Events when the string matches.
I have looked at generic Unity events and creating a type so I can have an array of strings and events but the array isn't even showing up in the inspector for me to start testing.
Here is the pseudo code I have started with: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events;
public class ObjectEventString
{
public string cardType;
public UnityEvent objectEvents;
public ObjectEventString()
{
}
}
public class ObjectInteractionController : MonoBehaviour
{
//This aray is not showing up in the inspector?
public ObjectEventString[] variations = new ObjectEventString[2];
void Start()
{
}
public void ObjectEventAction()
{
// for each matching string from the ObjectEventString class cardType and the server cardType trigger an event.
//Something like:
//check the string in the inspector against strings being passed by the server
/* foreach(string cardID in variations)
{
//when the string matches
if(variations.cardType == server cardType from server)
//play the desired events and ignore the others.
cardID.variations.objectEvents.Invoke();
} */
}
}
Thank you.
Answer by Giantbean · Sep 15, 2021 at 02:17 PM
This does what I want
private string owningType = "";
[Tooltip("String Array this interactable could apply to")]
public string[] interactionType;
[Tooltip("Array of events that will fire when matched at index with a string from above array upon interaction from player")]
public UnityEvent[] interactableInteractedWithEvent;
[Tooltip("Default event fired upon interaction from player")]
public UnityEvent defaultInteractedWithEvent;
void Start()
{
//left start so I can disable the script in the inspector.
}
public void CheckForInteractedEvent()
{
Debug.Log("CHECK FOR INTERACTED EVENT");
// loop through the strings of cardTypes i.e. "criticalComputerUpdate"
for (int i = 0; i < cardType.Length; i++)
{
// if it matches get the index and pass that index into the array of events to fire the event we tied to it
if(owningType == cardType[i])
{
Debug.Log("Fire the associated INTERACTED Event!");
interactableInteractedWithEvent[i].Invoke();
}
else
{
// do default...
defaultInteractedWithEvent.Invoke();
}
}
}
public void CheckForInteractedCompletedEvent()
{
Debug.Log("CHECK FOR INTERACTED COMPLETED EVENT");
defaultInteractedWithEvent.Invoke();
owningType = "";
}
Answer by qsp18 · Aug 20, 2021 at 07:06 PM
Make a dictionary. The Key is a string, the Value is an Action
Sorry newb here, I have not used dictionary's and while I have been looking at them the past few days I don't understand how the Value can be a UnityEvent that I can set in the inspector to do different things at different times. I would like to put this script on as many as 150 objects in a scene and each object could have between 1 and 6 different events that can happen based on the string that is compared. can the dictionary populate an array of unity events in the inspector?
Well I have used a dictionary now and its still not doing what I want.