Vector2.sum() - Mtax-Development/GML-OOP GitHub Wiki

Argument

Name
Type
Optional
Description
value?
Yes
The value to add to the x and y properties.

Returns

{real|Vector2} | On error: {undefined}

Description

Performs mathematical addition to return a sum of a calculation involving the x and y properties without modifying their values in the constructor. The type of the calculation depends on the data type of the value argument:
• None or {undefined}: The sum of the x and y properties of the constructor calling this method will be returned as a {real} number.
{real}: The sum of the x and y properties and the value argument will be returned in a new {Vector2}.
{Vector2}: The values of x and y properties will be added to the values of the same properties of the value argument and returned in a new {Vector2}.

Vector2.add() is a similar method that modifies the properties of the constructor calling it instead of returning a separate constructor.


Example
Code
constructor = new Vector2(5, 50);
result = [constructor.sum(), constructor.sum(-5), constructor.sum(constructor)];

Result

[55, Vector2(x: 0, y: 45), Vector2(x: 10, y: 100)]

Explanation
• Line 1: A Vector2 is constructed.
• Line 2: This method is called with three different argument types and the results returned by these calls are saved in an array.
The first call returns a {real} number for the sum of the x and y properties of the constructor, resulting in the following calculation: 5 + 50 = 55.
The second call returns a {Vector2} for the sum with the specified {real} number, resulting in the following calculations: 5 + (-5) = 0, 50 + (-5) = 45.
The third call returns a {Vector2} for the sum of its respective properties, resulting in the following calculations: 5 + 5 = 10, 50 + 50 = 100.
• Result: The properties of the constructor calling this method were not modified. The results of three different calculations performed on its values were saved in an array. The first is a {real} number, the remaining two are separate {Vector2}.

⚠️ **GitHub.com Fallback** ⚠️