Button Event Persisting Across Scenes

EricJ13EricJ13 Posts: 24Member
I'm having a problem where Ouya controller button events are persisting across scenes. I use the button code found in https://devs.ouya.tv/developers/docs/unity:

public class OuyaShowUnityInput
{
    void Update()
    {
        if (OuyaExampleCommon.GetButton(OuyaSDK.KeyEnum.BUTTON_L3, OuyaExampleCommon.Player))
        {
            //button is pressed
        }
        else
        {
            //button is not pressed
        }
    }
}

to call Application.LoadLevel in a scene.

But if that loaded scene contains a similar if statement in Update it fires in the new scene too even if the button isn't pressed. I'm pressing and releasing the button as quickly as possible, I don't think its a "GetButton" vice "GetButtonDown" thing but I don't know. This doesn't happen in the Unity IDE, only on the Ouya device. In my vanilla project I have an init scene, a main scene and a second scene. Init initializes OuyaGameObject and loads main. Main waits for the controller "O" button to be pressed and loads second scene. Second scene has a bool member "oPressed" that is set to true if the controller "O" button is pressed, GUI.Label shows state of "oPressed". In Unity IDE oPressed stays at false, in Ouya device it is set to true. Am I the only one having this problem? A forum and Google search turned up nothing. Any help would be greatly appreciated.

Code from the project:

The Init Scene - 
using UnityEngine;
using System.Collections;

public class ControllerTestInit : MonoBehaviour
{
void Update ()
    {
        Application.LoadLevel("ControllerTestMainMenu");
}


    void OnGUI()
    {
        GUI.Label(new Rect(128, 128, 128, 32), "Initializing...");
    }
}

The Main Scene - 
using UnityEngine;
using System.Collections;

public class ControllerTestMainMenu : MonoBehaviour
{
    void Update()
    {
        if (OuyaExampleCommon.GetButton(OuyaSDK.KeyEnum.BUTTON_O, OuyaExampleCommon.Player))
        {
            Application.LoadLevel("ControllerTestSecondScene");
        }
}


    void FixedUpdate()
    {
        OuyaExampleCommon.UpdateJoysticks();
    }


    void OnGUI()
    {
        GUI.Label(new Rect(128, 128, 512, 64), @"Main menu - Press the ""O"" button to go to Second Scene");
    }
}

The Second Scene - 
using UnityEngine;
using System.Collections;

public class ControllerTestSecondScene : MonoBehaviour
{
    bool oPressed;

    void Start()
    {
        oPressed = false;
    }

    void Update()
    {
        if (OuyaExampleCommon.GetButton(OuyaSDK.KeyEnum.BUTTON_O, OuyaExampleCommon.Player))
        {
            oPressed = true;
        }

        if (OuyaExampleCommon.GetButton(OuyaSDK.KeyEnum.BUTTON_A, OuyaExampleCommon.Player))
        {
            Application.LoadLevel("ControllerTestMainMenu");
        }
    }

    void FixedUpdate()
    {
        OuyaExampleCommon.UpdateJoysticks();
    }

    void OnGUI()
    {
        GUI.Label(new Rect(128, 128, 512, 64), @"Second Scene - Press the ""A"" button to go to Main menu" + "\noPressed: " + oPressed.ToString());
    }
}

