Announcement

Collapse
No announcement yet.

Pointer to C++ Class invalid after start, Accessed None trying to read property

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

  • Pointer to C++ Class invalid after start, Accessed None trying to read property

    Hello guys, I have a problem that I don't quite understand.
    I have a blueprint derived from a C++ class that is something like this

    Code:
    // FirstPlayer.h
    
    UCLASS()
    class MYPROJECT_API AFirstPlayer : public ACharacter
    {
      [...]
    public:
      UPROPERTY(BlueprintReadOnly)
      UStatistics* Statistics;
    }
    
    // FirstPlayer.cpp
    
    AFirstPlayer::AFirstPlayer()
    {
      // Set this character to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
      PrimaryActorTick.bCanEverTick = true;
    
      Statistics = CreateDefaultSubobject<UProgressiveStatistics>(TEXT("Statistics"));  
    }
    Code:
    UStatistics
    is a C++ class derived from a UObject. When I try to use the variable Statistics in the event graph like this

    Click image for larger version

Name:	Capture.PNG
Views:	1
Size:	48.0 KB
ID:	1359098
    I receive the error "Accessed None trying to read property [...]". Moreover, using IsValid returns always false.
    What am I missing here? Do I need to specify something to the GC in order for it not to delete the variable?

  • #2
    Yeah, GC is fully automated for Actors, but not for UObjects and you have to do some legwork for proper behavior. The easiest way is just to set variable as UPROPERTY and GC will automatically "know" how to handle the object.
    I recommend this wiki article, it explains quite well what is going on with Uobjects and GC
    SuperGrid: Marketplace Page | Feedback Thread | Demo | Website
    Level design and prototyping for newbies

    Comment


    • #3
      Ok, but as you can see here

      Code:
      UPROPERTY(BlueprintReadOnly)
      UStatistics* Statistics;
      the variable is actually a UPROPERTY. That's why I have some concerns...

      Comment


      • #4
        Hey! Could you verify that the object is actually created?
        Use
        Code:
         Statistics = CreateDefaultSubobject<UProgressiveStatistics>(TEXT("Statistics"));
        check(Statistics);
        
         //also you can log something here and check if it exists in your logs:
        
        UE_Log(LogClass, Log, TEXT("Statistics object created"));
        Just to check if you are missing something simple! Because I don't think GC is the issue here...
        Check my User Interface Kit here: https://forums.unrealengine.com/show...-Interface-Kit
        A WIP Hex Planet Generation: https://forums.unrealengine.com/show...ral-Generation

        Comment


        • #5
          The check macro doesn't trigger anything. I can print the Log after the check.

          If inside AFirstPlayer class I do something like this

          Code:
              class UStatistics* Stastistics;
          
          protected:
              // Called when the game starts or when spawned
              virtual void BeginPlay() override;
          
          public:    
          
              UFUNCTION(BlueprintCallable)
              class UStatistics* GetStat() const
              {
                  return Stastistics;
              }
          The blueprint works without problems. FYI I didn't change how I'm initializing the Statistics variable, always with CreateDefaultSubobject function.
          I can call all functions from the event graph of the blueprint without any problem. I really don't understand why this is happening.

          Comment


          • #6
            I still have this issue.
            I can get it work with the get function, but if I set the variable as UPROPERTY(BlueprintReadOnly), after I stop the game pressing ESC I get the error reported in the first post.

            I would really like to understand what is going on here, a help would be much appreciated.

            Comment

            Working...
            X