Implement Gamedistribution ads using webview

It will not be right away as I’m reading some of the emscripten source code to better understand the way they are doing things and to match it well. But I will try to post something working before I stop.

sound good! if all work, we can monetize html5 defold games with ads or implements java api from sponsors.

Yep, and freely communicate between HTML pages and the Defold game for any other purpose as well!

Here is a working test! Look to the bottom below “// this is a proof, do not use as is”

Set this html file in your Defold project and run it. In the console you should see something like this. It has set data, got it, and its the same kind of data that Defold is expecting when it saves/loads files in the virtual filesystem.

The next step is to make a script which polls the data every so often and does whatever you want depending on the contents of the data. For your use case, you only need the HTML script to be able to set data based on what your ads are doing, and then within your Defold game you would periodically reload that file and check its contents to be 1 or 0. You don’t need JSON either with such simple data. Use the Lua io functions I posted above. Try to get this working in your own project how you want and ask questions if you get stuck.

I’m going to clean it up more and make it a general purpose way of passing JSON (or raw) messages between Defold HTML5 game and its HTML container page… although I’m really pressed for time right now so this will have to be put on todo, maybe early next month.

Great news Pkeod, so I need save/load in defold with this line?

local inbox_path = sys.get_save_file("html5_inbox", inbox_file)

inbox_file can have any name true?
and how set values in file?

local file = io.open(inbox_path, "w")
file:write(encoded_inbox)
file:close()

for example: set ads = 1?

other question: I need put an setInterval() in html file to load every x time the db? or I can do it in defold?

You’ll need to create a function in a Defold script to check the file as often as you want to.

local path = sys.get_save_file("html5_inbox", "data.file")

...
-- set content of a file on the filesystem
local html5_data = "some data"

function write_file(file, data)
    local file = io.open(path, "w")
    file:write(html5_data)
    file:close()
end

...
-- get content of a file on the filesystem
local html5_data = read_file(path)
-- then do something if it's 1 or 0?

function read_file(file)
    local f = io.open(path, "r")
    local content = f:read("*all")
    f:close()
    return content
end

Take care for the details you will need to customize for whatever naming of the files etc. Full path needs to match in the HTML JS and in Defold. Read previous posts again for a reminder if you need help with that.

For the polling timer in Defold see these posts

I get this error now, java no is for me:

dmloader.js:299 ERROR:CRASH: Failed to write Crashdump file.printErr @ dmloader.js:299put_char @ My project.js:1write @ My project.js:1write @ My project.js:1doWritev @ My project.js:1___syscall146 @ My project.js:1iza @ My project.js:28Zya @ My project.js:28_ya @ My project.js:28Cba @ My project.js:8Pta @ My project.js:5Ota @ My project.js:5ccallFunc @ My project.js:1window.onerror @ dmloader.js:622
dmloader.js:301 Exception thrown, see JavaScript console
My project.js:1 Uncaught NotFoundError: Failed to execute ‘index’ on ‘IDBObjectStore’: The specified index was not found.

I get this error when remove cache.

i will check the new info.

The problem is attempting to load a file index which doesn’t exist yet. Disable the custom html file for now. Get it working in Defold first and check in Chrome to make sure the data is properly there at the right path that the HTML JS is expecting. Then use the custom html file (which you’ll want to edit some too). It needs to have better error handling which it doesn’t have yet - this will need to be done before using this in production it’s still only proof that it works right now.

I remove the index.html from defold and game.project work fin with ctrl+b but for html5 launch get the same error

Are you trying to open a file which doesn’t exist yet? You should check if it exists first in the Defold script before doing the other code.

function file_exists(name)
   local f=io.open(name,"r")
   if f~=nil then io.close(f) return true else return false end
end

Does the file you are trying to load show up in the filesystem in the Chrome console?

I remove all code, remove __htmlLaunchDir, all but get the same error, weird.
I export the game and upload to my local server and work, but build and launc html5 from defold not is working.

now is working, this is very crazy lol

Wait for the clean and standalone example. There are a few things which I did that are too messy to use / cause it to break / don’t work at all right yet. I’ve already fixed a few major issues so far but it’s really late for me so I must stop now for real.

If you want ads very easily, consider using giftgaming which is completely SDK-less

1 Like

this is my code in defold:

local path = sys.get_save_file("html5_inbox", "data.json")

-- set content of a file on the filesystem
local html5_data = "1"

function write_file(file, data)
local file = io.open(path, "w")
file:write(data)
file:close()
end

function read_file(file)
local f = io.open(path, "r")
local content = f:read("*all")
f:close()
return content
end

function update()
	print(read_file(path))
end

in the console always get nil, but I should have “test data here”?

your index:

var test_data_array = {timestamp: {}, mode: 33206};
test_data_array.contents = stringToUint("test data here"); // this is obviously not JSON, 
store.put(test_data_array, "/data/.html5_inbox/data.json")

The JS needs to be modified too to make it work. Don’t need to be using atbo / btoa, they both need to be removed to work with Defold’s data. There are other changes which need to be made too but I can’t do right now.

	function stringToUint(string) {
		var string = unescape(encodeURIComponent(string)),
			charList = string.split(''),
			uintArray = [];
		for (var i = 0; i < charList.length; i++) {
			uintArray.push(charList[i].charCodeAt(0));
		}
		return new Uint8Array(uintArray);
	}

	function uintToString(uintArray) {
		var encodedString = String.fromCharCode.apply(null, uintArray),
			decodedString = decodeURIComponent(escape(encodedString));
		return decodedString;
	}

No problem, Pkeod, thank you very much for your help! you are the best!

2 Likes

Hi,

new news? I am very anxious to use defold :slight_smile:

Maxi.

1 Like

I’ll spend some more time on this later today. Will post when I’m done with whatever I have.

Edit: Didn’t have the time, will early tomorrow.

1 Like

I fixed the last bug, but still not ready to upload. I’ll make a new thread for the example, and post here when ready hopefully will have time tomorrow to finish. Too busy with work and life…

3 Likes

Very goods news :slight_smile: