From e1f585f6fd4d0f2cec95c21bc8232ba56cf28a78 Mon Sep 17 00:00:00 2001 From: inkoalawetrust <56005600+inkoalawetrust@users.noreply.github.com> Date: Thu, 25 Jan 2024 20:23:59 +0200 Subject: [PATCH] Exposed FindStateByString() to ZScript. This allows for using ZScript code to jump to different versions of states without using If/Else blocks or Switch cases. --- src/p_states.cpp | 13 +++++++++++-- wadsrc/static/zscript/actors/actor.zs | 1 + 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/p_states.cpp b/src/p_states.cpp index 49c3fe71f1..da8ef7f965 100644 --- a/src/p_states.cpp +++ b/src/p_states.cpp @@ -387,7 +387,7 @@ FState *FStateLabelStorage::GetState(int pos, PClassActor *cls, bool exact) //========================================================================== // -// State label conversion function for scripts +// State label conversion functions for scripts // //========================================================================== @@ -395,7 +395,7 @@ DEFINE_ACTION_FUNCTION(AActor, FindState) { PARAM_SELF_PROLOGUE(AActor); PARAM_INT(newstate); - PARAM_BOOL(exact) + PARAM_BOOL(exact); ACTION_RETURN_STATE(StateLabels.GetState(newstate, self->GetClass(), exact)); } @@ -407,6 +407,15 @@ DEFINE_ACTION_FUNCTION(AActor, ResolveState) ACTION_RETURN_STATE(newstate); } +// find state by string instead of label +DEFINE_ACTION_FUNCTION(AActor, FindStateByString) +{ + PARAM_SELF_PROLOGUE(AActor); + PARAM_STRING(newstate); + PARAM_BOOL(exact); + ACTION_RETURN_STATE(self->GetClass()->FindStateByString(newstate.GetChars(), exact)); +} + //========================================================================== // // Search one list of state definitions for the given name diff --git a/wadsrc/static/zscript/actors/actor.zs b/wadsrc/static/zscript/actors/actor.zs index d3395e6565..db83620db5 100644 --- a/wadsrc/static/zscript/actors/actor.zs +++ b/wadsrc/static/zscript/actors/actor.zs @@ -798,6 +798,7 @@ class Actor : Thinker native native bool CheckMissileRange(); native bool SetState(state st, bool nofunction = false); clearscope native state FindState(statelabel st, bool exact = false) const; + clearscope native state FindStateByString(string st, bool exact = false) const; bool SetStateLabel(statelabel st, bool nofunction = false) { return SetState(FindState(st), nofunction); } native action state ResolveState(statelabel st); // this one, unlike FindState, is context aware. native void LinkToWorld(LinkContext ctx = null);