Getting and setting individual x and y values in Vector2

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By WayfairSalesRep

I have the following code:

var Location = Vector2(64, 64)

if Location[0] > 911.0:
		Location[0] = 64.0
		Location[1] = Location[1] + 64.0
		Location = Location[0] + 112.0
	return Location

I have also tried this with using .x instead of [0] and .y instead of [1].

I get the following error:

Invalid get index ‘0’ (on base: ‘float’).

or if I use .x instead of [0] I get:
Invalid get index ‘x’ (on base: ‘float’).

So that makes me think that it is somehow a float, so I try getting rid of the index altogether and then it says:
Invalid operands ‘Vector2’ and ‘float’ in operator >

How do I check what the x value is and then change it?

:bust_in_silhouette: Reply From: jgodfrey

You can use either Location[0] and Location[1] or Location.x and Location.y.

Specifically, your problem is here:

Location = Location[0] + 112.0

There, you’re trying to set Location (which is a Vector2) to a floating point value. I assume you mean Location[0] += 112

Really, you can do the above like this:

var Location = Vector2(64, 64)
if Location.x > 911.0:
	Location.x = 64
	Location.y += 64
	Location.x += 112

Holy nuts, of course. That seems super obvious now. Thanks for your help.

WayfairSalesRep | 2022-08-08 16:49