As the subject line says, what is the difference between GetWorld() and GEngine->GetWorldContexts()[0].World() ? But also is there any reason to use one over the other?
The GetWorld() function returns the world the actor/component is in that you call the function on. It is quick as it returns a cached ref.
The other function will return the first world in the engine WorldList. It requires that the WorldList is populated and requires care that you are calling the correct world… normally is fine to use this, but may fail in special cases.
I tried using GEngine->GetWorldContexts()[0].World(), but noticed it was working in Standalone, but failing in PIE. Does PIE interact with the WorldList differently than Standalone?
You may end up generating and using multiple worlds in PIE, esp testing multiplayer… this may be causing issues.
It looks like GEngine->GetWorldContexts()[0].World() is generally used in UObjects or struct functions where the Outer (parent) isn’t available as it is in Levels, Actors, components, etc. You probably want to avoid this function if you can. You can see in the codebase that they use the GWorld global variable when WITH_EDITOR to possibly get around the issue you point out. Keep in mind the comments for GWorld warns: “Use of this pointer should be avoided whenever possible.”.
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.