Page 1 of 1
Functions in Save Files
Posted: Fri Nov 07, 2014 10:45 am
by JohnWordsworth
I guess this question is mainly pointed at Petri, but some other Lua aficionados might also know what the "norm" is within Lua.
I'm developing a GUI library (ultimately to build my notebook with). I would like to have widgets as almost class type objects. I was wondering whether the following code would save a bunch of references to a single function, or lots of functions in the save file (bloating the save).
Code: Select all
g_prototypes = {
drawPanel = function(self, widget)
-- Do Draw Stuff
end,
drawLabel = function(self, widget)
-- Do Draw Stuff
end
}
function createPanel(...)
widget.onDraw = g_prototypes.drawPanel;
...
end
-- Create Lots of Widgets
I don't really know much about how typical Lua serialisation handles functions. If creating 20 widgets this way would then result in 20 copies of the "drawPanel" function appearing in the save file, then I will do it a different way. If this would only result in 1 function and 20 references to that function, then this has the cool flexibility of being able to easily define a new onDraw method for a widget.
Re: Functions in Save Files
Posted: Fri Nov 07, 2014 10:56 am
by petri
I don't think this will be a problem. However, I'd suggest looking into IMGUI techniques. Much more easier to use, no need to create the widgets or transfer state between client and widgets. With IMGUI your gui library would just be a list of functions in a script entity. Simple and efficient.
http://iki.fi/sol/imgui/
Re: Functions in Save Files
Posted: Fri Nov 07, 2014 11:24 am
by JohnWordsworth
Many thanks for the feedback Petri! I have been going back and forth about whether a full widget-like hierarchy is over-kill (not that it would be more than a couple hundred lines of code I guess)... But it's good to know that it shouldn't be a problem if I did go down that route.
My original plan was to build up to something which allowed modders to do the following...
Code: Select all
defineGUI({
name = "something",
widgets = {
{
type = "panel",
position = { x=10, y=10 },
size = { width=100, height=50 }
children = {
{
type = "button",
position = { x=5, y=5 },
size = { width=100, height= 30 },
bgColor = { 255, 0, 0, 100 },
text = "Click Me!",
onClick = function(self)
print("Hello!")
end
}
}
}
}
});
gui = createGui("something");
gui:show();
--- Later
gui:destroy();
My primary goal is to make something that's really easy to use... While this feels like it would be pretty cool, maybe it is a little OTT / heavyweight. I will ponder while at work today and see if I get a bit of time over the weekend to throw something together.
Re: Functions in Save Files
Posted: Fri Nov 07, 2014 12:44 pm
by petri
Just to give you some ideas, IMGUI version could be something like this:
Code: Select all
function doGui()
gui.panel{"panel1", 10, 10, 100, 50}
if gui.button{"but1", 5, 5, 100, 30, bgColor = {255,0,0,100}, text = "Click Me!"} then
print("Hello!")
end
end
Re: Functions in Save Files
Posted: Fri Nov 07, 2014 12:47 pm
by petri
And since gui definition is actually code, you can do dynamic GUIs very easily

Re: Functions in Save Files
Posted: Fri Nov 07, 2014 2:12 pm
by JohnWordsworth
I guess that doesn't prevent you from having a basic skinning definition by just having those parameters basically saved as structs that you use...
Code: Select all
skin1 = {
main_panel = {"panel1", 10, 10, 100, 50},
button_1 = {"but1", 5, 5, 100, 30, bgColor = {255,0,0,100}, text = "Click Me!"},
}
function doGui()
local skin = skin1;
gui.panel(skin.main_panel)
if gui.button(skin.button_1) then ... end
end
That's basically what I did with the first notebook, but without the intermediate abstraction layer (my own "gui" object). This feels more in-keeping with the underlying GUI system in LOG though, so I am tempted to go down this route. Decisions, decisions!
Re: Functions in Save Files
Posted: Fri Nov 07, 2014 3:28 pm
by Eleven Warrior
John I am no good at coding but man i know you will get it soon you are a genious mate ahy

Re: Functions in Save Files
Posted: Fri Nov 07, 2014 3:52 pm
by NutJob
Just to add my confirmation (not that it means anything here) I wrap all my functions like that. Something about lookup overhead was mentioned but I haven't noticed, yet.
Re: Functions in Save Files
Posted: Fri Nov 07, 2014 11:35 pm
by JKos
As a one of the grimwidgets developers I would say that try to keep it as simple as possible, most modders are not programmers and they don't understand things that which we have studied many years. So just remember KISS and try to keep it DRY. Grimwidgets was a good idea, but just a few modders understood what it was about.