[Feature Request] Add ability to initialize float[3] as NULL_VECTOR
Help us help you
- [X] I have checked that my issue doesn't exist yet.
- [X] I have tried my absolute best to reduce the problem-space and have provided the absolute smallest test-case possible.
- [X] I can always reproduce the issue with the provided description below.
Environment
- Operating System version: Windows 10 x64
- Game/AppID (with version if applicable): not applicable
- Current SourceMod version: 1.12
- Current SourceMod snapshot: 1.12.0.7030
- Current Metamod: Source snapshot: not applicable
- [X] I have updated SourceMod to the latest version and it still happens.
- [X] I have updated SourceMod to the latest snapshot and it still happens.
- [X] I have updated SourceMM to the latest snapshot and it still happens.
Description
Could we have the ability to initialize vectors (i.e. float arrays of size 3) as NULL_VECTOR?
Using NULL_VECTOR to signify the variable hasn't yet received its proper value (or the proper value could not be calculated) is often convenient for vectors, since the default-initialized value of { 0.0, 0.0, 0.0 } (or other magic values) could overlap with a real value, and the IsNullVector check also spares the programmer from having to check all three of the X,Y,Z elements when that's not really what they semantically meant.
Problematic Code (or Steps to Reproduce)
Instead of:
void foo()
{
float vec[3];
vec = NULL_VECTOR;
}
one could write:
// this does not compile
void foo()
{
// with array size specified
float v1[3] = NULL_VECTOR; // "error 047: array sizes do not match, or destination array is too small"
// or with implicit array size, even
float v2[] = NULL_VECTOR; // "error 047: array sizes do not match, or destination array is too small"
}
This gets particularly annoying when wanting to initialize a static variable in global scope as NULL_VECTOR:
// some_include.inc
static float my_encapsulated_vec[3];
// because we can't touch "my_encapsulated_vec" from the main .sp file
void InitTheThingsOfThisInclude()
{
my_encapsulated_vec = NULL_VECTOR;
}
// plugin.sp
#include <some_include>
public void OnPluginStart()
{
InitTheThingsOfThisInclude()
}
when all of this could be a one-liner initialization, as with most primitive types:
// some_include.inc
static float my_encapsulated_vec[3] = NULL_VECTOR; // yay!
Related #490 #369
This specific usecase of assigning NULL_VECTOR to be used with IsNullVector won't work though. since IsNullVector checks if the passed parameter is a reference to the global NULL_VECTOR pubvar. Copying NULL_VECTOR to some other variable won't pass that check.
Ah, I just incorrectly assumed I would be able to use NULL_VECTOR like this. I guess the feature request still stands of wanting to use NULL_VECTOR assignment together with IsNullVector like this, although feel free to close as invalid, if you see fit :sweat_smile:
(1) This assignment should work. I'm keeping the bug open for that.
(2) As @peace-maker explained, it wouldn't do what you want. SM could consider populating NULL_VECTOR with NaN values, and changing null vector tests to look at that. It could be a breaking change though.
This is a very complicated issue to fix given that the language doesn't have nullable types.