Page 1 of 1

Passing dynamic parameters to scene independent events/Custom Script functions

Posted: Sun May 02, 2021 7:29 am
by jksleary
Hi Tony,

I have a query regarding passing dynamic values to custom functions registered with Lua and/or scene independent events.

Context is an rts style strategy game where your units (volunteers) are instantiated at runtime on a strategymap.
These units are each named Actors in the database. The 3d model for the Actor in the gameworld is named "Actorname+VolunteerModel"

At runtime, under certain conditions, these actors can have conversations initiated with NPC conversants (simulating a police/army checkpoint) where the potential outcomes vary from no effect to the unit is killed/arrested.

The plan is to use one generic Checkpoint conversation and dynamically assign actor/conversant at runtime depending which unit initiates the Checkpoint conversation. The player then acts as that Actor for the particular conversation.

I have a custom Lua Function registered called ArrestVolunteer:

Code: Select all

 public class CheckpointFunctions : MonoBehaviour // Rename this class.
    {
        WMSK map;// reference to the strategy map
        [Tooltip("Typically leave unticked so temporary Dialogue Managers don't unregister your functions.")]
        public bool unregisterOnDisable = false;

        void OnEnable()
        {
            map = WMSK.instance;
            // Make the functions available to Lua: (Replace these lines with your own.)
            Lua.RegisterFunction("ArrestVolunteer", this, SymbolExtensions.GetMethodInfo(() => ArrestVolunteer(string.Empty)));
            //Lua.RegisterFunction("AddOne", this, SymbolExtensions.GetMethodInfo(() => AddOne((double)0)));
        }

        void OnDisable()
        {
            if (unregisterOnDisable)
            {
                // Remove the functions from Lua: (Replace these lines with your own.)
                Lua.UnregisterFunction("ArrestVolunteer");
              //  Lua.UnregisterFunction("AddOne");
            }
        }

        public void ArrestVolunteer(string volunteername)
        {
            GameObject go = GameObject.Find(volunteername + "VolunteerModel");
            VolunteerModel vm = go.GetComponent<VolunteerModel>();
            vm.VolunteerData.VolunteerStatus = VolunteerStatus.Custody;
            GameObjectAnimator anim = go.GetComponent<GameObjectAnimator>();
            map.VGOUnRegisterGameObject(anim);
            DestroyImmediate(go);


        }

I also have a scriptable object based custom GameEvent asset which I would like to be able to pass the same string to by raising it as the target of a Scene Independent Event with a string parameter.


Question is, how would I go about passing the Actor name dynamically as a string (or partially dynamic, like:

ArrestVolunteer(CurrentActorName+"VolunteerModel")
when I wont know the Actors name ahead of runtime? and essentially the same question for passing CurrentActor Name as a parameter to a scene independent Event.

Thanks in advance for any help.

Cheers,

JC

Re: Passing dynamic parameters to scene independent events/Custom Script functions

Posted: Sun May 02, 2021 9:06 am
by Tony Li
Hi,

Add DialogueActor components to your instantiated units, and set their actor properties to the name of the actor in your dialogue database.

When a conversation starts, the Dialogue System sets four Lua variables (full details), including Variable["Actor"] and Variable["Conversant"]. Assuming your unit is being used as the conversation actor in your generic conversation, its name will be in Variable["Actor"]. You can use it in Lua like this to pass the actor name to ArrestVolunteer:

Code: Select all

ArrestVolunteer(Variable["Actor"])
If you need to append the string "VolunteerModel", use this:

Code: Select all

ArrestVolunteer(Variable["Actor"] .. "VolunteerModel")
(Note that Lua uses ".." to concatenate strings.)

If your unit is being used as the conversation conversant, use Variable["Conversant"] instead.

Related info: Character GameObject Assignments. (<- Explains how GameObjects and their corresponding actors/names get assigned to runtime conversations.)

Re: Passing dynamic parameters to scene independent events/Custom Script functions

Posted: Sun May 02, 2021 2:30 pm
by jksleary
Great stuff Tony. Amazing as always. Thanks a million!

Re: Passing dynamic parameters to scene independent events/Custom Script functions

Posted: Sun May 02, 2021 2:44 pm
by Tony Li
Happy to help!

Re: Passing dynamic parameters to scene independent events/Custom Script functions

Posted: Wed Jun 02, 2021 4:18 pm
by timbecile
Hey Tony, just came across this in a search and had a question...

Is there a way to pass the actual GameObject (or a script) to a LUA Fuction? I plan on using a script to store configuration data (a list of places this NPC can travel to) in a script and pass that script to a LUA function for the UI travel elements

Re: Passing dynamic parameters to scene independent events/Custom Script functions

Posted: Wed Jun 02, 2021 4:47 pm
by Tony Li
Hi,

No. But you can access the conversation's current actor and conversant GameObjects: DialogueManager.currentActor and DialogueManager.currentConversant.

Alternatively, you can use dialogue entries' OnExecute() events. They're UnityEvents to which you can assign scene GameObjects or assets in the project such as ScriptableObject assets.