Announcement

Collapse
No announcement yet.

Some basic questions about using pointers

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

    Some basic questions about using pointers

    I have a few questions regarding use of pointers..

    1. If I store an actor in a UPROPERTY() pointer say
    Code:
    ABaseActor*
    and the original actor get deleted, so what happens to the pointer after the actor is deleted?

    2. Pointers point to only one specific instanced class. If I want to store a specific class which is not instanced, is it possible to do so? Like its information are accessible no matter what happens to the actor. Like how normal variables work.


    I tried something like this as a test. It got compiled but when I tried to access any variables inside the non-pointer class, and got the crash(I accessed it inside the BeginPlay function)

    Code:
    AGeneratorClass *Generator;
    Generator = GetWorld()->SpawnActor<AGeneratorClass>(PlanetGenerationClass);
    AGeneratorClass Ga = *Generator;
    int32 va = Ga.SomeVariable; //Crash
    3. Is it possible to access the variables inside a class without creating an instance of it?
    Suppose I got a class
    Code:
    TSubclassOf<class APlanetBase> TheClass;
    I want to access a variable of the class. Is it possible for me to do so in any way other than spawning the actor in the world. Creating a pointer to the actor and accessing the value through that pointer?

    4. Other than the normal UPROPERTY() pointer, there are many other types of smart pointer given by Epic. https://docs.unrealengine.com/latest...ary/index.html
    I have never used them yet. Where are exactly should I use pointers like TSharedRef?

    #2
    Hi,
    Pointers become invalid after the object is deleted/garbage collected. It's never safe to access them after that point. It's safer to wrap the pointer to a transient object in a TWeakObjectPtr container. You can use it almost exactly like the raw pointer, except it has an IsValid method which will let you know if its instances is garbage collected or gone. E.g. I keep a reference to a scrolling waypoint instance from the level (blueprint editable) this way:
    Code:
    	UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = ScrollMovement)
    	TWeakObjectPtr<AScrollWaypoint> NextWaypoint;


    The only variables that can be accessed inside a class without an instance of the class actually existing are static variables. These are shared between all instances of the class, globally. They're often used for statically initialized things and sometimes sharing data between all instances (but that's a bit dangerous if ticking access is multithreaded; best to use events/delegates/the usual channels).

    I think there is a way to grab a handle to an actors class type so that new instances of that class can be spawned; the UClass returned from GetClass() on an actor can be passed into World::SpawnActor. You can make a safe general pointer to a UClass with TSubclassOf, e.g. a class to hold anything derived from APawn TSubclassOf<class APawn> SomePawnChildClass;

    I ran into this when checking out how to spawn blueprint actors from c++ (you load the BP and get a reference to their UClass).

    Comment


      #3
      Something useful about classes that might help with point 3 - every UClass automatically has an instance of it created. So if you've got a UClass* (or TSubclassOf), you can call GetDefaultObject on it and it'll return that default instance. Then you can access variables and stuff from it (I use it quite often for data objects). Don't go editing it though. I'm not sure what would happen but I'm pretty sure it would be bad.

      Comment


        #4
        Originally posted by Stormharrier View Post
        Something useful about classes that might help with point 3 - every UClass automatically has an instance of it created. So if you've got a UClass* (or TSubclassOf), you can call GetDefaultObject on it and it'll return that default instance. Then you can access variables and stuff from it (I use it quite often for data objects). Don't go editing it though. I'm not sure what would happen but I'm pretty sure it would be bad.
        I am also using something like that presently. I actually changed its value and all classes I spawned had the value changed. It worked once. Now it is not working for me any more. Not sure why.

        Comment

        Working...
        X