Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / [Guide] Calling JavaScript Functions via AGK (HTML5 Export)

Author
Message
xCept
21
Years of Service
User Offline
Joined: 15th Dec 2002
Location:
Posted: 30th Dec 2017 02:48 Edited at: 30th Dec 2017 02:53
By request, here is a quick way to call external JavaScript functions and pass AppGameKit data to them, after exporting your app as HTML5. This is useful when you need to modify standard HTML elements with data retrieved from AppGameKit, or use advanced JavaScript functionality with custom data delivered straight from AGK.

For instance, you can update a standard HTML heading tag to reflect which level the user is currently on or build a more sophisticated UI using standard HTML to surround your AppGameKit app. No HTTP requests or server-side coding is needed for this and it works in Tier 1.

Ideally this will be offered as a native feature to AppGameKit in a future version, as well as the reverse to easily send JavaScript data back to AGK. For now the method I describe here will suffice. This guide only covers sending data from AppGameKit to JavaScript. There are several approaches to doing the opposite but it is generally easiest to use async HTTP calls for that purpose and that's beyond the scope of this thread.

Summary: We will modify the Message() command so that we can optionally use it to call a custom JavaScript function and pass JSON-formatted AppGameKit data to it for further parsing in JavaScript. The command will still work in its usual way if you are simply passing a string message to it.

Part 1 - AppGameKit Source Code

1.) In your AppGameKit source code, construct a UDT for storing the data that you wish to eventually pass through to JavaScript. The only requirement is that it include a "func_name" string property, which is used to tell the browser which specific JavaScript function you wish to call. You can add as many other properties as you wish for organizing the data you wish to send. The passed data can be strings, integers or floats. For compatibility with how AppGameKit converts the UDTs to JSON and how it is read and referenced in JavaScript, use all small letters for the property names.


Above, I have defined a simple UDT that can store up to five strings, integers and floats along with the desired JavaScript function name.

2.) When you want to call a JavaScript function from within your code, set the properties of the previously defined UDT as desired:


In this example, the target JavaScript function to be executed is named "UpdateHeader" and it will send the AGK-derived username from AppGameKit to JavaScript/HTML. It also depends on a CallJavascript(data) function as defined below, which stringifies the UDT and passes it along with a special tag to alert the JavaScript that it is requesting to execute an outside JavaScript function.


The full AppGameKit source code for this example is below. It includes two simple JavaScript call tests. One test will update an HTML header with the AGK-entered username, and another will update the title bar (e.g., tab bar text) to indicate how many times the user has clicked within the AppGameKit app.



Part 2 - JavaScript Source Code

Now that the AppGameKit code is done, we can export it as HTML5.

1.) Select File > Export Project > As HTML5. Enter an Output Folder that you can easily access and transfer to a Web server and then click Export. In my case, I output it directly to a local server path (e.g., XAMPP) so that it is immediately accessible from my browser without needing to copy/paste or upload it elsewhere. You cannot open the generated HTML files directly from My Computer / Finder or you'll encounter security and script errors, it must be accessed from a local or remote server from your browser.

2.) Make a copy of the default generated .html file and rename the copy to index.html. This will allow you to access the app without needing to specify the filename on the server and, more importantly, will prevent AppGameKit from overwriting it each time you export the AppGameKit project.

3.) Create a blank file in the same HTML5 output directory named scripts.js. We will use this file to add our custom JavaScript code.

4.) Open the index.html file in a text editor. Right above the closing </head> tag, add a reference to the scripts file by including the line below:



5.) Now as an example, let's add a header to the HTML page so that it can be modified from AGK. Insert the following code directly below the <body> tag. Then save the file.


6.) Open up the scripts.js file in a text editor. Then add the following functions:


The first function is the important one, and the other two are for the two tests in our AppGameKit project. The top function overrides (proxy prototype) the default behavior of the JavaScript alert function. It will check for the special |JS| attribute and, if found, will parse the data and pass it to the requested JavaScript function. Otherwise it is treated as a normal alert box.

It is important that the names of the other functions exactly match the func_name data in AGK. If everything has been done correctly, you should now be able to update your name in AppGameKit and it will be reflected in the standard HTML header. You can also click in blank areas in the AppGameKit web view and the title bar will be updated to reflect the total.

A working example of this project is online at: http://mjpsites.com/agk/CallJavascript/

The full project source is attached to this post.

Attachments

Login to view attachments
Ortu
DBPro Master
17
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 30th Dec 2017 22:29
Very cool man.

What i would really like to see is an html5/js layer within an ios/android/pc export.

Think CEF or ionic/cordova but with AGKs graphics engine and ease of setup and use.
http://games.joshkirklin.com/sulium

A single player RPG featuring a branching, player driven storyline of meaningful choices and multiple endings alongside challenging active combat and intelligent AI.
MikeMax
AGK Academic Backer
12
Years of Service
User Offline
Joined: 13th Dec 2011
Location: Paris
Posted: 2nd Jan 2018 01:41
Good job xCept very well workaround of the message/alert function
Mobiius
Valued Member
21
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 2nd Jan 2018 09:45

Login to post a reply

Server time is: 2024-11-24 04:12:06
Your offset time is: 2024-11-24 04:12:06