So i created a blueprint of a floor mesh and added an actor component to it that is linked to a trigger volume. i have also created a blueprint implementable event function in the class out of which the blueprint was created and implemented a simple print-string. What i aim to achieve is that when i walk into that trigger volume, the blueprint-implementable event would get called. However i need to call that function in the actor component class. I’ve tried quite a lot of things but i am unable to call that function which is why i’d like to know if there is a way to call static/non-static member functions of a class in another class.
help needed ASAP!
This is strictly about non-static member functions. To call a static member function you just include the class scope
struct Example
{
static int Foo() { return 0; }
};
int main()
{
return Example::Foo();
}
To call a non-static member function you need an instance to call it on. How do you have this set up? Are these persistent actors in the level (are they always there or are they spawned dynamically)?
Yes, The Actor that has the actor component on it is always there.
The setup is as follows:
First, I created a class based on AActor class and later created a blueprint (say, BP_Platform) out of it and set the static mesh component to a floor mesh. In this class, I have created a blueprint implementable event.
below are the snippets of the header file and the cpp file for this class:
after that, i created a class based on the UActorComponent class and added this component to the BP_Platform. Below are the code snippets for the header and cpp files of this actor component class:
I’ve already mentioned where i’d like to call that blueprint implementable function in the actor component class in the form of comments.
I have already tried to create an instance of that class and using the . / → operator, i have even called for the function but unreal crashes everytime i try to play the game. I guess the instance is uninitialized as when i introduce an if statement with the instance variable as the condition, the body of the if won’t execute.
You already have an instance of the trigger volume. You just need your pointer to point to it.
If your actor that has that component is already in the level and not spawned into you can just expose the pointer to the editor and set it there
UPROPERTY(EditInstanceOnly)
class ATriggerVolume* DisplayTrigger = nullptr;
If it isn’t (i.e. it is spawned during gameplay) then you will need to use a different way as you can only set that on instances (which is why I used InstanceOnly)
If there’s only one trigger volume you could use
#include "Kismet/GameplayStatics.h"
Cast<ATriggerVolume>(UGameplayStatics::GetActorByClass(ATriggerVolume::StaticClass());
Or you can get it directly by name
#include "UObject/Object.h"
FindObject<ATriggerVolume>(GetWorld()->GetCurrentLevel(), TEXT("ID_Name"));
Just use either at BeginPlay to set the pointer.
You can get the ID Name by right clicking the header in the world outliner
I’m afraid this is not the solution i needed. I am not talking about the ATriggerVolume instance in this question. What i aim to achieve is a way to call the BlueprintImplementableEvent StartGameInstructions() function [declared in the AInstructionsDisplayPlatform class] in the UInstructionsDisplayZone class’s tick function. I havent even created an instance of the AInstructionsDisplayPlatform class yet!
Really need the help
Well you need to get a pointer to the instance of AInstructionsDisplayPlatform from UInstructionsDisplayZone. How is the platform going to exist? Same questions as above, spawned or persistent in the level? More than one?
the platform is persistent in the level. and only 1 exists in the level.
Then simply change ATriggerVolume
to AInstructionsDisplayPlatform
above.
ah i see now! thank you!
Could you also tell me how i would initialise the pointer to AInstructionsDisplayPlatform class in the case of having multiple such platforms?
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.
Well you would need a way to determine which one you want but there’s GetAllActosrByClass
that like the above that uses a TArray
output parameter.
The function Dan is referring to is actually called GetAllActorsOfClass
in UGameplayStatics
, and based on what @sameerdash1 is describing in terms of his goal I don’t think he needs to use that function at all.
It sounds like he’s trying to display some text when a user steps on the platform by using a trigger volume that is referenced by an actor component. I’d probably do away with the actor component and just add a public variable to the Moving Platform BP that is set to a Trigger Box object reference. As soon as you do that, you’ll see a list of Events pop up in the variable’s details panel where you can easily add things like “On Actor Begin Overlap” for instance. In the main viewport, all of your Moving Platform BPs will now have that same variable exposed in their details panels from which you can select the trigger you want to pair with the platform.