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 can I check if a pointer to actor was freed

Lets say that I have a class of fireball which holds the fireball target (actor type).  In the tick function I am updating the fireball velocity direction to the target so if the target moves the fireball will keep track at it. The problem is - what if the actor was already destroyed and freed by the engine? lets say that the fireball was shot, and then the target died by another entity (whatever it is), so the destroy function has been called and the engine freed the actor. In this situation the fireball hold pointer (target) to a freed memory, and if it will try to call any of the actor functions the game will crash. How can I check if actor was freed?

I have thought about sending an event when the actor is dead before it gets destroyed, is it the best option?

Product Version: UE 4.14
Tags:
more ▼

asked Oct 11 '17 at 09:51 PM in C++ Programming

avatar image

Darkstorm1997
82 9 17 27

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

1 answer: sort voted first

You can use a TWeakObjectPtr to hold the weak actor reference in your code.

Is it specifically designed for this sort of thing:

  /***
   * 
   * FWeakObjectPtr is a weak pointer to a UObject. 
   * It can return nullptr later if the object is garbage collected.
   * It has no impact on if the object is garbage collected or not.
   * It can't be directly used across a network.
   *
   * Most often it is used when you explicitly do NOT want to prevent something from being garbage collected.
   **/
more ▼

answered Oct 11 '17 at 09:56 PM

avatar image

Jamie Dale STAFF
7.5k 177 67 305

avatar image Darkstorm1997 Oct 11 '17 at 10:04 PM

Thank you! Quick question - with TWeakObjectPtr, is there a case which I will want to use regular C++ pointer? or should I always use TWeakObjectPtr?

avatar image Jamie Dale STAFF Oct 11 '17 at 10:07 PM

TWeakObjectPtr is designed to avoid keeping an object alive and will be aware of when it's dead (or pending kill).

You still use a normal pointer (as a property) when you want a hard-reference that will keep an object alive... although that's only as far as the GC system is concerned, an object can always be forcibly marked as pending kill.

(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