Implement Gamedistribution ads using webview

Hi Pkeod,

thank you very much for the info, I will try now, but if you have some time free to make an example would be great.

Regards.
Maxi.

I am very confused.
I have this code in defold to save my game, I found the code here in the forum.

local M = {
  player = {
    highscore = 0,
    score = 0
  }
}

local shared_data = M
local save_file = sys.get_save_file("my_game", "gamedata1")

function save_gamedata(self)
	-- Reset player score
	--shared_data.player.score = 0
	-- Save gamedata
	if not sys.save(save_file, shared_data) then
		pprint("Gamedata not saved.")
	end
end

function load_gamedata(self)
	--	Load gamedata
	local gamedata = sys.load(save_file)
	if not next(gamedata)  then
		pprint("Gamedata empty")
	else
		pprint(gamedata)
		shared_data.player.highscore = gamedata.player.highscore
	end
end

function init(self)
	msg.post(".", "acquire_input_focus")
	load_gamedata(self)
	M.player.score = 18
	save_gamedata()
	--load_menu(self)
end

function on_message(self, message_id, message, sender)

end

but I am unable to read values from the index.html, I am very confused with the names of the databases and others problems.

can you help me?

Regards.
Maxi.

sys.save saves to a Lua table. You need to use the JSON encoder I linked to in the last post, and use it to convert your Lua tables into JSON, which will be easier for your JS to access. Youā€™ll also need to use Luaā€™s io functions and not Defoldā€™s for the loading / writing on the Defold side.

local json = require("utils.json") -- loads json.lua from the /utils/json.lua file

local inbox_data = {data = "test"}
local inbox_path = sys.get_save_file("html5_inbox", inbox_file)
local encoded_inbox = json.encode(inbox_data)
print(encoded_inbox)
print(inbox_path)

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

Test something like this in your HTML5 build. Look up Lua file io.

If you use the Chrome browser: on your html page, right click on page anywhere and click Inspect -> Application -> Storage -> IndexedDB

As far as using the data goes, I will have to do tests when I can (doing some now but canā€™t promise Iā€™ll have enough time to make it work). Maybe Defold engine team can mention any gotchas for HTML5 IndexedDB filesystem storage. From a quick look, it looks like everything is encoded one character at a time?.. if I save ā€œhiā€ to a file I get

{timestamp: {}, mode: 33206, contents: {0: 104, 1: 105}}

Looking more into this it seems like an emscripten MEMFS thing. There will have to be another step. Looking into itā€¦

I have this error:

ā€˜/utils/json.luaā€™ canā€™t be found loader.script /race/content/main line 0 Problem

Download the file I linked to before, and put it somewhere where you would like to store utils at. You cannot require the file until it exists in your project.

sorry, I am new in defold, thanks.

what is ā€œinbox_fileā€ here:

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

I explained this in the previous post too - sys.get_save_file gets an appropriate path to save / load to. You specify a project name and a filename.

In chrome: indexedDB->/data->filedata i get this:

key: "/data/.html5_inbox/hello"
value: {timestamp: {}, mode: 33206,ā€¦}

so, we need decode value?

Yes, thatā€™s the extra step. Weā€™ll have to encode / decode to the array buffer. Iā€™m still looking things up.

it looks complicated

It wonā€™t be so bad. Of course directly communicating with the game would be easier if it was implementedā€¦

Iā€™m testing things then Iā€™ll post an update.

great, i am very anxious :slight_smile:

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.