Announcement

Collapse
No announcement yet.

Object Numbering

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    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?

    #2
    If "place it on the stage" means spawning I think you can do something like that
    Code:
    void AYourActor::BeginPlay()
    {
       Super::BeginPlay();
       AYourActor::ThisClassInstanceCounter++;
    }
    Where ThisClassInstanceCounter is a static int32 variable of AYourActor.

    Comment


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

      Comment


        #4
        Originally posted by whitegоld View Post
        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.

        Comment


          #5

          Maybe I’m writing something wrong, but when I try to assign a certain value to the static variable, I get an error
          Code:
          // 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;
          };
          Code:
          // 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;
          }
          Click image for larger version  Name:	-1.PNG Views:	0 Size:	7.3 KB ID:	1694620
          Last edited by whitegоld; 12-05-2019, 10:44 AM.

          Comment


            #6
            Well static variables should be initialized at compile time, so your .cpp file should look like this:
            Code:
              // 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;
            }

            Comment


              #7
              Yes, it seems to work, thanks!

              Comment

              Working...
              X