IAP in Godot Engine


Posted: 2022-01-08 12:22:51 |    Posted by vkrishna |    sites.google.com/site/vkgamedev

1
Login to like

IAP in Godot Engine

The platform provides an easy way to enable IAP for the games you launch here, but you still need to do some work in the game to

  • Check what item(s) that user has bought
  • Open the Shop UI from Game. 

I'll be covering the details of how you can do this if you are building a game for this platform using Godot engine. 

I assume that you know the following

  • Setup a scene in Godot
  • Write code in GDScript
  • Exporting the game as HTML5

Check Item Status:

If you didn't know, Godot has a node for pretty much everything.. and yes there is one for making an HTTP Request this is going to be very handy for our purpose of checking if an item is bought by the user or not.

The above screenshot shows an example scene setup with HTTPRequest node added. 

Once you have setup a scene with HTTPRequest node, you can add a script to the root of the scene and add the following code. 

func _ready()
	var headers = ["auth_token: <Auth Token>"]
	#https://play.idevgames.co.uk

	#item check url for store item. 
	var url = "https://play.idevgames.co.uk/request/check/xxxxxxxxxx"
	var http_request = $HTTPRequest #add HTTPRequest node to your scene.
	http_request.connect("request_completed", self, "_on_request_completed")
	http_request.request(url, headers)

func _on_request_completed(result, response_code, headers, body):
	if body.get_string_from_utf8() == "true":
		print("Purchased")
	else:
        print("Not purchased")

We are basically making an http request and parsing the response to check if its' true or not. If the response is true it means that the user has bought the item and you can enable it to be used in-game. 

Opening the Shop UI:

When you enable IAP for you game, the platform automatically adds a "GameShop" button as an overlay for the game (check screenshot below)

When you click on that button you will be able see something like this

This is pretty handy and you really dont' have to do much beyond setting up the items and checking if the user owns the item for the integration to be complete. But you might want to introduce flow which will surface the IAP from your game for that you will have to do some more work. This shop UI can be opened by just calling a javascript function 

window.parent.openShop()



Godot 3.4 and above have streamlined the JavaScript API interaction from GDScript and the following snippet show how to use it.

func _on_UnlockAll_pressed():
	JavaScript.eval("window.parent.openShop()")

So that's how you integrate IAP from idevgames platform into your Godot game, hope this was helpful to you. 

Would you like to comment on this post? Create an account in less then 1 minute Click here



Back to top

Bump Reminder ChiefBuddy DISBOARD Dyno GameAddict gamerbread
12 Online Discord
·