Verse error: The assignment's left hand expression type `component` cannot be assigned to(3509)

Hey, guys!

I’m trying to implement a simple map to hold instances in verse. But I got a subject error, and can’t figure out how to fix it. May somebody explain what is wrong in my code?

game_model<public> := module:
    component_type<public> := enum {Player}

    component<public> := class:
        Type<public> : component_type

    entity<public> := class:
        Components : [component_type]component = map{}

        AddComponent(Component:component)<decides>: void = 
                                   # error here
                                   # |
                                   # v
            set Components[Component.Type] = Component

The assignment’s left hand expression type component cannot be assigned to(3509)

3 Likes

This error is because you are trying to change a variable that has been declared as a constant. Add “var” in front of Components when you initialize it.

4 Likes

It works, thank you! However, it seems a little strange to me. In this case, I didn’t create a new map, I just changed it. In TypeScript, for example, the logic is different for this case.

In this case, I didn’t create a new map

In Verse maps are immutable. In order to update them you need to mark them as var, then set the map using the set syntax. What is actually happening in this code (set Components[Component.Type] = Component) is that a new map is being created under the hood with Component.Type => Component added/modified from the original map.

1 Like

Aha, thank you for the explanation; one thing has become clear, with a million more to go!
P.S. You guys should train ChatGPT on your datasets to teach it these kind verses tricks; it would make things easier for all of us :slight_smile:

1 Like

Yea Copilot seems to barely know Verse in January 2025. lol I had a similar issue, unfortunately that didn’t solve it for me.