I am trying to understand the purpose of World Context in relation to Blueprint Function Libraries.
If I create an empty Blueprint Function Library, create a new function called “Foo”, leave it empty, and try and use “Foo” in certain classes, it will require me to pass in a World Context object to “Foo”. Now “Foo” doesn’t interact with the game world, or any objects for that matter (in this example its a no-op) and the same method copied directly into the calling class works without requiring a World Context.
What is the purpose/need to have World Context required in Blueprint Function Libraries; and is there a way around this?
When function libraries need a reference of the world sometimes for some tasks…
for example get all actors with some tag…the function need to know the world where those actors are…or a delta time, etc.
There is a way to tell the library function to use the actor that is calling it as world context object so the world context pin does not appears in the blueprint node and is adding the meta WordlContext.
Example:
UFUNCTION(BlueprintCallable, BlueprintPure, Category = "My Blueprint Library" , DisplayName = "get delta seconds in a function", meta = (WorldContext = "WorldContext") )
static float GetDeltaSecs(const UObject* myWorldContext)
{
if (myWorldContext)
{
return myWorldContext->GetWorld()->GetDeltaSeconds();
}
else { return -1; }
}
Unfortunately that doesn’t really answer the question; it just hand waves and says don’t worry about it; but often I use Blueprint Function Libraries from Blueprints that do not have a world context, and shouldn’t need one.
Imagine I have a simple Blueprint Function Library that has a single function called “Add”. It takes two floats, and returns the value of the two added together. I wouldn’t expect this to need a reference to the world context.
I would love for someone to follow up on this.
I’ve created several BGLs in our project and today I created a new function where World Context is being automatically added this way.
It’s the first time I’ve ever seen this behavior.
Here’s and example:
One function takes an input actor and gets equipment data directly from the actor and returns currently equipped weapon. For some reason the function auto generates a World Context input.
Another function spawns an object, which would potentially require a World context, but the function doesn’t auto generate such an input. However, there still seems to be an auto generated World Context VARAIBLE.