Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Save warning for html5 exports — Gideros Forum

Save warning for html5 exports

SinisterSoftSinisterSoft Maintainer
edited August 2021 in Code snippets
Sometimes you may not want the user to leave the game without giving a warning that they will lose information, here is code that will allow you to do this on html5 exports:
if JS then
	JS.eval([[
	window.saveWarning=false;
	window.saveWarningListener = (e) => {if (window.saveWarning==true) {e.preventDefault(); e.returnValue='';}};
	window.addEventListener('beforeunload',saveWarningListener);
	]])
end
 
function saveWarning(f)
	if JS then
		if f then
			JS.eval("window.saveWarning=true;")
		else
			JS.eval("window.saveWarning=false;")
		end
	end
end




On some browsers there may need to be some interaction with the game/app before it will work.

Likes: MoKaLux, E1e5en

Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
https://deluxepixel.com
Tagged:
+1 -1 (+2 / -0 )Share on Facebook

Comments

  • E1e5enE1e5en Member
    edited September 2021
    I try to repeat the given solution, but nothing happens to me.
    I want to keep track of the visibilitychange event that fires when switching tabs.
    This code works:
    JS.eval([[
    		document.addEventListener('visibilitychange', function() {
    			if (document.visibilityState == 'hidden') { 
    				alert('test');
    			};
    		});
    	]])
    But I do not know how to execute the code written in Lua or set a sign (for example, I want to pause the game). In your example, as I understand it, this is possible.
    Beginner game developer:https://e1e5en.itch.io, Google Play
  • MoKaLuxMoKaLux Member
    edited September 2021
    there is this thread https://forum.gideros.rocks/discussion/comment/59228/#Comment_59228
    JS.eval( localStorage.setItem('name', 'John'))
    print(JS.eval( localStorage.getItem('name'))
    You would need to somehow set a value pause to TRUE / FALSE and use it in lua. If pause they don't update your game else update.

    PS: don't worry too much about this option. If you can't make it work you will get frustrated and start hating gideros but it's not gideros fault ;) I am not sure you could do this in other engines too :)
    PS2: you can also let the user pause himself the game with a key (for example the P key)?

    Likes: E1e5en

    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
    +1 -1 (+1 / -0 )Share on Facebook
  • E1e5enE1e5en Member
    edited September 2021
    @SinisterSoft sorry, everything works. This is my mistake, I didn't understand the code, I didn't understand that saveWarning needs to be set to True for it to work. Thought it would work with the defaults.

    @MoKaLux thanks for the link to the post. I searched the forum but missed it.
    The documentation does not indicate that the JS.eval() function can return values. So I figured out how to return a value from JavaScript to Lua. This example helped me figure it out, thanks.
    As a result, you can do this:
    -- Set a variable and listener
    JS.eval([[
    		document.isPause = false;
    		document.addEventListener('visibilitychange', function() {
    			if (document.visibilityState == 'hidden') { 
    				document.isPause = true
    			};
    		});
    	]])
     
    -- Get the value of a variable
    print(JS.eval("document.isPause"))
    Thus, by tracking the value of the variable, you can pause the game.

    P.S.
    MoKaLux said:

    PS: don't worry too much about this option. If you can't make it work you will get frustrated and start hating gideros but it's not gideros fault ;) I am not sure you could do this in other engines too :)
    PS2: you can also let the user pause himself the game with a key (for example the P key)?

    @MoKaLux you have a very categorical thought. If I can't get what works for others, it means that the problem is with me, not with the engine. :)
    This option is available in other engines, and will appear in the Gideros engine in future releases. About it wrote @hgy29 in community chat, we did discuss this issue. Most likely it will be available through the Event.APPLICATION_SUSPEND event, Event.APPLICATION_RESUME. ;)


    Update:
    It's funny, but yesterday the documentation for the JS.eval() function was different, now there is more information. @MoKaLux thanks! :)
    Beginner game developer:https://e1e5en.itch.io, Google Play
    +1 -1 (+2 / -0 )Share on Facebook
  • SinisterSoftSinisterSoft Maintainer
    edited September 2021
    Shouldn't it be:
    -- Set a variable and listener
    JS.eval([[
    		document.isPause = false;
    		document.addEventListener('visibilitychange', function() {
    			if (document.visibilityState == 'hidden') 
    				document.isPause = true
    			else
    				document.isPause = false;
    		});
    	]])
     
    -- Get the value of a variable
    print(JS.eval("document.isPause"))
    or similar? (in case the user unhides it)

    Likes: MoKaLux, E1e5en

    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
    +1 -1 (+2 / -0 )Share on Facebook
  • E1e5enE1e5en Member
    edited September 2021
    Of course, but usually in dynamic games, the player himself has to turn off the pause. That's why I wrote it like that.
    And if you control the playback of music, etc., then of course you can immediately return to its original state.

    Update:
    It all depends on the task. I think further each developer will draw conclusions for himself and modify it as necessary. :)
    Beginner game developer:https://e1e5en.itch.io, Google Play
Sign In or Register to comment.