How to create a static variable.

Static variables aren’t bound to an instance, so can’t be set in the constructor initialisation list.

You’d set the default value where you define (the header contains the declaration) the static variable (typically inside the cpp file of the class containing it), eg)

// .h
class Wooble
{
public:
    Wooble();

protected:
    static int32 MyVariable;
};

// .cpp
int32 Wooble::MyVariable = 0;

Wooble::Wooble()
{
}

The only exception to this is static const integers, which can have their value specified at the same place as they’re declared.

// .h
class Wooble
{
public:
    Wooble();

protected:
    static const int32 MyVariable = 0;
};

Hi I am trying to create a static integer in a class but I am getting errors on declaring it.

I can’t initialize it in the constructor of the class.

.h file
static int32 NumberOfProjectiles;

It shows the error

1>d:\ue4\new folder\myproject\source\myproject\ShotgunProjectile.h(31): error C2864: ‘AShotgunProjectile::NumberOfProjectiles’ : a static data member with an in-class initializer must have non-volatile const integral typeD:\ue4\New folder\MyProject\Source\MyProject\ShotgunProjectile.h(31) : error C2864: ‘AShotgunProjectile::NumberOfProjectiles’ : a static data member with an in-class initializer must have non-volatile const integral type

How do i give it a value?

Ok thanks…

Can static variable values be changed in a blueprint defaults?

Some thing that i accessed by Blueprints? Like we use Editanywhere and Blueprintreadandwrite for an Uproperty?

1 Like