Hi
Very simple question.
How to get a public variable from another class probably using pointers
Basically this, but your question is too open, since you could get the object from collision or maybe its a variable in your current class:
auto myObj = Cast<AMyActor>(OtherActor); //or whatever object you require
if(myObj){
auto val = myObj->MyPublicProperty;
}
Yes, I probably expressed myself very widely. I have a float variable Seconds in class Actor1. I need to print it in another class Actor2. Is your code suitable for this situation?
Yes, but the first issue is getting access to Actor1 in Actor2. Not sure how you have that setup but the code above will work:
Sorry again, but what syntax should the other class have?
No, you want the actual object instance that is living in the scene and not the class. Is this a collision method? If so then the OtherActor variable should be your actor.
If you still need help can you show the whole method including the signature?
This is the collision method, however, I do not need a collision actor, I need a certain class, from which in case of overlap I will take the Seconds variable
Here is the whole class:
void ATriggerActor::OnOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
if (OtherActor && OtherActor != this)
{
GLog->Log(TEXT("TriggerOverlaped"));
ATriggerActor* Ptr = Cast<ATriggerActor>(AAnTimActor);
if (Ptr)
{
};
GEngine->AddOnScreenDebugMessage(-20, 99999.f, FColor::Red, FString::Printf(TEXT("Trigger overlaped,&f"), DSeconds)); // DSeconds is a variable equivalent to Seconds
}
}
This method is called once your actors overlap so the “OtherActor” is the one you need to cast in order to access the Seconds variable.
ATriggerActor* Ptr = Cast<ATriggerActor>(OtherActor); //assuming other actor is a ATriggerActor
You probably misunderstood, I need to call a variable not from an actor that was overlapped, but from AAnTimActor
Okay, but how does your current actor reference AAnTimActor? You need access to the instance of AAnTimActor to get the property values.
Not yet. That’s the question, I need to get the value of the variable, but I don’t know how
You generally don’t “access a class” to pull a variable. A class is just a template (for lack of a better word) for an object. Instead you access Instances of an object. These are objects that are created during runtime either through the editor (dragging an actor into a stage creates an instance of that actor when the level loads), or through C++ manually instantiating objects.
If you know your level has AAnTimActor objects within it (e.g. you added them in the level through the editor), you can use an ObjectIterator to loop through all the objects of a class:
for(TObjectIterator<AAnTimeActor> It; It; ++It)
{
// Grab whatever data.
It->MyPublicProperty; // Do stuff.
}
It’s not the ideal way to find instances of an object, but without knowing more of what exactly you’re trying to do and how your objects are setup (is the Player a AnTimActor? Can the player overlap with AnTimActor? Or are they completely unrelated do a collision), it’s hard to tell you the best course of action.
Thank you, these two сlass Actors are not connected in any way, the essence of them is that one counts the time, and the second displays time on the screen when the character overlaps the trigger.
Is this the best option for this?
Gotcha. There’s a couple of ways to do this. If your actor which Displays time cares about which actor (“I only want to know when Actor Instance A hits this trigger”), then you’d want a way to pass it that actor during construction (In OnBeginPlay for example). If it doesn’t care about the actor (“I want to know when ANY Actor of Type X hits this trigger”), then you could use a Delegate which is just an event you can subscribe to (for the displaying actor) and have the actors call it when they collide with something.
Useful docs:
Thanks, I will try to do the first way
If your just starting to get the hang of oop and polymorphism I recommend making little programs from scratch yourself, to get the theory down. I used a bootstrapped language called “processing” which helped a lot. It lets you easily display a window and draw shapes/textures in a few lines of code. Even lets you do 3D stuff. It’s mostly a learning tool. And it’s bootstrapped over java but has a global scope. https://processing.org/
Maybe not the best way but it’s how I learned many years ago. There’s also openFrameworks if you want to do it in cpp it’s just there’s less tutorials to follow there. Chilli’s framework is another good one. His teaching style is very entertaining if you like that kind of charisma. Beginner C++ Game Programming Tutorial 0 DirectX [Introduction/Setup] - YouTube
That voice… Could listen to it for hours on end.
Well then how would you pass the actor to it in BeginPlay? If I have a unique actor and I know what it is from the getgo, then how can i pass it?
Set it as a property on your object, or create it via the Actor (UIs for example). You can also leverage the Subsystem interface that came in with 4.22, etc. There’s lots of ways to grab Actors efficiently, it just depends on how your data is setup and what the use case is.