Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Add ability to initialize float[3] as NULL_VECTOR #888

Open
6 tasks done
Rainyan opened this issue Jul 12, 2023 · 3 comments
Open
6 tasks done

[Feature Request] Add ability to initialize float[3] as NULL_VECTOR #888

Rainyan opened this issue Jul 12, 2023 · 3 comments
Labels
bug compiler Problems with the compiler parsing source code.

Comments

@Rainyan
Copy link

Rainyan commented Jul 12, 2023

Help us help you

  • I have checked that my issue doesn't exist yet.
  • I have tried my absolute best to reduce the problem-space and have provided the absolute smallest test-case possible.
  • 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

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!
@Rainyan Rainyan changed the title [Feature Request] Initialize float[3] as NULL_VECTOR [Feature Request] Ability to initialize float[3] as NULL_VECTOR Jul 12, 2023
@Rainyan Rainyan changed the title [Feature Request] Ability to initialize float[3] as NULL_VECTOR [Feature Request] Add ability to initialize float[3] as NULL_VECTOR Jul 12, 2023
@peace-maker peace-maker transferred this issue from alliedmodders/sourcemod Jul 14, 2023
@peace-maker peace-maker added enhancement compiler Problems with the compiler parsing source code. labels Jul 14, 2023
@peace-maker
Copy link
Member

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.

@Rainyan
Copy link
Author

Rainyan commented Jul 14, 2023

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 😅

@dvander dvander added bug and removed enhancement labels Jul 30, 2023
@dvander
Copy link
Member

dvander commented Jul 30, 2023

(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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug compiler Problems with the compiler parsing source code.
Projects
None yet
Development

No branches or pull requests

3 participants