Confusion over squirrel Class static attributes

Hi guys,

I’m seeing confusing behaviour when trying to write classes that are intended for singleton use (and don’t feature a constructor).

For example:
`class TestClass
{
_myNumber = 0;

function setMyNumber()
{
    server.log( _myNumber );
    _myNumber = 1;
}

}

TestClass.setMyNumber();`

Result:
I can read/log the number but I can’t set it, resulting in: “cannot set property of object of type ‘class’”. I get exactly the same result if I set it to static.

If I attempt to instance my class:
`class TestClass
{
_myNumber = 0;

function setMyNumber()
{
    server.log( _myNumber );
    _myNumber = 1;
}

}

local testClass = TestClass();
testClass.setMyNumber();`

Result:
This solves the issue, however, if I make _myNumber static to share it across instances, it can’t be found as a property. In C++ I would use the namespace i.e. TestClass::_myNumber but I can’t find the equivalent here?

I think perhaps I need a quick Squirrel-lang Classes / Static 101, can anyone advise?

Many thanks

Among Squirrel’s subtle differences from C++ is that static class members are read-only. If you’re thinking of using a Squirrel class as if it were a C++ namespace, what you might want instead is a Squirrel table:

`local TestClass ={
    _myNumber = 0,
    
    function setMyNumber()
    {
        server.log( _myNumber );
        _myNumber = 1;
    }
};

TestClass.setMyNumber();`

Peter

Hi Peter,

That clarifies a lot thank you!