Skip to content

Commit

Permalink
Func to get a ship's persona directly (#6497)
Browse files Browse the repository at this point in the history
* func to get a ship's persona directly

* virtvar instead of func

* get persona index

* lua index
  • Loading branch information
MjnMixael authored Jan 5, 2025
1 parent fa8e584 commit 297a938
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
19 changes: 19 additions & 0 deletions code/scripting/api/objs/message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,25 @@ ADE_VIRTVAR(Name, l_Persona, "string", "The name of the persona", "string", "The
return ade_set_args(L, "s", Personas[idx].name);
}

ADE_VIRTVAR(Index, l_Persona, nullptr, nullptr, "number", "The index of the persona")
{
int idx = -1;

if (!ade_get_args(L, "o", l_Persona.Get(&idx)))
return ade_set_args(L, "i", -1);

if (Personas.empty())
return ade_set_args(L, "i", -1);

if (!SCP_vector_inbounds(Personas, idx))
return ade_set_args(L, "i", -1);

if (ADE_SETTING_VAR)
LuaError(L, "Setting index is not supported");

return ade_set_args(L, "i", idx + 1);
}

ADE_FUNC(isValid, l_Persona, NULL, "Detect if the handle is valid", "boolean", "true if valid, false otherwise")
{
int idx = -1;
Expand Down
25 changes: 24 additions & 1 deletion code/scripting/api/objs/ship.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "object/objectdock.h"
#include "parse/parselo.h"
#include "playerman/player.h"
#include "scripting/api/objs/message.h"
#include "ship/ship.h"
#include "ship/shipfx.h"
#include "ship/shiphit.h"
Expand Down Expand Up @@ -831,7 +832,7 @@ ADE_VIRTVAR(Team, l_Ship, "team", "Ship's team", "team", "Ship team, or invalid
return ade_set_args(L, "o", l_Team.Set(shipp->team));
}

ADE_VIRTVAR(PersonaIndex, l_Ship, "number", "Persona index", "number", "The index of the persona from messages.tbl, 0 if no persona is set")
ADE_VIRTVAR_DEPRECATED(PersonaIndex, l_Ship, "number", "Persona index", "number", "The index of the persona from messages.tbl, 0 if no persona is set", gameversion::version(25, 0), "Deprecated in favor of Persona")
{
object_h *objh;
int p_index = -1;
Expand All @@ -849,6 +850,28 @@ ADE_VIRTVAR(PersonaIndex, l_Ship, "number", "Persona index", "number", "The inde
return ade_set_args(L, "i", shipp->persona_index + 1);
}

ADE_VIRTVAR(Persona, l_Ship, "persona", "The persona of the ship, if any", "persona", "Persona handle or invalid handle on error")
{
object_h* objh;
int idx = -1;
if (!ade_get_args(L, "o|o", l_Ship.GetPtr(&objh), l_Persona.Get(&idx)))
return ade_set_error(L, "o", l_Persona.Set(-1));

if (!objh->isValid())
return ade_set_error(L, "o", l_Persona.Set(-1));

ship* shipp = &Ships[objh->objp()->instance];

if (ADE_SETTING_VAR && idx > -1) {
shipp->persona_index = idx;
}

if (!SCP_vector_inbounds(Personas, shipp->persona_index))
return ade_set_args(L, "o", l_Persona.Set(-1));
else
return ade_set_args(L, "o", l_Persona.Set(shipp->persona_index));
}

ADE_VIRTVAR(Textures, l_Ship, "modelinstancetextures", "Gets ship textures", "modelinstancetextures", "Ship textures, or invalid shiptextures handle if ship handle is invalid")
{
object_h *sh = nullptr;
Expand Down

0 comments on commit 297a938

Please sign in to comment.