Trigger Volumes And Pointers

Hi,

So I just had a quick question regarding using pointers with Trigger Volumes, i get that some functions require pointers as parameters therefore when making any variable I should make sure its a pointer. But here’s the thing any time I make a variable with the ATriggerVolume class type, it will give me an error unless I put the * after it. Isn’t this supposed to be a return type? Nothing is using this variable at the moment so why is it mandatory that I make it into a pointer.

Thank you

Nothing is using this variable at the moment

As you can note there’s a UPROPERTY attached. Funny thing about UPROPERTY is that you don’t need to do anything else via code so that in code it just looks like its a nullptr in the cpp, and it will be nullptr, unless assigned from the editor. You could of course probably do everything via code if you really wanted.

UPROPERTY(EditAnywhere)
ATriggerVolume* PressurePlate = nullptr;

So if you use PressurePlate from within the editor then it will have a value of ATriggerVolume with nothing else needed in code - it can just be used as-is.

This is why PressurePlate can just be used without issue. It goes from the above to being used:

if (!PressurePlate) {...}

if (!PressurePlate) { return TotalMass; }
PressurePlate->GetOverlappingActors(OUT OverlappingActors);

I agree its a bit jarring and odd at first that its done in the background and not via your code. It would be nice if they could just insert something else so it looks like its correct too, that is non-null besides any checks (that is that its in use via the editor). But at least if you see UPROPERTY then you know to look in the editor and if its null when running the code then its not in use at all or has some problem.

1 Like

I’m not sure what you mean here. You can have pointer variables, they aren’t limited to return types.

Otherwise you would be creating a variable on the stack and UOpenDoor isn’t what is spawning/creating the TriggerVolume it’s just referencing it.
The trigger volume is going to spawned at runtime by the engine and UOpenDoor wants to reference that, if you don’t use pointers or references you would be creating a copy with code like

ATriggerVolume Plate = Trigger;

So that would be bad.


So why not references instead? Because you need to initialise references when you declare them but there’s no way to do that here i.e.

UPROPERTY()
ATriggerVolume& PressurePlate = //??? what could you possibly put here?
1 Like

Thank you guys so much,

I think I get it now, because it doesn’t exist yet unreal requires that I have a pointer to the address of the volume so when it does eventually spawn it can initialize the variable, is what I’m saying correct? Also the same thing goes for AActor* as well, if i’m not mistaken.

Essentially yes. You want to be able to call functions on the volume that is spawned like IsOverlappingActor but since that is created dynamically during the game then the only way to refer to that is through a pointer.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.