When are you calling your function? It needs to be around begin play for the world to exist.
.h
#pragma once
#include "CoreMinimal.h"
#include "Subsystems/WorldSubsystem.h"
#include "MyWorldSubsystem.generated.h"
UCLASS()
class YOUR_API UMyWorldSubsystem : public UWorldSubsystem
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintCallable)
void CallFunc();
};
.cpp
#include "MyWorldSubsystem.h"
void UMyWorldSubsystem::CallFunc()
{
// GetWorld() returns null here?!
if(!GetWorld())
{
GEngine->AddOnScreenDebugMessage(1, 4, FColor::Red, "World is null");
} else
{
GEngine->AddOnScreenDebugMessage(1, 4, FColor::Green, "World is ok");
}
}
Tester class
.h
just basic actor header
.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "WorldTester.h"
#include "MyWorldSubsystem.h"
// important
void AWorldTester::BeginPlay()
{
Super::BeginPlay();
UMyWorldSubsystem * ws = GetWorld()->GetSubsystem<UMyWorldSubsystem>();
ws->CallFunc();
}
Gives info