About Store Forum Documentation Contact
Donations:
140$/mo



Post Reply 
Problem using setData method of List GUI object
Author Message
phleshdef Offline
Member

Post: #1
Problem using setData method of List GUI object
I'm attempting to do a simple test to create and populate a List inside of a window. The goal here is to create an Inventory GUI that functions similar to the SkyUI mod for Skyrim.

Everything seems to work except for the part where I actually try to populate the darn thing with some dummy test data. It builds fine, but the game crashes whenever I call the setData method, passing a Memx object to it.

This is basically my code here:
Code:
void InventoryWindow::Create()
{
    Text_ds.color = BLACK;
    Text_ds.shadow = 0;
    Gui += window.create(Rect(-0.5f, -0.4f, 0.5f, 0.4f), "Inventory");
    window += region.create(Rect( 0.05f, -0.6f, 0.95f, -0.1f));     
    window.hide();
    IsShowing = false;
}

void InventoryWindow::Show()
{    
    if (!IsShowing)
    {
        invItems.clear();

        Memx<ItemInfo> data;
    
        ItemInfo &itemInfo = data.New();        

        itemInfo.name = "Test 1";
        itemInfo.damage = 20;
        itemInfo.value = 5;
        
        ListColumn list_column[]=
        {
           ListColumn(MEMBER(ItemInfo, name), 0.3f, L"Name"),
           ListColumn(MEMBER(ItemInfo, damage), 0.3f, L"Damage"),
           ListColumn(MEMBER(ItemInfo, value), 0.3f, L"Value")
        };
    
        region += invItems.create(list_column, Elms(list_column)).setData(data);    

        Ms.visible(true);
        window.show();
        IsShowing = true;
    }
}

I'm ultimately calling the above InventoryWindow.Show() method using an Input handling class that I created that "listens" for input in the UpdateGame() function in Game.cpp.

ItemInfo is just a struct I created with 3 basic properties for testing. I noticed that if I remove the ItemInfo &itemInfo = data.New(); part OR if I remove the setData(data) lines, the crash doesn't occur (I just get a blank grid with some column headers). I've peeled through the tutorials, the documentation, I've read every thread in this forum dealing with Memx and List. I've been through the RPG2 source code, trying every alternative I can think of to try and get this to work and I'm just not sure what I'm doing wrong here. I've also tried this same approach using the other Mem type classes and even using a simple, regular old array of my ItemInfo struct. I get the same result for all the variations.

Admittedly, C++ (particularly working with pointers) isn't my strong suit as I've been working professionally with C# for quite awhile now, haven't really had to work much with C++, and so I may be doing something wrong in regards to some required pointer manipulation (yes I know I'm not using a pointer in the above example, but I've tried passing a pointer to data instead of data itself during all of this). If someone could just glance at the above code and give me some suggestions as to what I may be doing wrong, it would be greatly appreciated.
(This post was last modified: 09-22-2012 10:23 PM by phleshdef.)
09-22-2012 10:09 PM
Find all posts by this user Quote this message in a reply
Rubeus Offline
Member

Post: #2
RE: Problem using setData method of List GUI object
What is the crash you are getting?
Did you make sure that data is getting populated with your data properly?
Make sure this line: ItemInfo &itemInfo = data.New(); works as intended.
09-23-2012 12:35 AM
Find all posts by this user Quote this message in a reply
fatcoder Offline
Member

Post: #3
RE: Problem using setData method of List GUI object
Gui objects and gui data need to be kept in constant memory, such as heap of data memory. You're declaring your "data" in stack memory, so its being deallocated when exiting Show().

To solve the problem, you could declare your data as a global (like below), or leave it where it is and make it static. Although I do not recommend either of these approaches. If possible, declare it in a class or struct instead, or at least a namespace.

Code:
Memx<ItemInfo> data;

void InventoryWindow::Create()
{
    Text_ds.color = BLACK;
    Text_ds.shadow = 0;
    Gui += window.create(Rect(-0.5f, -0.4f, 0.5f, 0.4f), "Inventory");
    window += region.create(Rect( 0.05f, -0.6f, 0.95f, -0.1f));     
    window.hide();
    IsShowing = false;
}

void InventoryWindow::Show()
{    
    if (!IsShowing)
    {
        invItems.clear();

        ItemInfo &itemInfo = data.New();        

        itemInfo.name = "Test 1";
        itemInfo.damage = 20;
        itemInfo.value = 5;
        
        ListColumn list_column[]=
        {
           ListColumn(MEMBER(ItemInfo, name), 0.3f, L"Name"),
           ListColumn(MEMBER(ItemInfo, damage), 0.3f, L"Damage"),
           ListColumn(MEMBER(ItemInfo, value), 0.3f, L"Value")
        };
    
        region += invItems.create(list_column, Elms(list_column)).setData(data);    

        Ms.visible(true);
        window.show();
        IsShowing = true;
    }
}
09-23-2012 12:44 AM
Find all posts by this user Quote this message in a reply
phleshdef Offline
Member

Post: #4
RE: Problem using setData method of List GUI object
Fatcoder, thx a bunch. Yea, that fixes my problem and makes total sense. I made it a member of my InventoryWindow struct and its all good now.
(This post was last modified: 09-23-2012 02:51 AM by phleshdef.)
09-23-2012 02:27 AM
Find all posts by this user Quote this message in a reply
Post Reply