Skip to content

Commit

Permalink
Allow WorldUnloaded events to know the next map name (if any).
Browse files Browse the repository at this point in the history
  • Loading branch information
Marisa Kirisame authored and coelckers committed Oct 1, 2021
1 parent 51faf8a commit 457f7c3
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
8 changes: 5 additions & 3 deletions src/events.cpp
Expand Up @@ -294,11 +294,11 @@ void EventManager::WorldLoaded()
}
}

void EventManager::WorldUnloaded()
void EventManager::WorldUnloaded(const FString& nextmap)
{
for (DStaticEventHandler* handler = LastEventHandler; handler; handler = handler->prev)
{
handler->WorldUnloaded();
handler->WorldUnloaded(nextmap);
}
}

Expand Down Expand Up @@ -629,6 +629,7 @@ DEFINE_FIELD_X(RenderEvent, FRenderEvent, Camera);

DEFINE_FIELD_X(WorldEvent, FWorldEvent, IsSaveGame);
DEFINE_FIELD_X(WorldEvent, FWorldEvent, IsReopen);
DEFINE_FIELD_X(WorldEvent, FWorldEvent, NextMap);
DEFINE_FIELD_X(WorldEvent, FWorldEvent, Thing);
DEFINE_FIELD_X(WorldEvent, FWorldEvent, Inflictor);
DEFINE_FIELD_X(WorldEvent, FWorldEvent, Damage);
Expand Down Expand Up @@ -769,13 +770,14 @@ void DStaticEventHandler::WorldLoaded()
}
}

void DStaticEventHandler::WorldUnloaded()
void DStaticEventHandler::WorldUnloaded(const FString& nextmap)
{
IFVIRTUAL(DStaticEventHandler, WorldUnloaded)
{
// don't create excessive DObjects if not going to be processed anyway
if (isEmpty(func)) return;
FWorldEvent e = owner->SetupWorldEvent();
e.NextMap = nextmap;
VMValue params[2] = { (DStaticEventHandler*)this, &e };
VMCall(func, params, 2, nullptr, 0);
}
Expand Down
5 changes: 3 additions & 2 deletions src/events.h
Expand Up @@ -78,7 +78,7 @@ class DStaticEventHandler : public DObject // make it a part of normal GC proces

//
void WorldLoaded();
void WorldUnloaded();
void WorldUnloaded(const FString& nextmap);
void WorldThingSpawned(AActor* actor);
void WorldThingDied(AActor* actor, AActor* inflictor);
void WorldThingGround(AActor* actor, FState* st);
Expand Down Expand Up @@ -144,6 +144,7 @@ struct FWorldEvent
// for loaded/unloaded
bool IsSaveGame = false;
bool IsReopen = false;
FString NextMap;
// for thingspawned, thingdied, thingdestroyed
AActor* Thing = nullptr; // for thingdied
AActor* Inflictor = nullptr; // can be null - for damagemobj
Expand Down Expand Up @@ -232,7 +233,7 @@ struct EventManager
// called right after the map has loaded (approximately same time as OPEN ACS scripts)
void WorldLoaded();
// called when the map is about to unload (approximately same time as UNLOADING ACS scripts)
void WorldUnloaded();
void WorldUnloaded(const FString& nextmap);
// called around PostBeginPlay of each actor.
void WorldThingSpawned(AActor* actor);
// called after AActor::Die of each actor.
Expand Down
6 changes: 3 additions & 3 deletions src/g_level.cpp
Expand Up @@ -468,7 +468,7 @@ void G_InitNew (const char *mapname, bool bTitleLevel)

// did we have any level before?
if (primaryLevel->info != nullptr)
staticEventManager.WorldUnloaded();
staticEventManager.WorldUnloaded(FString()); // [MK] don't pass the new map, as it's not a level transition

if (!savegamerestore)
{
Expand Down Expand Up @@ -701,10 +701,10 @@ void FLevelLocals::ChangeLevel(const char *levelname, int position, int inflags,
for (auto Level : AllLevels())
{
// Todo: This must be exolicitly sandboxed!
Level->localEventManager->WorldUnloaded();
Level->localEventManager->WorldUnloaded(nextlevel);
}
// [ZZ] unsafe world unload (changemap != map)
staticEventManager.WorldUnloaded();
staticEventManager.WorldUnloaded(nextlevel);
unloading = false;

STAT_ChangeLevel(nextlevel, this);
Expand Down
2 changes: 2 additions & 0 deletions wadsrc/static/zscript/events.zs
Expand Up @@ -15,6 +15,8 @@ struct WorldEvent native play version("2.4")
native readonly bool IsSaveGame;
// this will be true if we are re-entering the hub level.
native readonly bool IsReopen;
// for unloaded, name of next map (if any)
native readonly String NextMap;
// for thingspawned/thingdied/thingdestroyed/thingground
native readonly Actor Thing;
// for thingdied. can be null
Expand Down

0 comments on commit 457f7c3

Please sign in to comment.