Implement Gamedistribution ads using webview

Hi, I am Maxi from QkyGames Studio. We are just starting using defold and we are already making some games, but we need to use this API to show ads in our games, please check it here: http://www.gamedistribution.com/Api/1

Is it possible use this API to show ads in html5 games? For mobile games we use Appnext and for that I’ve already found a library here in the forums.

Sorry for my english.

2 Likes

Are these for banner style ads?

Webview for banners isn’t available yet but it’s planned. You can only use fullscreen webview right now.

3 Likes

they have fullscreen ads for games, video or image.

I try make this in my game:

function webview_callback(self, webview_id, request_id, type, data)
    if type == webview.CALLBACK_RESULT_URL_OK then
        -- the page is now loaded, let's show it
        webview.set_visible(webview_id, 1)
    elseif type == webview.CALLBACK_RESULT_URL_ERROR then
        print("Failed to load url: " .. data["url"])
        print("Error: " .. data["error"])
    elseif type == webview.CALLBACK_RESULT_EVAL_OK then
        print("Eval ok. Result: " .. data['result'])
    elseif type == webview.CALLBACK_RESULT_EVAL_ERROR then
        print("Eval not ok. Request # " .. request_id)
    end
end

function init(self)
	local webview_id = webview.create(webview_callback)
end

but I get this error: main/gd_ads.script:16: attempt to index global 'webview' (a nil value) stack traceback:

You must test webview on iOS/Android only. Put conditionals in to disable on other platforms.

https://github.com/britzl/defads_example

so, I can not use webview for html5 games?

Not at this time, but you can edit the html file and add ads that way.

what html file need edit? the “index.html” exported file?

Yes, you could modify it to put banner ads in, or full screen ads which periodically show up.

And i can call to the api from defold? or get callback from pausegame or resumegame function in the index?

There is no way to directly communicate between your game and scripts running on the page yet unfortunately… you could communicate with a server but that’s more messy. I wish there was some way to send messages back and forth between the game and a script on the page.

I am not sure how this works on HTML5 builds - if it checks the canvas focus or window focus - but you could try it for detecting if game is out of focus http://www.defold.com/ref/window/

You could make the game fullscreen, and then have a permanent rotating banner ad anchored at the bottom middle above the canvas.

https://www.bookofdefold.com/examples/FullWindowCanvas/

https://codepen.io/tag/fixed%20footer/

I have a strange error with the edited index: http://html5.GameDistribution.com/fef023cc25cd4e01a32e921059d93252/

I can see test ads in the gamedistribution page, but the game crash and not start.

Does not appear to be uploaded properly

http://html5.gamedistribution.com/fef023cc25cd4e01a32e921059d93252/archive/game.projectc0 404

to upload the game I need make this:

Notice: For HTML5 games, file must be ZIPped file and file extention name ends with “.html5”. Ex: gamename.html5 … Index file name is “index.html”

so, I export my game to html5 and enable “release mode” compress the folde and rename.

Are you zipping all of the files together and not just the index on its own? You should contact them to figure out why the game is not uploading properly.

They fix the problem now, so, I can pass value from Javascript into Game?

Just to be clear: Do you want to pass data to and from a running Defold HTML5 game or only pass data to the game when it is starting?

  • You can not send data to/from a running HTML5 game using Javascript (I suppose you could send data using a local websocket though)
  • You can send additional data when launching the game (like a user id, session token or something else)

Not sure if this is what you’re after, but there is webview.eval() which allows you to communicate back and forth with the running page.

Webview does not work on HTML5 builds. It would be great if it did.

1 Like

Hi,

so, I can add a setInterval() function in my index to check every time if my localstorage is changed, so i can check if ads = 1 call function showad() and set ads = 0 in localstorage, the question is, how can create a localstorage in defold to read and set from my index.html (javascript), the game is HTML5.

sorry for my english.
Maxi

Great idea! I had not thought of that.

Have your Defold game and Javascript constantly check a file for any communication. You could set up a method of message passing between the game and the local Javascript files with this method. Each side can have their own inbox which they can clear, and put messages into the other’s inbox. Or share a file. I’m not sure what would be ideal in this case.

From there, you can then do any logic you need.

You can use Lua io save/load functions.

Defold uses IndexedDB API for dealing with a kind of filesystem on HTML5 builds. It uses the “/data” database version 21. If you used sys.get_save_file(“html5_inbox”, “html5_inbox_state”) it would be the text of a path “/data/.html5_inbox/html5_inbox_state” so in JS you would connect to the “/data” db and then access the “/data/.html5_inbox/html5_inbox_state” key to be able to load that file.

You could save / load a simple file type, or encode/decode from something like JSON. Here’s a JSON encoder/decoder as Defold’s build in one only decodes.

Here’s an indexedDB example although as the comments there say it may be a better idea to use a pre-made solution and not work with the indexedDB api directly. I have not experimented with all of these things together yet so I can’t say for sure how to make this all work for sure but if you time give it a try.

I want to make an example but I do not have the spare time right now. This should be possible to do though. Again, great idea! It will probably be a workable html5 game to browser JS communication problem solution.