Comments

  • tgraupmanntgraupmann Posts: 2,869Administrator, Team OUYA
    edited July 2013
    There should be something you can call in the OnDestroy event like:

    Input.ResetInputAxes();

    Just in case you mean a button press is persisting across scene loads.
    Post edited by tgraupmann on
    ~Tim Graupmann
    OUYA Inc | Android Developer
    Skype: tgraupmann_prey

    http://github.com/ouya/docs
    http://github.com/ouya/ouya-sdk-examples

    Check out the latest docs for your game engine: [setup] [adobe air] [android] [clickteam fusion] [construct 2] [corona] [libGDX] [game maker] [html5] [marmalade] [monogame] [unity] [unreal]

    Use caution when setting [persistent wireless mode].
  • EricJ13EricJ13 Posts: 24Member
    Thanks but that's not it I'm afraid. In my full project where I first discovered the problem I have your:

        void Awake()
        {
            OuyaSDK.registerMenuButtonUpListener(this);
            OuyaSDK.registerMenuAppearingListener(this);
            OuyaSDK.registerPauseListener(this);
            OuyaSDK.registerResumeListener(this);
            Input.ResetInputAxes();
    }

        void OnDestroy()
        {
            OuyaSDK.unregisterMenuButtonUpListener(this);
            OuyaSDK.unregisterMenuAppearingListener(this);
            OuyaSDK.unregisterPauseListener(this);
            OuyaSDK.unregisterResumeListener(this);
            Input.ResetInputAxes();
        }

    from https://devs.ouya.tv/developers/docs/unity in all but the init scene. For the sake of brevity (okay laziness) I didn't put those in the stripped down test project but to be certain I added:

        void Awake()
        {
            Input.ResetInputAxes();
        }

        void OnDestroy()
        {
            Input.ResetInputAxes();
        }

    to all the scenes in my test project and the problem remains.
  • tgraupmanntgraupmann Posts: 2,869Administrator, Team OUYA
    You must've attached the script to an object that is persisting across your scenes.
    ~Tim Graupmann
    OUYA Inc | Android Developer
    Skype: tgraupmann_prey

    http://github.com/ouya/docs
    http://github.com/ouya/ouya-sdk-examples

    Check out the latest docs for your game engine: [setup] [adobe air] [android] [clickteam fusion] [construct 2] [corona] [libGDX] [game maker] [html5] [marmalade] [monogame] [unity] [unreal]

    Use caution when setting [persistent wireless mode].
  • EricJ13EricJ13 Posts: 24Member
    Yeah I was thinking about that too but the only Object in the entire project that persists is the OuyaGameObject from the init scene in either my test project or the game I'm trying to get done.
  • EricJ13EricJ13 Posts: 24Member
    I added:

        void OnLevelWasLoaded(int theScene)
        {
            Input.ResetInputAxes();
        }

    to the OuyaGameObject class thinking that since it's the only persistent object that may ensure the reset happens even though the Awake/Destroys should handle it. Still no joy.
  • EricJ13EricJ13 Posts: 24Member
    I'm chasing my tail here. This doesn't make any sense. Like I said the problem doesn't happen when the projects are run in Unity, just compiled on the Ouya device. I mean this sounds like an Android thing more than a Unity thing. I think. I dunno man. Why am I seemingly the only one this is happening to? I hate being special.
  • tgraupmanntgraupmann Posts: 2,869Administrator, Team OUYA
    Maybe your init scene is in the build scene list more than once?
    ~Tim Graupmann
    OUYA Inc | Android Developer
    Skype: tgraupmann_prey

    http://github.com/ouya/docs
    http://github.com/ouya/ouya-sdk-examples

    Check out the latest docs for your game engine: [setup] [adobe air] [android] [clickteam fusion] [construct 2] [corona] [libGDX] [game maker] [html5] [marmalade] [monogame] [unity] [unreal]

    Use caution when setting [persistent wireless mode].
  • EricJ13EricJ13 Posts: 24Member
    I had a moment of brightness when I saw that, but no, init's just there once.
  • EricJ13EricJ13 Posts: 24Member
    Okay I've been able to get the second scene to load with oPressed showing as False. I didn't change any code, I just hit the O button from the main scene ridiculously fast, pecked it with a flick of my finger in a manner which no person would interact with the controller in real life. So it's acting like Unity's Input.GetButton(buttonName) instead of Input.GetButtonDown(buttonName) but across scenes instead of just across frames. Again though the issue only manifests itself compiled on the Ouya, not in the Unity IDE. Seriously, nobody else has reported this problem?

    One good thing that's come from this is that I found the logcat stuff on the Android SKD tab in the Ouya Panel, makes it much easier to watch the events in real time than having to manually go to the command prompt.
  • tgraupmanntgraupmann Posts: 2,869Administrator, Team OUYA
    Right it's just using Input.GetButton, but you can change that. Getting more tempted to move just the mapping to axises to OuyaSDK. Even more every day.
    ~Tim Graupmann
    OUYA Inc | Android Developer
    Skype: tgraupmann_prey

    http://github.com/ouya/docs
    http://github.com/ouya/ouya-sdk-examples

    Check out the latest docs for your game engine: [setup] [adobe air] [android] [clickteam fusion] [construct 2] [corona] [libGDX] [game maker] [html5] [marmalade] [monogame] [unity] [unreal]

    Use caution when setting [persistent wireless mode].
  • EricJ13EricJ13 Posts: 24Member

    Getting more tempted to move just the mapping to axises to OuyaSDK. Even more every day.
    That would be swell.

    My first game's single player anyway, using Input.GetButtonDown("Fire1") for the O button is giving me what I need so maybe I'll just do that approach for the whole controller. 

    I added:
        public static bool GetButtonDown(int buttonNum, OuyaSDK.OuyaPlayer player)
        {
            string keyCode = GetKeyCode(buttonNum, player);
            if (string.IsNullOrEmpty(keyCode))
            {
                return false;
            }
            OuyaKeyCode key = (OuyaKeyCode)Enum.Parse(typeof(OuyaKeyCode), keyCode);
            return Input.GetKeyDown((KeyCode)(int)key);
        }

    to OuyaExampleCommon as a quick hack after I'd looked things over a bit and had a better idea what was going on. I used:
            if (OuyaExampleCommon.GetButtonDown(0, OuyaExampleCommon.Player))
            {
                Application.LoadLevel("ControllerTestSecondScene");
            }

    and that works too but taking that route seems convoluted to me.

    Code comment in OuyaInputManager says OuyaInputManager.GetButtonDown wraps Unity's method but calling OuyaInputManager.GetButtonDown("O") didn't return true and to be honest I didn't feel like digging through anymore code that I'm not familiar with so I abandoned that route. 

    It shouldn't be this difficult IMHO. I dunno, I've been at it too long today and my mood is sour. Tomorrow's a new day, I'm looking forward to making more progress then.
Sign In or Register to comment.