playMaker

Author Topic: calling specific FSM from Unity script on game object with multiple FSMs[SOLVED]  (Read 2019 times)

weobi

  • Playmaker Newbie
  • *
  • Posts: 2
Been working on a hide-and-seek game (just flavor, not technical context), player gameObjects will run several FSMs to deal with input/movement, collision and word interaction, etc.

I currently have a player controller script scraping inputs from InControl, storing them locally, then throwing them to a FSM by assigning values to local FSM variables. This setup works well on my player gameObject as long as it has only one FSM. Where I'm having trouble is in referencing a specific FSM (to which I can throw my InControl values).

I'm a total hack in C#, not much of a coder... maybe that's why I couldn't get this to work for me: https://hutonggames.fogbugz.com/default.asp?W329 or this to work either: https://matthewwaring.wordpress.com/2014/01/14/using-playmaker-with-code-quick-introduction/

Script here (note: yes, I'm scraping everything from InControl, sending everything to a single FSM):
Code: [Select]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using InControl;                        // needed for InControl

public class seeker_controller_script : MonoBehaviour
{
    // allows this script to interact with FSM attached to gameobject
    public PlayMakerFSM theFsm;
    //public static PlayMakerFSM FindFsmOnGameObject(GameObject go, string controller_input_FSM);

    // declare variables to store input from controller
    float leftStickX = 0f;
    float leftStickY = 0f;
    bool leftStickButton = false;

    float rightStickX = 0f;
    float rightStickY = 0f;
    bool rightStickButton = false;

    bool leftTrigger = false;
    bool leftBumper = false;
    bool rightTrigger = false;
    bool rightBumper = false;

    bool buttonO = false;
    bool buttonA = false;
    bool buttonU = false;
    bool buttonY = false;

    bool dPadLeft = false;
    bool dPadRight = false;
    bool dPadUp = false;
    bool dPadDown = false;


    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        // device input setup
        var seekerPlayerDevice = InputManager.Devices[0];

        // assign input from controller to local variables
        leftStickX = seekerPlayerDevice.LeftStickX;
        leftStickY = seekerPlayerDevice.LeftStickY;
        leftStickButton = seekerPlayerDevice.LeftStickButton;

        rightStickX = seekerPlayerDevice.RightStickX;
        rightStickY = seekerPlayerDevice.RightStickY;
        rightStickButton = seekerPlayerDevice.RightStickButton;

        leftTrigger = seekerPlayerDevice.LeftTrigger;
        leftBumper = seekerPlayerDevice.LeftBumper;
        rightTrigger = seekerPlayerDevice.RightTrigger;
        rightBumper = seekerPlayerDevice.RightBumper;

        buttonO = seekerPlayerDevice.Action1;
        buttonU = seekerPlayerDevice.Action2;
        buttonY = seekerPlayerDevice.Action3;
        buttonA = seekerPlayerDevice.Action4;

        dPadLeft = seekerPlayerDevice.DPadLeft;
        dPadRight = seekerPlayerDevice.DPadRight;
        dPadUp = seekerPlayerDevice.DPadUp;
        dPadDown = seekerPlayerDevice.DPadDown;

        // assign locally stored input from controller to PlayMaker FSM variables - must also create these in FSM!!
        theFsm.FsmVariables.GetFsmFloat("moveX").Value = leftStickX;
        theFsm.FsmVariables.GetFsmFloat("moveZ").Value = leftStickY; // note: leftStickY controls moveZ!!
        theFsm.FsmVariables.GetFsmBool("leftStickButton").Value = leftStickButton;

        theFsm.FsmVariables.GetFsmFloat("rightStickX").Value = rightStickX;
        theFsm.FsmVariables.GetFsmFloat("rightStickY").Value = rightStickY;
        theFsm.FsmVariables.GetFsmBool("rightStickButton").Value = rightStickButton;

        theFsm.FsmVariables.GetFsmBool("leftTrigger").Value = leftTrigger;
        theFsm.FsmVariables.GetFsmBool("leftBumper").Value = leftBumper;
        theFsm.FsmVariables.GetFsmBool("rightTrigger").Value = rightTrigger;
        theFsm.FsmVariables.GetFsmBool("rightBumper").Value = rightBumper;

        theFsm.FsmVariables.GetFsmBool("buttonO").Value = buttonO;
        theFsm.FsmVariables.GetFsmBool("buttonU").Value = buttonU;
        theFsm.FsmVariables.GetFsmBool("buttonY").Value = buttonY;
        theFsm.FsmVariables.GetFsmBool("buttonA").Value = buttonA;

        theFsm.FsmVariables.GetFsmBool("dPadLeft").Value = dPadLeft;
        theFsm.FsmVariables.GetFsmBool("dPadRight").Value = dPadRight;
        theFsm.FsmVariables.GetFsmBool("dPadUp").Value = dPadUp;
        theFsm.FsmVariables.GetFsmBool("dPadDown").Value = dPadDown;

    }
}

Any insight would be helpful! Thank you.
« Last Edit: November 26, 2019, 04:15:11 AM by djaydino »

weobi

  • Playmaker Newbie
  • *
  • Posts: 2
Welp, a real-life human helped me by finding another post:
https://hutonggames.com/playmakerforum/index.php?topic=1143.0

and here's the code I wound up including:
Code: [Select]
{
    public PlayMakerFSM theFsm;

    // Start is called before the first frame update
    void Start()
    {
        PlayMakerFSM[] temp = GetComponents<PlayMakerFSM>();
        foreach (PlayMakerFSM fsm in temp)
        {

            if (fsm.FsmName == "layer_switch")
            {
                theFsm = fsm;
                break;
            }
        }

        theFsm.FsmVariables.GetFsmBool("hello").Value = true;
    }

    // Update is called once per frame
    void Update()
    {
       
    }
}

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Hi,

 yeah... I am no bot :)

Bye,

 Jean