Skip to content

Commit

Permalink
Exposed FindStateByString() to ZScript.
Browse files Browse the repository at this point in the history
This allows for using ZScript code to jump to different versions of states without using If/Else blocks or Switch cases.
  • Loading branch information
inkoalawetrust authored and madame-rachelle committed Jan 25, 2024
1 parent c8a7507 commit e1f585f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/p_states.cpp
Expand Up @@ -387,15 +387,15 @@ FState *FStateLabelStorage::GetState(int pos, PClassActor *cls, bool exact)

//==========================================================================
//
// State label conversion function for scripts
// State label conversion functions for scripts
//
//==========================================================================

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));
}

Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions wadsrc/static/zscript/actors/actor.zs
Expand Up @@ -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);
Expand Down

0 comments on commit e1f585f

Please sign in to comment.