x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Slate SListView item source not being accepted.

I'm trying to get a basic UI working using Slate, unfortunately I'm still fairly inexperienced in C++ so I might be missing something really obvious. Here's the code in my widget cpp file:

 void SMyGameSlateHUDWidget::Construct(const FArguments& InArgs)
 {
     OwnerHUD = InArgs._OwnerHUD;
 
     TArray Items = GetListOfItems();
 
     ChildSlot
     .VAlign(VAlign_Fill)
     .HAlign(HAlign_Fill)
     [
             SNew(SOverlay)
             +SOverlay::Slot()
             .VAlign(VAlign_Bottom)
             .HAlign(HAlign_Center)
             .Padding(TAttribute(this, &SMyGameSlateHUDWidget::GetPadding))
             [
                 SNew(STextBlock)
                 .ShadowColorAndOpacity(FLinearColor::Black)
                 .ColorAndOpacity(FLinearColor::Red)
                 .ShadowOffset(FIntPoint(-1,1))
                 .Text(this, &SMyGameSlateHUDWidget::GetSomeString)
                 
             ]
             +SOverlay::Slot()
             .VAlign(VAlign_Center)
             .HAlign(HAlign_Left)
             [
                 
                     SNew(SListView)
                     .ItemHeight(24)
                     .ListItemsSource(&Items)
                     .HeaderRow
                     (
                         SNew(SHeaderRow)
                         + SHeaderRow::Column("Name")
                         [
                             SNew(SBorder)
                             .Padding(5)
                             .Content()
                             [
                                 SNew(STextBlock)
                                 .Text(FString(TEXT("Name")))
                             ]
                         ]
                         + SHeaderRow::Column("Number") .DefaultLabel(FString(TEXT("Number")))
                         + SHeaderRow::Column("TextField") .DefaultLabel(FString(TEXT("Text Field")))
                         + SHeaderRow::Column("TextBlock") .DefaultLabel(FString(TEXT("Text Block")))
                         + SHeaderRow::Column("AddChild") .DefaultLabel(FString(TEXT("Add Child")))
                     )
 
                 
             ]
     ];
         
 }
 
 TArray SMyGameSlateHUDWidget::GetListOfItems() const
 {
     FString temp = FString(TEXT("TestText"));
     TArray Items;
     Items[0] = &temp;
 
     return Items;
 }

The compiler throws this error:

 c:\program files\rocket\engine\source\runtime\slate\public\TableView/TableViewTypeTraits.h(215): error C2664: 'FReferenceCollector::AddReferencedObject' : cannot convert parameter 1 from 'FString *' to 'const UObject *'

With a reference later on to the error being on the SNew(SListView) line.

I don't understand why, since it's the same as what the example code shows. What am I missing?

Thanks,

Matt.

Product Version: Not Selected
Tags:
more ▼

asked Mar 11 '14 at 02:31 AM in C++ Programming

avatar image

ue4-archive ♦♦ STAFF
50k 3670 1999 9124

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

2 answers: sort voted first

Any change in behavior if you use a Shared Ptr?

 SNew(SListView<    TSharedPtr   >)

instead of

 SNew(SListView)


:)

Rama

more ▼

answered Mar 11 '14 at 02:32 AM

avatar image

ue4-archive ♦♦ STAFF
50k 3670 1999 9124

avatar image ue4-archive ♦♦ STAFF Mar 11 '14 at 02:32 AM

Well, it compiles now, but when I start it it gives me:

 C:\Program Files\Rocket\Engine\Source\Runtime\Core\Public\Containers\Array.h(517): Ensure condition failed: (Index != 0) | (ArrayNum != 0)


I'm guessing something's wrong with the way I create my array:

 void SMyGameSlateHUDWidget::Construct(const FArguments& InArgs)
 {
     OwnerHUD = InArgs._OwnerHUD;
 
     TArray> Items = GetListOfItems();
     
 
     ChildSlot
     .VAlign(VAlign_Fill)
     .HAlign(HAlign_Fill)
     [
             SNew(SOverlay)
             +SOverlay::Slot()
             .VAlign(VAlign_Bottom)
             .HAlign(HAlign_Center)
             .Padding(TAttribute(this, &SMyGameSlateHUDWidget::GetPadding))
             [
                 SNew(STextBlock)
                 .ShadowColorAndOpacity(FLinearColor::Black)
                 .ColorAndOpacity(FLinearColor::Red)
                 .ShadowOffset(FIntPoint(-1,1))
                 .Text(this, &SMyGameSlateHUDWidget::GetSomeString)
                 
             ]
             +SOverlay::Slot()
             .VAlign(VAlign_Center)
             .HAlign(HAlign_Left)
             [
                 
                     SNew(SListView>)
                     .ItemHeight(24)
                     .ListItemsSource(&Items)
                     .HeaderRow
                     (
                         SNew(SHeaderRow)
                         + SHeaderRow::Column("Name")
                         [
                             SNew(SBorder)
                             .Padding(5)
                             .Content()
                             [
                                 SNew(STextBlock)
                                 .Text(FString(TEXT("Name")))
                             ]
                         ]
                         + SHeaderRow::Column("Number") .DefaultLabel(FString(TEXT("Number")))
                         + SHeaderRow::Column("TextField") .DefaultLabel(FString(TEXT("Text Field")))
                         + SHeaderRow::Column("TextBlock") .DefaultLabel(FString(TEXT("Text Block")))
                         + SHeaderRow::Column("AddChild") .DefaultLabel(FString(TEXT("Add Child")))
                     )
 
                 
             ]
     ];
         
 }
 
 TArray> SMyGameSlateHUDWidget::GetListOfItems() const
 {
     FString temp = FString(TEXT("TestText"));
     TSharedPtr temp2 = TSharedPtr(&temp);
     TArray> Items;
     Items[0] = temp2;
 
     return Items;
 }
