Download

Object Numbering

Hi, I need to create a variable that numbers every object of this class when I place it on the stage, how can this be done?

If “place it on the stage” means spawning I think you can do something like that



void AYourActor::BeginPlay()
{
   Super::BeginPlay();
   AYourActor::ThisClassInstanceCounter++;
}


Where ThisClassInstanceCounter is a static int32 variable of AYourActor.

I searched the Internet, and I can’t find how static variables can be created in ue4, I get an build error

In the project I’m working I have a bunch of AActor class that use static variables and I have absolutely no compilation errors.
I think you cannot mark a static variable as UPROPERTY however you can create UFUNCTIONs to return values from static variables if you need to access them in Blueprint classes.

Maybe I’m writing something wrong, but when I try to assign a certain value to the static variable, I get an error



// Fill out your copyright notice in the Description page of Project Settings.  
#pragma once  
#include "CoreMinimal.h"
#include "Engine/TargetPoint.h"
#include "BotTargetPoint.generated.h"  
UCLASS()
class MYTESTPROJECT224_API ABotTargetPoint : public ATargetPoint
{    
    GENERATED_BODY()        
        ABotTargetPoint();
protected:    
    virtual void BeginPlay() override;  
public:    
    static int32 ThisClassInstanceCounter;
};




// Fill out your copyright notice in the Description page of Project Settings.  
#include "BotTargetPoint.h"  
ABotTargetPoint::ABotTargetPoint()
{  

}  
void ABotTargetPoint::BeginPlay()
{    
    Super::BeginPlay();    
    ABotTargetPoint::ThisClassInstanceCounter = 0;
}


Well static variables should be initialized at compile time, so your .cpp file should look like this:



  // Fill out your copyright notice in the Description page of Project Settings.  
#include "BotTargetPoint.h"    
**int32 ABotTargetPoint::ThisClassInstanceCounter = 0;**  

ABotTargetPoint::ABotTargetPoint()
{    
}  
void ABotTargetPoint::BeginPlay()
{    
Super::BeginPlay();    
ABotTargetPoint::ThisClassInstanceCounter = 0;
}


Yes, it seems to work, thanks!