The open-source game engine and graphics engine for multiplayer, cross-platform, real-time 3D action

It is currently 2014-09-02, 06:16

All times are UTC + 1 hour [ DST ]




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: GUI: dynamically created
PostPosted: 2012-02-27, 17:45 
Offline
User avatar

Joined: 2011-11-23, 09:28
Posts: 85
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:
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?

_________________
Project Status: Code architecture definition
6 Programmers, 1 Photographer, 1 Architect, 1 Game designer


Top
 Profile  
 
PostPosted: 2012-02-28, 01:19 
Offline
Site Admin
User avatar

Joined: 2004-08-19, 13:46
Posts: 1948
Location: Germany
Could you please attach the complete *.cgui files for your GUI?
Things are hard to tell from that fragment alone.

_________________
Best regards,
Carsten


Top
 Profile  
 
PostPosted: 2012-02-28, 12:17 
Offline
User avatar

Joined: 2011-11-23, 09:28
Posts: 85
For sure I can :)
Inventory_init.cgui
Code:
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:
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

_________________
Project Status: Code architecture definition
6 Programmers, 1 Photographer, 1 Architect, 1 Game designer


Top
 Profile  
 
PostPosted: 2012-02-29, 00:45 
Offline
Site Admin
User avatar

Joined: 2004-08-19, 13:46
Posts: 1948
Location: Germany
Well, in function InvBg:OnInit() a line like
Code:
    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:
function InitInventorySlots(SlotsCnt, ...)
    -- ...
end

InitInventorySlots(3);

_________________
Best regards,
Carsten


Top
 Profile  
 
PostPosted: 2012-02-29, 08:59 
Offline
User avatar

Joined: 2011-11-23, 09:28
Posts: 85
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

_________________
Project Status: Code architecture definition
6 Programmers, 1 Photographer, 1 Architect, 1 Game designer


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC + 1 hour [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group