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"

Office Holiday

Epic Games' offices will be on holiday from June 22nd to July 7th. During this period support will be limited. Our offices will reopen on Monday, July 8th. 

How to store local variables?

I'm making a turn based battle system and now I try to implement a creature queue, which should determine the order of activation of the creatures. I created a simple raw C++ struct which looks something like this:

 struct FQueueRecord
 {
      ACreature* Creature;
      int32 PointsToNextTurn;
 };

The question is how should I store the records inside a member TArray variable? Should the struct be a USTRUCT and ACreature* a UPROPERTY?

A FQueueRecord instance is created loaclly inside a method. After adding a new record, the queue is sorted based on the PointsToNextTurn variable. Would the use of a smart pointer be a good option? If so, which type should be used: std::unique_ptr, TUniquePtr or even TUniqueObj? Or is there any better solution?

(I considered a TMap for this, but it should be possible to add the creature more than once.)

Product Version: UE 4.20
Tags:
more ▼

asked Sep 11 '18 at 08:23 AM in C++ Programming

avatar image

BlackBird604
12 1 3 4

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

1 answer: sort voted first

Yes if you using UObjects you need USTRUCT and UPROPERTY in it and TArray need to be UPROPERTY too, so engine see those varbales and can automatically null invalid pointers of destroyed actors (ofcorse oyu will need to do struct cleaning in your case). Other then that it's needed to other thing, it's not dangerous with actors but if you got pure UObject you need to have them referenced in the way engine see them or else they be trashed.

I don't know what you mean by local varbales as the way you describe sound not like local variables (mean varbales in scope of function). If you indeed use local variable the all those doesn't matter as in scope of function engine won't able to delete those objects, even if you do that with destroy function they be just marked as pending kill and deleted after current frame, so in this case this is all optional.

All you need to know is that without all those U macros engine don't see those variables, and most impotently GC don't see it.

more ▼

answered Sep 12 '18 at 03:13 AM

avatar image

Shadowriver
36.6k 930 169 1111

avatar image BlackBird604 Sep 12 '18 at 08:48 AM

By local variables I mean something like this:

Header:

 USTRUCT()
 struct FQueueRecord
 {
     GENERATED_USTRUCT_BODY()
     
     UPROPERTY()
     ACreature* Creature;
     
     int32 PointsToNextTurn;
 };

 UCLASS()
 class UCreatureQueueComponent : public UActorComponent
 {
     GENERATED_BODY()

 public:
     void AddCreature(ACreature* Creature);
  
 private:
     UPROPERTY()
     TArray<FQueueEntry> Queue;
 };

Source:

 void UCreatureQueueComponent::AddCreature(ACreature* Creature)
 {
     FQueueRecord Record;
     Record.Creature = Creature;
     Record.PointsToNextTurn = Creature->GetDefaultPointsToNextTurn();
 
     // Should Record be here converted to some sort of a smart pointer
     // or is it ok to store it by value?
     // What if the Record variable was bigger?

     Queue.Add(Record);
 
     // ...
 }

What exactly do you mean by "struct cleaning"?

By the way, should every UObject* (including AActor*) be marked as a UPROPERTY even inside a UCLASS?

avatar image staticvoidlol Sep 13 '18 at 10:11 AM

I'll just answer your last question: Yes, all properties that are instances of UObject-derived classes must be decorated with UPROPERTY() otherwise they will be automatically garbage-collected by the engine and your game will crash when you try to reference them.

(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