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"

How to initialize static TMap?

Header:

 // Use a struct because I need a TArray inside the TMap
 USTRUCT(BlueprintType)
 struct FWBList{..}

 static TMap<EStructureCategory, FWBList> WBMap;

CPP:

 // Statics in c++ MUST be initialized in order to prevent linker errors.
 TMap<EStructureCategory, FWBList> ABaseStructure::WBMap;
 // ... erm... How to initialize it now? TMap has no initializer and I'm now allowed to use .Add() here.

Problem is mainly that the TMap has no initializer. I cannot mark it const nor do I know how to initialize it. It's just a lookup dictionary. In worst case I could convert it from a TMap to a TArray and cast the enum to int32 (blueprints do not support uint8) all the time but this is kinda bad. A last resort would be to store it inside a singleton I guess.

Also I hope that it won't be garbage collected because I cannot mark it as a UPROPERTY.

Product Version: UE 4.18
Tags:
more ▼

asked Dec 20 '17 at 07:17 AM in C++ Programming

avatar image

Napoleonite
562 82 97 144

(comments are locked)
10|2000 characters needed characters left

1 answer: sort voted first

I got it to work by using a static function as the 'initializer'. Feels like a dirty workaround but until someone can give me something better it at least seems to work so far. I'm still not 100% sure though if it gets garbage collected but it's still not collected after 5 minutes so that is good:

 // CPP
 TMap<EStructureCategory, FWBList> ABaseStructure::WBMap = ABaseStructure::InitWB();

 // The static function
 TMap<EStructureCategory, FWBList> ABaseStructure::InitWB()
 {
     TMap<EStructureCategory, FWBList> retVal;

     retVal.Add(...)
     retVal.Add(...)
     retVal.Add(...)
     // Etc.

     return retVal;
 }
more ▼

answered Dec 20 '17 at 08:09 AM

avatar image

Napoleonite
562 82 97 144

avatar image DragonslayerOut Jan 02 '18 at 06:05 AM

Thanks for this. It's strange that TArray can be initialized as desired by TMap can't.

(comments are locked)
10|2000 characters needed characters left
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