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"

GetWorld() returns NULL in ActorComponent startup callbacks

There is a pattern that recurs multiple times in our codebase: a component type registers itself with a singleton manager actor once at the time it spawns (for example, a "TargetableComponent" calls the game state to add itself to a dynamic array of "targetable actors").

Ideally this would be a simple call inside the "OnActivate" or "OnRegistered" callbacks in the component class. However, these don't work for actors that are hand-placed in the level, because GetWorld() returns NULL at the time that the callbacks are invoked.

Right now we have a horrible hacky work-around: we set up the component to generate ticks, register the component with the singleton within the TickComponent method, and then turn off ticks forever. This is ugly.

It is notable that BeginPlay on the actor does the right thing at the actor level -- GetWorld() always seems to return the non-NULL, whether for hand-placed actors or actors spawned by code or blueprint later. So an actor that needs to register with a singleton can do so in a single codepath inside of BeginPlay.

Is there another Component callback I should be implementing other than OnActivate or OnRegistered that behaves properly? Or do I need to continue in my filthy work-around?

Product Version: Not Selected
Tags:
more ▼

asked Mar 31 '15 at 07:28 PM in C++ Programming

avatar image

disla
92 5 9 13

avatar image marineuac Mar 28 '19 at 09:32 PM

bump 4.21.2

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

1 answer: sort voted first

Does your singleton rely on a valid World instance? Is it static or do you have a static accessor? Can you decouple the reliance on the World from your singleton?

Have you tried another area of the UObject/AActor lifecycle? Perhaps PostSpawnInitialize, maybe?

more ▼

answered Apr 03 '19 at 09:46 AM

avatar image

Jawdy
361 2 4 6

(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