#How does "set" work?

5 messages · Page 1 of 1 (latest)

icy shuttle
#

Hey!
Regarding the first line:

var cell := Vector2.ZERO:

This just initializes the variable. The ":=" means the type of the right site is inferred.
You could also write this which is basically the same thing.

var cell: Vector2 = Vector2.ZERO:

Now to the setter:
You don`t really need it but it can be nice to have. Basically you write logic there that you want to run each time you assign a value to your variable. It gets automatically called when you do stuff like this.

cell = Vector2(new_x,new_y)

The setter gets called after this.

Like the comment says here the setter makes sure that the cell variable is always inside the grid.
Instead of using a setter you can alternatively write this every time you assign a new value to your variable which does the same thing:

cell = grid.grid_clamp(Vector2(new_x,new_y))
#

If you have a setter then in the line where you assign a new value to your variable

cell = Vector2(new_x,new_y)

The right site Vector2(new_x,new_y) is what the value variable in your setter is

glacial galleon
#

But in theory it updates the value. Here it gives a value which is equal to zero here.

func _reinitialize() -> void:
    _units.clear()

    for child in get_children():
        var unit := child as Unit
        if not unit:
            continue
        _units[unit.cell] = unit
#

I mean, I'm trying to understand how it sets the base value for objects. And here there's only "set" which updates relative to zero.
I feel like half an idiot, half a dinosaur, roaring in bewilderment.

icy shuttle
#

I think i might not understand your exact problem yet sorry^^ But we´ll figure it out.
So in this code where exactly is the problem with the "set" you have?