playMaker

Author Topic: InControl Player Disconnecting Action  (Read 2821 times)

OliverAge24

  • Playmaker Newbie
  • *
  • Posts: 8
InControl Player Disconnecting Action
« on: August 09, 2015, 06:29:37 AM »
Hey,

I'm working on a single-player game with Playmaker that supports keyboard+mouse and gamepads.

I really need an action that triggers an event when a controller is removed. (I'm going to pause the game and enable some gui elements that explain a controller was removed.)

I'm using InControl to do my input/control system and the manual says that you can subscribe to notifications when controllers are attached/detached with the following code:

Code: [Select]
InputManager.OnDeviceAttached += inputDevice => Debug.Log( "Attached: " + inputDevice.Name );
InputManager.OnDeviceDetached += inputDevice => Debug.Log( "Detached: " + inputDevice.Name );
InputManager.OnActiveDeviceChanged += inputDevice => Debug.Log( "Switched: " + inputDevice.Name );

But if anybody can help create a custom action that sends a playmaker event whenever a controller is detached, that would be amazing!

If anybody can help with this, I'd happily add your name to the credits of my game as a "Unity Senpai".



Thank you!
Oliver Age 24 - Horns of Fury

OliverAge24

  • Playmaker Newbie
  • *
  • Posts: 8
Re: InControl Player Disconnecting Action
« Reply #1 on: August 09, 2015, 07:52:27 AM »
OMG I actually managed to figure it out, here's the action in case anybody else wants to use it!

If you can add it to the wiki or ecosystem, please do, it's super useful!

Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2013. All rights reserved.

using System;
using UnityEngine;
using InControl;
using System.Collections.Generic;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Input)]
[Tooltip("Send an event when controller is removed.")]
public class InControlControllerRemoved : FsmStateAction
{
public FsmEvent sendEvent;
public override void Reset()
{
sendEvent = null;
}

public override void Awake()
{
InputManager.OnDeviceDetached += OnDeviceDetached;
}


void OnDeviceDetached( InputDevice inputDevice )
{
Fsm.Event(sendEvent);
}


}
}

autumnboy

  • Junior Playmaker
  • **
  • Posts: 73
Re: InControl Player Disconnecting Action
« Reply #2 on: June 22, 2019, 02:49:46 AM »
How could I get this action/code to get the device disconnected as an int value?

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: InControl Player Disconnecting Action
« Reply #3 on: June 24, 2019, 03:44:38 AM »
Hi.
You could try like this :

Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2013. All rights reserved.

using System;
using UnityEngine;
using InControl;
using System.Collections.Generic;

namespace HutongGames.PlayMaker.Actions
{
    [ActionCategory(ActionCategory.Input)]
    [Tooltip("Send an event when controller is removed.")]
    public class InControlControllerRemoved : FsmStateAction
    {
        [UIHint(UIHint.Variable)]
        public FsmInt deviceInt;

        public FsmEvent sendEvent;
        public override void Reset()
        {
            sendEvent = null;
        }

        public override void OnEnter()
        {
            InputManager.OnDeviceDetached += OnDeviceDetached;
        }


        void OnDeviceDetached(InputDevice inputDevice)
        {
            deviceInt.Value = (int)inputDevice;
            InputManager.OnDeviceDetached -= OnDeviceDetached;
            Fsm.Event(sendEvent);
        }
    }
}
i can't test as i do not own InControl.
You might get error on line 31
Code: [Select]
deviceInt.Value = (int)inputDevice;
its possible you need to convert to int32 or something.