avatar image ue4-archive ♦♦ STAFF Mar 11 '14 at 02:32 AM

Hi there!

I believe you have an error here:

 TArray> Items;
     Items[0] = temp2;

in

 TArray> SMyGameSlateHUDWidget::GetListOfItems() const
 {
     FString temp = FString(TEXT("TestText"));
     TSharedPtr temp2 = TSharedPtr(&temp);
     TArray> Items;
     Items[0] = temp2;
 
     return Items;
 }


you are making an array, and then trying to access element 0, when no elements yet exist, so you are going out of the bounds of the array.

Using .Add()

To add an item to a new array, you need to use .Add

 TArray> Items;
 Items.Add(temp2);
avatar image ue4-archive ♦♦ STAFF Mar 11 '14 at 02:32 AM

Excellent, thanks Rama :)

Coming from doing C# in Unity3d, all this pointer stuff is a bit tricky to get my head around.

avatar image ue4-archive ♦♦ STAFF Mar 11 '14 at 02:32 AM

I'm happy to have helped out!

:)

:)

Rama

PS: if your issue is resolved please check mark my answer, or let us know what still needs resolving related to this topic :)

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

There's a number of problems with your code:

First, TArrays must be defined with a template argument. The syntax is just like Lists in C#. Also the SListView only accepts TArray's of pointer values. So you should define your arrays as

 TArray<TSharedPtr<FString>>

As Rama pointed out SListView also requires a template argument, i.e.

 SNew(SListView<TSharedPtr<FString>>)

But most importantly you're handling your array memory incorrectly. The SListView holds onto a pointer to the array you specify as ListItemSource. That means your array needs to stay around for as long as your SListView is around or the SListView will try to access invalid memory.

You should make your TArray a member/variable of your class instead of a local variable for your function. By making it part of the function it will be cleaned up after the function exits leaving you with unpredictable behavior.

Another thing, you should only ever create a TSharedPtr using this syntax:

 TSharedPtr<FString> Item = MakeShareable(new FString())

Creating a TSharedPtr the way you are in your code causes a memory leak/corruption.

Ultimately, you want something like the following:

 class SMyGameSlateHUDWidget
 {
 public:
 
 void SMyGameSlateHUDWidget::Construct(const FArguments& InArgs)
 {
     OwnerHUD = InArgs._OwnerHUD;
 
     Items.Add(MakeShareable(new FString(TEXT("Sup"))));
     Items.Add(MakeShareable(new FString(TEXT("Hi"))));
     Items.Add(MakeShareable(new FString(TEXT("Yo"))));
 
     ChildSlot
     .VAlign(VAlign_Fill)
     .HAlign(HAlign_Fill)
     [
          SNew(SOverlay)
          +SOverlay::Slot()
          .VAlign(VAlign_Bottom)
          .HAlign(HAlign_Center)
          .Padding(TAttribute(this, &SMyGameSlateHUDWidget::GetPadding))
          [
           SNew(STextBlock)
           .ShadowColorAndOpacity(FLinearColor::Black)
           .ColorAndOpacity(FLinearColor::Red)
           .ShadowOffset(FIntPoint(-1,1))
           .Text(this, &SMyGameSlateHUDWidget::GetSomeString)
 
          ]
          +SOverlay::Slot()
          .VAlign(VAlign_Center)
          .HAlign(HAlign_Left)
          [
 
               SNew(SListView<TSharedPtr<FString>>)
               .ItemHeight(24)
               .ListItemsSource(&Items)
               .HeaderRow
               (
                  SNew(SHeaderRow)
                  + SHeaderRow::Column("Name")
                  [
                    SNew(SBorder)
                    .Padding(5)
                    .Content()
                    [
                     SNew(STextBlock)
                     .Text(FString(TEXT("Name")))
                    ]
                  ]
                  + SHeaderRow::Column("Number") .DefaultLabel(FString(TEXT("Number")))
                  + SHeaderRow::Column("TextField") .DefaultLabel(FString(TEXT("Text Field")))
                  + SHeaderRow::Column("TextBlock") .DefaultLabel(FString(TEXT("Text Block")))
                  + SHeaderRow::Column("AddChild") .DefaultLabel(FString(TEXT("Add Child")))
               )
 
 
          ]
     ];
 
 }
 
 private:
 
 TArray<TSharedPtr<FString>> Items;
 
 }














more ▼

answered Mar 25 '14 at 03:26 AM

avatar image

Sarge STAFF
627 14 5 36

avatar image Ineni.Realtime Mar 25 '14 at 03:50 AM

Haha, thanks for the help Sarge, unfortunately this question's actually over a month old, but the way they migrated it from the beta site to this one means you can't tell, actually, you can't even tell who asked the question (It was me) which is a bit annoying.

Also, I shiver to look at my code from back then, nasty stuff. To be fair, I shiver at my code now, but only because there's so much of it. Slate's line count has a tendency to spiral out of control :p

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question