Functions in Save Files

Ask for help about creating mods and scripts for Grimrock 2 or share your tips, scripts, tools and assets with other modders here. Warning: forum contains spoilers!
Post Reply
User avatar
JohnWordsworth
Posts: 1397
Joined: Fri Sep 14, 2012 4:19 pm
Location: Devon, United Kingdom
Contact:

Functions in Save Files

Post 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).
SpoilerShow

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.
My Grimrock Projects Page with links to the Grimrock Model Toolkit, GrimFBX, Atlas Toolkit, QuickBar, NoteBook and the Oriental Weapons Pack.
User avatar
petri
Posts: 1917
Joined: Thu Mar 01, 2012 4:58 pm
Location: Finland

Re: Functions in Save Files

Post 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/
User avatar
JohnWordsworth
Posts: 1397
Joined: Fri Sep 14, 2012 4:19 pm
Location: Devon, United Kingdom
Contact:

Re: Functions in Save Files

Post 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.
My Grimrock Projects Page with links to the Grimrock Model Toolkit, GrimFBX, Atlas Toolkit, QuickBar, NoteBook and the Oriental Weapons Pack.
User avatar
petri
Posts: 1917
Joined: Thu Mar 01, 2012 4:58 pm
Location: Finland

Re: Functions in Save Files

Post 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
User avatar
petri
Posts: 1917
Joined: Thu Mar 01, 2012 4:58 pm
Location: Finland

Re: Functions in Save Files

Post by petri »

And since gui definition is actually code, you can do dynamic GUIs very easily 8-)
User avatar
JohnWordsworth
Posts: 1397
Joined: Fri Sep 14, 2012 4:19 pm
Location: Devon, United Kingdom
Contact:

Re: Functions in Save Files

Post 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!
My Grimrock Projects Page with links to the Grimrock Model Toolkit, GrimFBX, Atlas Toolkit, QuickBar, NoteBook and the Oriental Weapons Pack.
User avatar
Eleven Warrior
Posts: 752
Joined: Thu Apr 18, 2013 2:32 pm
Location: Australia

Re: Functions in Save Files

Post 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 :)
NutJob
Posts: 426
Joined: Sun Oct 19, 2014 6:35 pm

Re: Functions in Save Files

Post 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.
User avatar
JKos
Posts: 464
Joined: Wed Sep 12, 2012 10:03 pm
Location: Finland
Contact:

Re: Functions in Save Files

Post 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.
- LoG Framework 2http://sites.google.com/site/jkoslog2 Define hooks in runtime by entity.name or entity.id + multiple hooks support.
- cloneObject viewtopic.php?f=22&t=8450
Post Reply