Hey there, wondering if anybody could help me out with this?
The TLDR is it is a module that should populate spells learned by character, when clicking on spell it should cast. The module opens, but nothing is in it.
Server side
Client side
Originally I didn't have the JSON.lua and such but browsing everything I found Oen's post regarding it and added them in the correct places on server and client, and set those up..
The error I get when running this script in terminal on client is as follows;
Maybe the JSON is communicating? Possibly something else at fault? I am unsure on how to proceed, any help is appreciated.
The TLDR is it is a module that should populate spells learned by character, when clicking on spell it should cast. The module opens, but nothing is in it.
Server side
Lua:
local OPCODE_CASTER = 108
local storage = 55390
function onExtendedOpcode(player, opcode, buffer)
local status, json_data =
pcall(
function()
return json.decode(buffer)
end
)
if not status then
return false
end
local action = json_data.action
local data = json_data.data
local spells = {}
if opcode == OPCODE_CASTER then
if action == "refresh" then
for _, spell in ipairs(player:getInstantSpells()) do
if player:getLevel() > spell.level then
spells[#spells + 1] = spell
end
end
table.sort(spells, function(a, b) return a.level > b.level end)
player:sendExtendedOpcode(OPCODE_CASTER, json.encode({action = "refresh", data = spells}))
elseif action == "cast" then
player:setStorageValue(storage, 1)
end
end
return true
end
Client side
Lua:
spellscasterWindow = nil
spellscasterButton = nil
spellsTable = nil
spells = nil
function init()
spellscasterWindow = g_ui.loadUI('spellscaster', modules.game_interface.getLeftPanel())
spellscasterWindow:setup()
g_keyboard.bindKeyPress('Delete', toggle)
connect(g_game, {
onGameStart = refresh,
onGameEnd = offline
})
local protocolGame = g_game.getProtocolGame()
if protocolGame then
protocolGame.registerExtendedOpcode(108, onExtendedOpcode)
end
spellscasterButton = modules.client_topmenu.addRightToggleButton('spellscasterButton', tr('Spellbook'), '/images/topbuttons/spelllist', toggle)
spellscasterButton:setOn(true)
end
function terminate()
protocolGame.unregisterExtendedOpcode(108, onExtendedOpcode)
end
function onExtendedOpcode(protocol, code, buffer)
local json_status, json_data =
pcall(
function()
return json.decode(buffer)
end
)
if not json_status then
g_logger.error("SHOP json error: " .. json_data)
return false
end
local action = json_data.action
local data = json_data.data
if not action or not data then
return false
end
if action == "refresh" then
spells = data
refresh()
end
end
function offline()
spellscasterWindow:hide()
end
function refresh()
local uiList = spellscasterWindow.contentsPanel
--local cleanList = uiList:getChildren()
--for k = 1, #children do children[k]:destroy() end
for i = 1, #spells do
local playerLabel = g_ui.createWidget('SpellsButton', uiList)
playerLabel.clickButton:setText(spells[i].name)
playerLabel.icon:setPercent(spells[i].cooldown)
playerLabel.onClick = function(widget)
local protocolGame = g_game.getProtocolGame()
if protocolGame then
scheduleEvent(function() g_game.talk(spells[i].words) end, 100)
protocolGame:sendExtendedOpcode(108, json.encode({action = "cast", data = spells[i].words}))
end
end
end
end
function toggle()
local protocolGame = g_game.getProtocolGame()
if protocolGame then
protocolGame:sendExtendedOpcode(108, json.encode({action = "refresh"}))
end
if spellscasterButton:isOn() then
spellscasterWindow:close()
spellscasterButton:setOn(false)
else
spellscasterWindow:open()
spellscasterButton:setOn(true)
end
end
Originally I didn't have the JSON.lua and such but browsing everything I found Oen's post regarding it and added them in the correct places on server and client, and set those up..
The error I get when running this script in terminal on client is as follows;
Code:
ERROR: protected lua call failed: LUA ERROR:
/spells_caster/spellscaster.lua:61: attempt to get length of global 'spells' (a nil value)
stack traceback:
[C]: in function '__len'
/spells_caster/spellscaster.lua:61: in function </spells_caster/spellscaster.lua:57
Maybe the JSON is communicating? Possibly something else at fault? I am unsure on how to proceed, any help is appreciated.
Last edited: