Page 1 of 7

Game (unity3d) with phidgets hangs on close

Posted: Sun Oct 24, 2010 12:37 pm
by seriousmedia
hello,
Im programming a game in unity3d that uses 3 steeringwheels connected to a ik888. It works great, but when i try to close the game it hangs.
I have, of course, tried without the phidgets code and then it closes fine.
I use the phidgets21.net.dll in c#.
I attach the ik888 and connect to a dictionary (and start a key listener).
Everything gets closed before the game actually quits, but still it hangs.

Can anyone help me with this?

Ramon

Re: Game (unity3d) with phidgets hangs on close

Posted: Mon Oct 25, 2010 8:39 am
by erik
After you close the phidgets, try inserting

Code: Select all

Application.DoEvents();
There might be event messages still in queue for the phidgets even after you've closed them.

Re: Game (unity3d) with phidgets hangs on close

Posted: Mon Oct 25, 2010 9:20 am
by seriousmedia
Thanks,

but there is no Application.DoEvents() in unity3d.
But i wait (using Invoke see:http://unity3d.com/support/documentatio ... nvoke.html) a second before quitting the application.

I don't know if this should have the same effect as the DoEvents in .NET.

Ramon

Re: Game (unity3d) with phidgets hangs on close

Posted: Tue Oct 26, 2010 2:31 am
by seriousmedia
I've tried a different approach (Coroutines with yield to simulate DoEvents in unity3d). But that still doesn't solve the problem.

The game still hangs while closing the phidgets.
I've attached errorEventHandlers to all the objects, but they don't fire an Error event.
I've also attached a ServerDisconnect event handler. But this one is never fired.
Is this supposed to fire on close?

greetings,

Ramon

Re: Game (unity3d) with phidgets hangs on close

Posted: Tue Oct 26, 2010 9:55 am
by Patrick
Do you remove all event handlers before calling close?

-Patrick

Re: Game (unity3d) with phidgets hangs on close

Posted: Tue Oct 26, 2010 1:14 pm
by seriousmedia
Yes,
All event handlers are removed before closing.
Is there any way to test/debug this?

Ramon

Re: Game (unity3d) with phidgets hangs on close

Posted: Thu Oct 28, 2010 2:30 am
by seriousmedia
Anyone??

Re: Game (unity3d) with phidgets hangs on close

Posted: Thu Oct 28, 2010 9:49 am
by Patrick
If you post your code, I can probably debug it. It's hard to say what could be going on without the code.

-Patrick

Re: Game (unity3d) with phidgets hangs on close

Posted: Tue Nov 02, 2010 10:04 am
by seriousmedia
I've created a simple test using the phidget in unity3d with the phidget21.net.dll and it works fine. Closing the test-app is no problem.
I must be doing something weird in my game.
I'll look into the code.
Thanks for the help!

greetings,

Ramon

Re: Game (unity3d) with phidgets hangs on close

Posted: Thu Nov 04, 2010 5:45 am
by seriousmedia
Hello again,

i'm afraid the problem is not solved yet.
I can close the game in the IDE about 4 times before Unity3D crashes.
When i publish the game i cannot close it without crashing.

Here's my code
- base code -------------------

Code: Select all

using UnityEngine; 
using System.Collections; 
using System; 


public class testscript : MonoBehaviour
{

   private Phidget_script phid;
   public bool okToQuit = false;
	
	void Awake ()
   {
	phid = GetComponent<Phidget_script>();
   } 
   
   void Start()
  {
	Debug.Log("Connecting to phidgets");
	phid.connect("localhost","5001");
  }
  

 void OnApplicationQuit () 
   {
	 Debug.Log("OnApplicationQuit() called."); 
	 if (okToQuit==false) { 
		Debug.Log("Cancel quitting");
		Application.CancelQuit(); 
		phid.close();
	} 
   }
   
   public void complete_close() 
   {
		Debug.Log("testscript Complete Close");
	   okToQuit=true;
	   Application.Quit();
   }
 
}
- phidget script -----------

Code: Select all

using System;
using System.Collections;
using System.Text;


using Phidgets;
using Phidgets.Events;  

using UnityEngine;

    public class Phidget_script: MonoBehaviour
    {
        InterfaceKit ifKit;
		
		private string phidget_adres;
		private int phidget_port;
		
        public void connect(string phidgetadres,string phidgetport)
        {
			Debug.Log("interfaceKit Program connect : "+ phidgetadres + " / " + phidgetport);
			phidget_adres = phidgetadres;
			phidget_port = Convert.ToInt32(phidgetport);

            try
            {
				Debug.Log("Initialize the InterfaceKit object");
				ifKit = new InterfaceKit();

				ifKit.Attach += new AttachEventHandler(ifKit_Attach);
				ifKit.open(phidget_adres,phidget_port);
            }
            catch (PhidgetException ex)
            {
                Debug.Log("InterfaceKit Program connect exception: "+ex.Description);
            }
			
        }

        void ifKit_Attach(object sender, AttachEventArgs e)
        {
            Debug.Log("InterfaceKit attached!"+e.Device.SerialNumber.ToString());
        }
		
		
		
		public void close() {
			StartCoroutine(closeEnum());
		}
		
		
   private IEnumerator closeEnum () {     

		yield return StartCoroutine(WaitFunction (0.5f));        
		close_ifkit1();
		yield return StartCoroutine(WaitFunction (1f));        
		close_ifkit2();
		yield return StartCoroutine(WaitFunction (1f));        
		SendMessage("complete_close");
	
	}

		private void close_ifkit1() {
				
			Debug.Log("close ifkit1");
			if (ifKit!=null) {
				ifKit.Attach -= new AttachEventHandler(ifKit_Attach);
			}

		}
		
		private void close_ifkit2() {
	
			if (ifKit!=null) {
				Debug.Log("close ifkit2");
				ifKit.close();
				Debug.Log("after ifKit.close");
				ifKit = null;
			}

		}

    IEnumerator WaitFunction (float delay) {       
		float timer = Time.time + delay;        
		while (Time.time < timer) {            
			yield return null;        
			}   
	}
}
i'm i doing something wrong?