Page 1 of 1

GUI: dynamically created

Posted: 2012-02-27, 17:45
by Haimi
Hi @ll,

I am trying to create an inventory with a variable Number of Slots.
So I tried to realize this using the following Code:

Code: Select all

function InitInventorySlots(SlotsCnt, ...)
	Inventory.SlotsCnt = SlotsCnt;
end

function Inventory:OnActivate()
    local i = 1;
    local ItemsPerLine = 5;
    Slots = {};
    for i = 1, self.SlotsCnt, 1 do
    	Slots[i] = gui:new("WindowT", "Slot" .. i);
    	Slots[i]:set("rect", 540 + ((i % ItemsPerLine) * 28), math.floor(i / ItemsPerLine) * 28, 25, 25);
	    Slots[i]:set("backColor", 0, 0.25, 0.7, 0.4);
	    Slots[i]:set("borderWidth", 0.5);
	    Slots[i]:set("borderColor", 0.7, 0.8, 0.9, 1);
		Inventory:AddChild(Slots[i]);
    end
end
The code used in Inventory:OnActivate() was in InitInventorySlots(SlotsCnt, ...) before, but my Inventory shows up without the Items. No error, just does not show up. Do I have to make an init method for every slot? And how can I realize this with dynamically created windows?

Re: GUI: dynamically created

Posted: 2012-02-28, 01:19
by Carsten
Could you please attach the complete *.cgui files for your GUI?
Things are hard to tell from that fragment alone.

Re: GUI: dynamically created

Posted: 2012-02-28, 12:17
by Haimi
For sure I can :)
Inventory_init.cgui

Code: Select all

InvBg = gui:new("WindowT", "InvBg");

Inventory = gui:new("WindowT", "Inventory");

gui:SetRootWindow(InvBg);

-- Setup the window hierarchy.
-- ***************************
InvBg:AddChild(Inventory);

-- Initialization of the window contents ("constructor code").
-- ***********************************************************

function InvBg:OnInit()
    self:set("backColor", 1, 0, 0, 0.7);

    gui:activate      (false);
    gui:setInteractive(true);
    gui:showMouse     (true);
end

function Inventory:OnInit()
    self:set("rect", 400, 100, 100, 300);
    self:set("backColor", 0, 0.25, 0.7, 0.4);
    self:set("borderWidth", 0.5);
    self:set("borderColor", 0.7, 0.8, 0.9, 1);
	gui:setFocus(Inventory);
end
and my Inventory.cgui

Code: Select all

dofile("Games/obg/GUIs/Inventory_init.cgui");

function InitInventorySlots(SlotsCnt, ...)
    local i = 1;
    local ItemsPerLine = 5;
    Slots = {};
    for i = 1, SlotsCnt, 1 do
    	Slots[i] = gui:new("WindowT", "Slot" .. i);
    	Slots[i]:set("rect", 540 + ((i % ItemsPerLine) * 28), math.floor(i / ItemsPerLine) * 28, 25, 25);
	    Slots[i]:set("backColor", 0, 0.25, 0.7, 0.4);
	    Slots[i]:set("borderWidth", 0.5);
	    Slots[i]:set("borderColor", 0.7, 0.8, 0.9, 1);
		Inventory:AddChild(Slots[i]);
    end
end

function InvBg:OnChar(ch)
    if (ch == 0x1B or ch == 0x69) then -- ESC or I
    	gui:activate(false);
    	return true;
    end
    return false;
end

Re: GUI: dynamically created

Posted: 2012-02-29, 00:45
by Carsten
Well, in function InvBg:OnInit() a line like

Code: Select all

    self:set("rect", 0, 0, 640, 480);
is missing if you want it to be visible.

Function InitInventorySlots() is nowhere called in your code, thus it cannot generate the windows that you expect.

If I copy the contents of InitInventorySlots() into the Inventory_init.cgui file and add line local SlotsCnt = 3; as well, then I see three generated child windows when I load the GUI into the GUI Editor.
Note however that the position of child windows is relative to their parent, so in this case, an x-coordinate of 540 and more is way off screen. ;-)

Btw., best you make sure that InitInventorySlots() is only called onceā€¦ e.g. just call if from the main body of the script:

Code: Select all

function InitInventorySlots(SlotsCnt, ...)
    -- ...
end

InitInventorySlots(3);

Re: GUI: dynamically created

Posted: 2012-02-29, 08:59
by Haimi
AH, oh my god, just found it: InitInventorySlots() is called on button press from c++, but i did not realize that I need to place the items relatively :D Thanks for opening my eyes :D