Html5.run causes error (a nil value) (SOLVED)

#1

Maybe someone can help me.

It’s strange, but when u try to run even simple code from example in manual, i have this error:
ERROR:SCRIPT: /main/ui.gui_script:3: attempt to index global ‘html5’ (a nil value)

in my file ui.gui_script i just call html5.run like this:

function init(self)
self.score = 0
local res = html5.run(“10 + 20”)
print(res)
end

Have I forgotten to include some library or something like this?

(i program it all on MacOS)

0 Likes

#2

The module “Html5” works only if you build for Html5 target (“Build and run Html5” or bundle Html5)
Also, it’s useful to check availability none-cross-platform modules before using:

if html5 then
html5.run()
end
1 Like

#3

Thank you, when i do “Build HTML” it goes without errors. but i still can’t get it, how i see the result of that code from example:

I understand that i can make javascript alert or something like that and see alert (i’ll still have to add in HTML after bulding from Defold).
But how does code from example shows result?

0 Likes

#4

You are able to write javascript code as a string and call this code using html5.run():

Executes the supplied string as JavaScript inside the browser. A call to this function is blocking, the result is returned as-is, as a string. (Internally this will execute the string using the eval() JavaScript function.)

For example:

	local result = html5.run("var a = 2+5; b = a + 2; b")
	print(result)

Also, it’s possible to write a javascript code right into index.html temlate in Module.
For example:
index.html template:

<script>
...

	Module["my_prefix_myCustomFunction"] = function(num)
	{
		var a = num+5;
		b = a + num;
		return b;
	};
...
	</script>

Your lua codeLua:

	local num = 4
	local result = html5.run("Module.my_prefix_myCustomFunction("..num..")")
	print(result)
2 Likes

#5

Thank you, now it looks clear. I’ll try this variant with javascript function, it should help.

0 Likes