Proof of Concept: Call APIs from GB
Proof of concept for calling APIs from GB Studio.
It's pretty messy but works well enough for simple use cases.
Note: Use this concept sparingly, if you're not careful your game may include functionality that is visibly broken when played on real hardware or other emulators.
Instructions
1. Download the latest dev build from GitHub links at https://github.com/chrismaltby/gb-studio (is currently required as this version makes sure the first 100 variables are always in a deterministic memory location).
2. When you want to call some Javascript in your game add an event "Variable: Set to 'True'" and choose one of the first 100 variables (important!) that you haven't used anywhere previously.
2. Using the dev version GB Studio, open and export your project as a ROM file.
3. Open the file `game.map` in a text editor and look for the line containing "_script_variables"
e.g.
_script_variables C545
Where C545 is the location in memory (as a hexadecimal value) of your game's variables. The exact value in your game will probably be different, and if you modify your game by adding/removing content it is likely to change, just make a note of the value for now.
4. Without changing any content rebuild your game but this time for web
5. Edit the generated index.html to add something like the following to the bottom of the page just before "</body>"
<script> setInterval(function(){ var variablesPtr = 0xC545; // Make sure to change this to your _script_variables location (note the 0x at the front too) var variableIndex = 21; // The variable that is set to true when you want to call the API (using $21$ here) if(gameboy && gameboy.memory[variablesPtr + variableIndex]) { // API call goes here alert("Do something..."); // Reset the variable back to zero once done to stop repeated calls gameboy.memory[variablesPtr + variableIndex] = 0; } }, 10); </script>
6. Run your web build in a browser, when you set the variable you're looking at to true your API should be called.
Status | Released |
Category | Other |
Platforms | HTML5 |
Rating | Rated 5.0 out of 5 stars (3 total ratings) |
Author | Chris Maltby |
Comments
Log in with itch.io to leave a comment.
For anyone who’s looking to do this with current versions of GB Studio, I just posted a demo on my Itch along with a writeup of exactly where to find the variables.
Very nice!
Thank you for taking time to help community with API Calls via GB Studio on web. I have been googling this, i'm thinking to save game state on server for players that play on the web. Also I have seen your Depths of Europa (love the name, by the way!) and also saw your Kickstarter. I think you should try again, it is destined to be a success. There is also a way to create your own crowdfunding page where the 30 day countdown does not creep on you. You already have a following and people writing to you that they would be willing to back your project again so that might be also a possibility. Good luck on your journey and feel free to respond if you have any questions or just want to say hi ᵔ ᵕ ᵔ
Hi there! I have figured out that the latest version which works exactly this way is GB Studio version 2.0.0 beta 4. It would be nice if it was still available on Itch.io or GitHub. It also works similarly with 2.0.0 beta 5, but instead of "_script_variables" you should look for "_script_v" in the `game.map` file. For later versions it is more complicated...
Hithere,
Is this possible in GBStudio 3.
I testes everything, but I can not find the _script_variables anymore.
Greetings,
Simerl
This is honestly amazing work. Do you know if this will be supported in version 3.0?
This has tremendous potential!!! ... I imagine continuing the game 'outside' the game.
Thanks for sharing!
Great feature ! I will probably use it for the game that I develop for a birth announcement (coming soon) =D!