Files
fs2open.github.com/code/scripting/api/objs/species.cpp
T
Goober5000andClaude Fable 5.1 37b0b6ea82 Standardize isValid across the Lua API by making the types auto-generate it (#7761)
* Automatically generate the isValid() Lua API function for handle types

If an ADE object's stored type has an isValid() member, ade_obj now registers an isValid function for it automatically.  An explicit ADE_FUNC(isValid, ...) declared after the ADE_OBJ replaces the generated one, so types with special semantics (e.g. sound) keep their own.  The 67 hand-written functions that merely delegated to the member are removed, and 26 types that previously exposed no isValid() gain one.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

* Add ADE_OBJ_VALIDATOR for index-based Lua handles

Int-index handles (shipclass, weaponclass, team, etc.) can now declare their validity predicate with a single ADE_OBJ_VALIDATOR or ADE_OBJ_VALIDATOR_RANGE line after the ADE_OBJ.  The registered validator drives the generated isValid() function and is also honored by ade_set_args when the mod table asks for nil instead of invalid objects, which previously never applied to these handles.  The 19 hand-written range-check functions are replaced, and loadout_ship, default_primary, default_secondary, and support_rearm_pool_team gain isValid().  Add a validator to HudGauge while we're at it.

Also fully qualify the scripting namespace in LuaConvert.h so its templates can be instantiated from the unit tests.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

---------

Co-authored-by: Claude Fable 5.1 <[email protected]>
2026-09-08 10:50:53 -04:00

41 lines
939 B
C++

//
//
#include "species.h"
#include "species_defs/species_defs.h"
extern int Species_initted;
namespace scripting {
namespace api {
//**********HANDLE: Species
ADE_OBJ(l_Species, int, "species", "Species handle");
ADE_OBJ_VALIDATOR(l_Species, idx, Species_info.in_bounds(idx));
ADE_VIRTVAR(Name, l_Species, "string", "Species name", "string", "Species name, or empty string if handle is invalid")
{
if(!Species_initted)
return ade_set_error(L, "s", "");
const char* s = nullptr;
int idx;
if(!ade_get_args(L, "o|s", l_Species.Get(&idx), &s))
return ade_set_error(L, "s", "");
if(idx < 0 || idx >= (int)Species_info.size())
return ade_set_error(L, "s", "");
if(ADE_SETTING_VAR && s != nullptr) {
auto len = sizeof(Species_info[idx].species_name);
strncpy(Species_info[idx].species_name, s, len);
Species_info[idx].species_name[len - 1] = 0;
}
return ade_set_args(L, "s", Species_info[idx].species_name);
}
}
}