I guess I will have to save the state of button.
btw could you make method to change caption? Deleting and recreating buttons change the order of buttons
edit: I keep getting this "LuaGuiElement::LuaIndex: unknown key nameofbutton" with destroy() too. Eventhough I see the button on screen. I dunno, might be my code.
edit2: Here is the code that I'm having problems with. It supposed to be creative mode. Works nice except after couple clicks it kind of looses reference to buttons it supposed to destroy.
Code: Select all
require "defines"
itemlist = {}
--
-- [1.1] Script Events
--
-- [[
game.oninit(function()
glob.stacksize = 1
glob.itemiterator = 1
glob.hiden = false
glob.ibuttons = {a="iron-plate",b="copper-plate",c="stone",d="steel-plate",e="coal"}
game.player.gui.add({position="ontherightofthegoal", type="flow", name="creativeflow"})
game.player.gui.creativeflow.add({position="ontherightofthegoal", type="button", name="creativebutton", caption="Creative ON/OFF"})
end)
game.onload(function()
end)
game.onsave(function()
end)
--]]
--
-- [1.2] Game Events
--
function ontick(event)
end
function onguitick(event)
if event.element.name == "creativebutton" then
creative_toggle()
elseif event.element.name == "hidebutton" then
toggle_inserts()
elseif event.element.name == "stacksizebutton" then
change_stacksize()
elseif event.element.name == "loopitemsbutton" then
loopitems()
elseif event.element.name == "a_button" then
insert_a()
elseif event.element.name == "b_button" then
insert_b()
elseif event.element.name == "c_button" then
insert_c()
elseif event.element.name == "d_button" then
insert_d()
elseif event.element.name == "e_button" then
insert_e()
end
end
function onbuiltentity(event)
if glob.creative ~= nil then
if glob.creative then
creativerestore(event.createdentity.name)
end
end
end
-- ontick = 0,
-- onguitick = 1,
-- onentitydied = 2,
-- onpickedupitem = 3,
-- onbuiltentity = 4,
-- onsectorscanned = 5,
-- onplayermineditem = 6,
-- onputitem = 7,
-- onshiplandingstart = 8
game.onevent(defines.events, function(event)
if event.name == defines.events.ontick then
--ontick(event)
elseif event.name == defines.events.onguitick then
game.player.print("OnGuiTick: "..event.element.name)
onguitick(event)
elseif event.name == defines.events.onentitydied then
game.player.print("OnEntityDied: "..event.entity.name)
--onentitydied(event)
elseif event.name == defines.events.onpickedupitem then
game.player.print("OnPickedUpItem:")
tprint(event)
--onpickedupitem(event)
elseif event.name == defines.events.onbuiltentity then
game.player.print("OnBuiltEntity:")
tprint(event)
onbuiltentity(event)
elseif event.name == defines.events.onsectorscanned then
game.player.print("OnSectorScanned:")
tprint(event)
--onsectorscanned(event)
elseif event.name == defines.events.onplayermineditem then
game.player.print("OnPlayerMinedItem:")
tprint(event)
--onplayermineditem(event)
elseif event.name == defines.events.onputitem then
game.player.print("OnPutItem:")
tprint(event)
--onputitem(event)
elseif event.name == defines.events.onshiplandingstart then
game.player.print("OnShipLandingStart:")
tprint(event)
--onshiplandingstart(event)
else
game.player.print("Unknown Event (>8):")
tprint(event)
end
end)
--
--[1.3] Script interface
--
remote.addinterface("creativemode",
{
printglob = function()
deeptprint(glob)
end
})
--
-- [2.1] Inventory Functions
--
function canremove(stack, inv)
if inv.getitemcount(stack.name) >= stack.count then
return true
else
return false
end
end
function insertstacks(slist, container)
for k, v in pairs(slist) do
if container.caninsert({name = k, count = v}) then
container.insert({name = k, count = v})
else
game.player.print("Fail to insert: " .. v .. " " .. k)
end
end
end
--
-- 2.2 Creative Functions
--
function creative_toggle()
if glob.creative == nil then
glob.creative = true
else
glob.creative = not glob.creative
end
if glob.creative then
--game.player.gui.creativeflow.add({position="ontherightofthegoal", type="button", name="hidebutton", caption="Hide/Show Inserts"})
if not glob.hiden then
game.player.gui.creativeflow.add({position="ontherightofthegoal", type="button", name="stacksizebutton", caption="Change Stacksize: "..glob.stacksize})
game.player.gui.creativeflow.add({position="ontherightofthegoal", type="button", name="loopitemsbutton", caption="Loop Items"})
game.player.gui.creativeflow.add({position="ontherightofthegoal", type="button", name="a_button", caption="A: "..glob.ibuttons.a})
game.player.gui.creativeflow.add({position="ontherightofthegoal", type="button", name="b_button", caption="B: "..glob.ibuttons.b})
game.player.gui.creativeflow.add({position="ontherightofthegoal", type="button", name="c_button", caption="C: "..glob.ibuttons.c})
game.player.gui.creativeflow.add({position="ontherightofthegoal", type="button", name="d_button", caption="D: "..glob.ibuttons.d})
game.player.gui.creativeflow.add({position="ontherightofthegoal", type="button", name="e_button", caption="E: "..glob.ibuttons.e})
end
else
--game.player.gui.creativeflow["hidebutton"].destroy()
if not glob.hiden then
game.player.gui.creativeflow["stacksizebutton"].destroy()
game.player.gui.creativeflow["loopitemsbutton"].destroy()
game.player.gui.creativeflow["a_button"].destroy()
game.player.gui.creativeflow["b_button"].destroy()
game.player.gui.creativeflow["c_button"].destroy()
game.player.gui.creativeflow["d_button"].destroy()
game.player.gui.creativeflow["e_button"].destroy()
end
end
for k,v in pairs(itemlist) do itemlist[k]=nil end
i = 1
for k, recipe in pairs(game.player.force.recipes) do
itemlist[i] = recipe.name
i = i + 1
end
game.player.print("Itemlist size: " .. #itemlist)
end
function creativerestore(item)
stack = {name = item, count = 1}
hotbar = game.player.character.getinventory(defines.inventory.playerquickbar)
inv = game.player.character.getinventory(defines.inventory.playermain)
if hotbar.caninsert(stack) then
hotbar.insert(stack)
elseif inv.caninsert(stack) then
inv.insert(stack)
end
end
function toggle_inserts()
game.player.print(tostring( glob.hiden ))
if glob.hiden then
game.player.gui.creativeflow.add({position="ontherightofthegoal", type="button", name="stacksizebutton", caption="Change Stacksize: "..glob.stacksize})
game.player.gui.creativeflow.add({position="ontherightofthegoal", type="button", name="loopitemsbutton", caption="Loop Items"})
game.player.gui.creativeflow.add({position="ontherightofthegoal", type="button", name="a_button", caption="A: "..glob.ibuttons.a})
game.player.gui.creativeflow.add({position="ontherightofthegoal", type="button", name="b_button", caption="B: "..glob.ibuttons.b})
game.player.gui.creativeflow.add({position="ontherightofthegoal", type="button", name="c_button", caption="C: "..glob.ibuttons.c})
game.player.gui.creativeflow.add({position="ontherightofthegoal", type="button", name="d_button", caption="D: "..glob.ibuttons.d})
game.player.gui.creativeflow.add({position="ontherightofthegoal", type="button", name="e_button", caption="E: "..glob.ibuttons.e})
else
game.player.gui.creativeflow["stacksizebutton"].destroy()
game.player.gui.creativeflow["loopitemsbutton"].destroy()
game.player.gui.creativeflow["a_button"].destroy()
game.player.gui.creativeflow["b_button"].destroy()
game.player.gui.creativeflow["c_button"].destroy()
game.player.gui.creativeflow["d_button"].destroy()
game.player.gui.creativeflow["e_button"].destroy()
end
glob.hiden = not glob.hiden
end
function change_stacksize()
if glob.stacksize < 1024 then
glob.stacksize = glob.stacksize * 2
else
glob.stacksize = 1
end
game.player.gui.creativeflow["stacksizebutton"].destroy()
game.player.gui.creativeflow.add({position="ontherightofthegoal", type="button", name="stacksizebutton", caption="Change Stacksize: "..glob.stacksize})
end
function loopitems()
if (glob.itemiterator + 4) > #itemlist then
glob.itemiterator = 1
end
glob.ibuttons.a = itemlist[glob.itemiterator]
glob.ibuttons.b = itemlist[glob.itemiterator + 1]
glob.ibuttons.c = itemlist[glob.itemiterator + 2]
glob.ibuttons.d = itemlist[glob.itemiterator + 3]
glob.ibuttons.e = itemlist[glob.itemiterator + 4]
glob.itemiterator = glob.itemiterator + 5
game.player.gui.creativeflow["a_button"].destroy()
game.player.gui.creativeflow["b_button"].destroy()
game.player.gui.creativeflow["c_button"].destroy()
game.player.gui.creativeflow["d_button"].destroy()
game.player.gui.creativeflow["e_button"].destroy()
game.player.gui.creativeflow.add({position="ontherightofthegoal", type="button", name="a_button", caption="A: "..glob.ibuttons.a})
game.player.gui.creativeflow.add({position="ontherightofthegoal", type="button", name="b_button", caption="B: "..glob.ibuttons.b})
game.player.gui.creativeflow.add({position="ontherightofthegoal", type="button", name="c_button", caption="C: "..glob.ibuttons.c})
game.player.gui.creativeflow.add({position="ontherightofthegoal", type="button", name="d_button", caption="D: "..glob.ibuttons.d})
game.player.gui.creativeflow.add({position="ontherightofthegoal", type="button", name="e_button", caption="E: "..glob.ibuttons.e})
end
function insert_a() game.player.character.insert({name=glob.ibuttons.a,count=glob.stacksize}) end
function insert_b() game.player.character.insert({name=glob.ibuttons.b,count=glob.stacksize}) end
function insert_c() game.player.character.insert({name=glob.ibuttons.c,count=glob.stacksize}) end
function insert_d() game.player.character.insert({name=glob.ibuttons.d,count=glob.stacksize}) end
function insert_e() game.player.character.insert({name=glob.ibuttons.e,count=glob.stacksize}) end
--
-- 2.3 Print Functions
--
function tprint(tbl)
if tbl == nil then
game.player.print("No table.")
end
for k, v in pairs(tbl) do
game.player.print( tostring(k) .. "=" .. tostring(v) )
end
end
function deeptprint(tbl)
--game.player.print(table.tostring(tbl))
game.showmessagedialog(table.tostring(tbl))
end
--
-- Following function are from lua-users-org/wiki/TableUtils
--
function table.val_to_str ( v )
if "string" == type( v ) then
v = string.gsub( v, "\n", "\\n" )
if string.match( string.gsub(v,"[^'\"]",""), '^"+$' ) then
return "'" .. v .. "'"
end
return '"' .. string.gsub(v,'"', '\\"' ) .. '"'
else
return "table" == type( v ) and table.tostring( v ) or
tostring( v )
end
end
function table.key_to_str ( k )
if "string" == type( k ) and string.match( k, "^[_%a][_%a%d]*$" ) then
return k
else
return "[" .. table.val_to_str( k ) .. "]"
end
end
function table.tostring( tbl )
local result, done = {}, {}
for k, v in ipairs( tbl ) do
table.insert( result, table.val_to_str( v ) )
done[ k ] = true
end
for k, v in pairs( tbl ) do
if not done[ k ] then
table.insert( result,
table.key_to_str( k ) .. "=" .. table.val_to_str( v ) )
end
end
return "{" .. table.concat( result, "," ) .. "}"
end