Webview error (a nil value) (SOLVED)


#1

Hi, I just started using Defols and I’m already making some progress with my game.
I need to implement a webview to open an HTML file but when if I try to run this snippet of code:

local 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)
    local request_id = webview.open(webview_id, "http://www.example.com", {hidden = true})
end

I get this error:

ERROR:SCRIPT: /main/main.script:18: attempt to index global ‘webview’ (a nil value)
stack traceback:
/main/main.script:18: in function </main/main.script:17>

What am I doing wrong?


#2

Hi @gabrieletrevisan02!

The webview is only available on the mobile platforms (iOS + Android). What platform are you running it on? (I think this can be more clearly stated in the documentation, I’ve added a ticket for that)

You can add a check for it by checking if it’s unavailable (i.e. nil):

if webview == nil then
    ... continue the init code ...
end

#3

Hi @Mathias_Westerdahl, thank you so mutch for the quick response!
I was trying to use the webview on Windows just to debug the app, now that I created a bundle for Android and installed it on my phone, it’s working perfectly!