OnApplicationQuit

Hi folks

I’ve got a little bitty bit of a problem.
I’m having a play around with web player type stuff and was trying to update my webserver DB when the user quits the browser or moves away.

according to the manual and from everything else I can gleane so far the following should work

function OnApplicationQuit(){
	if(OkToUpdate)getTest();
}

function getTest(){
	myPos=avatar.transform.position;
	var www : WWW = new WWW (myHome+"SetFValues.php?uid="+uid+"&px="+myPos.x+"&py="+myPos.y+"&pz="+myPos.z);
	yield www;
	var myp: String =www.data;
	
}

but it doesn’t
however if I move the line of code from OnAppquit to say FixedUpdate or update then it does properly update the database (ie call my php page) accordingly.
so it’s just that its not doing it from on app quit

any ideas ppl?

1 Like

I think the issue might be have to do something with starting a coroutine (your yield), when the application’s process is ending.

mmmm would that prevent the url call from being dispatched to my server.

is there another way of achieving this in some way so that I can be sure that the my php page is called before the app closes?

or do I just not exit this function until I’m happy that the Call has been made
(maybe carefully using a while loop or something)

OnApplicationQuit is run right before the application quits, so the yield is allowing it to complete the quit operation.

What you want to do is to update the database in whatever functions attempt to quit. Basically, it would call the update function first, and then call Application.Quit() afterwards.

Yep I know that Update FixedUpdate will run before Appquit, but the problem is I only want to call my php page at the last opportunity rather than multiple times each second all the way during gameplay.
its only for storing the player data when the user quits the game being inside of a browser there’s no control over how the user exits the app.
And so it looks to me that the only way provided is to use app quit.
also interestingly this does actually work fine when inside the editor.

This must be a common enough requirement for ppl to want to code, and so I’m sure theres a simple way of implementing it.

GargerathSunman explained pretty much what you do:

store the value, then quit.

This can be handled through a single quit request function, but you can just as well use a global value that tells you if the value is stored in the app quit. if not, then it will use cancelquit and coroutine to store the values and set the value to then quit again. as the value is now set the quit will go through and quit.

Ok so I’ve given this a shot and it’s still not working.
Maybe I should work on doing something in te hosting HTML page to prevent the unity web client closing so immediately when the player moves away from the page or something.

Ok so far the only way I can get it to work is by sticking a js event in the unload event if the HTML body where my unity web app is hosted and in that function I call a function inside my unity projects browser interface script which I’m using to handle interactions.
This seems like a cludge given that the app quit event is supposed to be called in the web mode as much as normal.

There must be a more elegant way of doing this surely?

mmm,
OK so my approach of calling a unity3d function from a javascript function in the html page hosting the unity webapp almost works
if I do a refresh of the page then indeed it triggers the update to the database via my unity function calling a php page.
however if I just close the window down it does nothing.
(in firefox neways)
(I’ve tried onunload onbeforeunload)

So am I the only person to try to save a players position from a web app before a window gets closed?

plus, what if they jerk the power or ethernet out of the wall or something?

Maybe you could use the webcam, and detect facial expressions of dissatisfaction, or sudden shutdown movements. Or how about checking for negative brainwaves!

:wink:

But seriously, maybe a reasonable InvokeRepeating interval that does the postback might be useful for this. No need to run it in Update.

add a logout functionality if you want the player to cleanly shut down or store the data continously.

the shutdown behaviour is up to the browser and if the browser just kills all stuff without granting time to store as it is, then there is nothing you can do about it other than accepting it.

also for the orig code: you would use cancelquit to stop the quit, do the coroutine and then set a global which is beeing checked in the quit function before the cancelquit is init. at least that is what I would to to prevent the immediate quit

Well I get the humour there, however I didn’t thinknmy qustion was that unrealistic.
I did try the cancelquit route as indicated in the docs ( I think) but that still didn’t do the job.
I’m happy to add a logout button to provide a clean shut down.
I just wanted to accomodateva ’ filthy’ shutdown as it were.
So at the end of the day I guess I can’t accomodate every scenario.

Thanks for the various suggestions.

It’s not an unreasonable question at all, I’ve had it come up before (the source of my silly comments). I applaud your “I can make it work” attitude, it’s the mother’s milk of this kind of work, it’s just kinda funny where it leads sometimes. :slight_smile:

As you’ve concluded, a lot of users are just going to kill the window or tab in a webplayer no matter what you do, and you can’t postback every frame. But if you do it every 5 or 10 seconds (or whatever interval works), and maybe also when certain events happen, that’s pretty close, and minimizes the overhead.

Well I just wanted to try trap it as much as I could. Yep granted you can’t check for a nwcable getting yanked, or a fuse going etc.

My main gripe is that (and really its not a gripe coz I’ve seen had to work with a lot worse) where the documentation implies it should work in the web player,

There are in fact situations where it doesn’t (browser window and thus the web view getting closed)

On reflection further investigation I’ve found that although my onunload event doesn’t cause the unity web app to quit graciously and so trigger OnApplicationQuit, my onunload event function in the hosting html page’s javascript does execute even after it appears that the browser window has closed.
ie in my same JS function which is called by onUnload, I stick a JS alert(‘test’) in there after it attempts to call the unity function.

So I have 1 more avenue to explore, that is to every 5 seconds or so from within Unity I perform an ExternalEval call to set my x,y,z 2 or 3 other important must have values. but only set these as JS variables for the HTML page to reference.

Then my html pages onUnload event calls an ajax style get to submit these values to a php page on the server.
This way it should minimise the latency which may be incured by having my unity app making a call to the server every 5 or so seconds.

Needless to say this isn’t for a conventional multiplay game where real time xyz etc data needs to be replicated to lots of other clients. just stored to provide continuity for the user experience.

this is fun