Poki sdk error "attempt to index global 'poki_sdk' (a nil value)"

Hi, I am trying to use poki sdk extenstion. I’ve fetched a library

and am trying to use it like this

function init(self)
	poki_sdk.gameplay_start()
	resources.load()
end

but I get an error
ERROR:SCRIPT: main/assets/levels/level_loader.script:26: attempt to index global 'poki_sdk' (a nil value)

What step am I missed?

1 Like

Are you using Build HTML? The extension probably wont work for native build.

2 Likes

Oh, thank you! I really didn’t think about that.

2 Likes

This is my `poki_helper.lua`. And it works very well. I use it in all my Poki projects.

maybe it can help you.

-- 仅在 HTML5 下加载原生扩展;桌面/移动端打包不会注册 poki_sdk 表,需 pcall 保护。
local html5 = sys.get_sys_info().system_name == "HTML5"
local poki_sdk = nil -- 明确初始化为 nil
if html5 then
	local ok, mod = pcall(require, "poki_sdk")
	if ok then
		poki_sdk = mod
	end
end

local M = {}

-- 1. 将 poki_sdk 直接暴露给外部
M.sdk = poki_sdk

function M.is_active()
	return poki_sdk ~= nil
end

function M.set_debug_from_engine()
	if not poki_sdk then
		return
	end
	local info = sys.get_engine_info()
	poki_sdk.set_debug(info.is_debug)
end

function M.gameplay_start()
	if poki_sdk then
		poki_sdk.gameplay_start()
	end
end

function M.gameplay_stop()
	if poki_sdk then
		poki_sdk.gameplay_stop()
	end
end

--- 首次进入可玩状态:按 Poki 建议在 gameplay_start 前先 commercial_break(与官方 HTML 示例一致)。
function M.on_first_session_ready()
	if not poki_sdk then
		return
	end
	poki_sdk.commercial_break(function(_, status)
		if status == poki_sdk.COMMERCIAL_BREAK_SUCCESS then
			poki_sdk.gameplay_start()
		end
	end)
end

--- 换关、重开关卡前:gameplay_stop → commercial_break → 再执行 callback(例如真正 load/reload)。
function M.before_session_reload(after_break_fn)
	if not poki_sdk then
		after_break_fn()
		return
	end
	poki_sdk.gameplay_stop()
	poki_sdk.commercial_break(function(_, status)
		if status == poki_sdk.COMMERCIAL_BREAK_SUCCESS then
			after_break_fn()
		end
	end)
end

function M.before_session_reload_for_load_level(after_break_fn)
	if not poki_sdk then
		after_break_fn()
		return
	end
	-- poki_sdk.gameplay_stop()
	poki_sdk.commercial_break(function(_, status)
		if status == poki_sdk.COMMERCIAL_BREAK_SUCCESS then
			after_break_fn()
		end
	end)
end

return M