Solved Triggering a plugin by Coding

bot1789

Member
Messages
36
Reactions
65
Points
18
How can I trigger the MacGruber Relay plugin (from MacGruber LogicBricks) attached to a Person from my C# script/plugin?
Unfortunately, I did not find an example of such code. But I would like to have this option, since in this case there is no need to write all actions in my C# script.
 
Solution
Something like this?

C#:
using UnityEngine;
using UnityEngine.Events;

namespace MacGruber
{
    public class TriggerExample : MVRScript
    {   
        JSONStorableAction myAction;

        public override void Init()
        {
            // These are the same fields as when you setup a trigger in VaM's UI.
            string atomName = "Person#2";
            string receiverName = "plugin#1_MacGruber.Relay";
            string receiverTarget = "Trigger";
            
            // For best performance we cache the action once for later use.
            myAction = GetAtomById(atomName)?.GetStorableByID(receiverName)?.GetAction(receiverTarget);   


            // Just setting up some button so we can show how to trigger the action...
Something like this?

C#:
using UnityEngine;
using UnityEngine.Events;

namespace MacGruber
{
    public class TriggerExample : MVRScript
    {   
        JSONStorableAction myAction;

        public override void Init()
        {
            // These are the same fields as when you setup a trigger in VaM's UI.
            string atomName = "Person#2";
            string receiverName = "plugin#1_MacGruber.Relay";
            string receiverTarget = "Trigger";
            
            // For best performance we cache the action once for later use.
            myAction = GetAtomById(atomName)?.GetStorableByID(receiverName)?.GetAction(receiverTarget);   


            // Just setting up some button so we can show how to trigger the action.
            SetupButton("Baduum!", OnButtonPressed, false);
        }
                
        private void OnButtonPressed()
        {
            // Note that myAction may end up being null, when the target did not exist at the moment we cached it or was deleted later.
            if (myAction != null)
                myAction.actionCallback();
        }       
        
        public void SetupButton(string label, UnityAction callback, bool rightSide)
        {
            UIDynamicButton button = CreateButton(label, rightSide);
            button.button.onClick.AddListener(callback);
        }
    }
}
 
Upvote 1
Solution
Something like this?

C#:
using UnityEngine;
using UnityEngine.Events;

namespace MacGruber
{
    public class TriggerExample : MVRScript
    {  
        JSONStorableAction myAction;

        public override void Init()
        {
            // These are the same fields as when you setup a trigger in VaM's UI.
            string atomName = "Person#2";
            string receiverName = "plugin#1_MacGruber.Relay";
            string receiverTarget = "Trigger";
           
            // For best performance we cache the action once for later use.
            myAction = GetAtomById(atomName)?.GetStorableByID(receiverName)?.GetAction(receiverTarget);  


            // Just setting up some button so we can show how to trigger the action.
            SetupButton("Baduum!", OnButtonPressed, false);
        }
               
        private void OnButtonPressed()
        {
            // Note that myAction may end up being null, when the target did not exist at the moment we cached it or was deleted later.
            if (myAction != null)
                myAction.actionCallback();
        }      
       
        public void SetupButton(string label, UnityAction callback, bool rightSide)
        {
            UIDynamicButton button = CreateButton(label, rightSide);
            button.button.onClick.AddListener(callback);
        }
    }
}
It works. Thank you!!
 
Upvote 0
I would like to specify an additional variable, for example "stringValue", for the attached matter. Is it possible?
Preferably, there is also a way to change that value inside the JSONStorableAction,

Thanks for reading.
 
Upvote 0
I would like to specify an additional variable, for example "stringValue", for the attached matter. Is it possible?
Preferably, there is also a way to change that value inside the JSONStorableAction,

Thanks for reading.
JSONStorableAction is used for parameter-less calls. There are a couple of other types....JSONStorableString and JSONStorableFloat among them.
Use ILSpy to look at the available methods and classes. Or just look at various plugins how they do it.
 
Upvote 0
Does that mean you figured it out? Or that you gave up because you didn't understand my answer? :unsure:
I was able to generate a trigger that allows to modify variables using other methods, but
Since the number of lines in the source increases, I was doing trial and error thinking that it would be better if I could do it this method.
That didn't work, so I asked for advice.
I understand this method wouldn't serve my purpose after all, so I'm very grateful that I didn't have to spend any more time on it.

I have never used ILSpy, but I have used some other people's codes as reference.
thank you for worrying.
 
Upvote 0
Hello everyone!
I would like to ask for help again. I want to trigger the MacGruber SequenceChoice plugin from my script. In this case, I need not only to launch the trigger, as for the MacGruber Relay, but also to set a value of the SelectItem variable.

I tried to implement it like this:
C#:
   void CallAction2(int actionID, string SelectItemName)
        {
          
            myString = containingAtom.GetStorableByID("plugin#" + actionID + "_MacGruber.SequenceChoice").GetStringJSONParam("SelectItem");
           myString =  new JSONStorableString("SelectItem", SelectItemName);

             myAction = containingAtom.GetStorableByID("plugin#" + actionID + "_MacGruber.SequenceChoice").GetAction("Trigger");
              if (myAction != null) myAction.actionCallback();

        }

where myString is the JSONStorableString variable.

There is no syntax error, but it also does not work correctly : (

Can you explain how to correctly select one of the choices and launch the MacGruber SequenceChoice plugin attached to a Person atom from my script?
 
Last edited:
Upvote 0
This creates a new JSONStorableString object...meaning you ignore the previously retrieved one :D
Use the .val property....like this:
C#:
myString.val = SelectItemName;
Oh yes : ) Thank you very much!

It works correctly this way:
C#:
void CallAction2(int pluginID, string SelectItemName)
        {
          myString = containingAtom.GetStorableByID("plugin#" + pluginID + "_MacGruber.SequenceChoice").GetStringJSONParam("SelectItem");
           myString.val =  SelectItemName;

            myAction1 = containingAtom.GetStorableByID("plugin#" + pluginID + "_MacGruber.SequenceChoice").GetAction("Trigger");
             if (myAction1 != null) myAction1.actionCallback();
        }
 
Upvote 0
Back
Top Bottom