How to get a BP placed in level from game mode?

Hi,

I have multiple BP’s using an interact interface placed in my level and want to execute them from the game mode bp.

How do I do this? (I already have interface events, but I need the target for that).

You could use an actor component overlap event or a line trace or any other type of trace near your character (depending if on the needed action) maybe in combination with an interact keypress if needed.

1 Like

The gamemode isn’t a good place for that. Getting a reference to an actor without searching the entire lists of actors is also not going to happen just by asking the level.

This is a common situation where you usually want to register actors into a certain system such as a GameInstanceSubsystem. Programming Subsystems | Unreal Engine 4.27 Documentation

You could for example make an CharacterSpawnerSubsystem which spawns and keeps track of characters, then simply request those characters when you need them. Technically you can also make that spawner just an actor or object but see which fits your needs best. For me it is usually the subsystem.

2 Likes

I’ll try this.
I basically want a telephone to ring at a certain point after other things have happened.
Will this also work with the game mode? That’s where the ‘other things’ happen.

Your gamemode is not really meant to do anything besides setting up some rules.

Game Mode and Game State | Unreal Engine 4.27 Documentation

You could still use it if you really want to. Optimally you want to create a new class with a simple task: Track events and trigger events conditionally. Write that class. To be able to access that class the common way would be to create a subsystem where you spawn and register that class, then you can simply do something like:

const UWorld* World = GetWorld();
const UGameInstance* GI = IsValid(World) ? World->GetGameInstance() : nullptr;
UEventTrackingSubsystem* EventTrackingSubsystem = IsValid(GI) ? GI->GetSubsystem<UEventTrackingSubsystem>() : nullptr;

if (!IsValid(EventTrackingSubsystem)) {
  return;
}

USomeTracker* SomeTracker = EventTrackingSubsystem->GetTracker("SomeTracker");
if (!IsValid(SomeTracker)) {
  return;
}

if (SomeTracker->HasEventHappened("")) {
  // DoStuff
}


Optimally make use of event dispatchers (delegates) so you don’t have to check if something happened or not but instead will simply be notified when something happens. There are videos on yt (matthew wadstein wtf is event dispatcher) and you can read about delegate usage on wikipedia “Observer pattern”

1 Like

Huh.
I thought that was what gamemodes were for.
What do I use as a replacement, such as for spawning AI, triggering other gameplay things?

I see, thanks.

How do I create a subsystem?
I’m using blueprints and I can’t find out how.

Programming Subsystems | Unreal Engine 4.27 Documentation

You can create a new blueprint asset and pick a subsystem as a parent class:

I am suprised not to see GameInstanceSubsystem in that list for me on UE5.1, see if it still fits your needs. Else creating one in c++ is easy as long as you know the basics of C.