Announcement

Collapse
No announcement yet.

How to get a variable from another class

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

    How to get a variable from another class

    Hi
    Very simple question.
    How to get a public variable from another class probably using pointers

    #2
    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;
    }
    SolidGasGames

    Comment


      #3

      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?

      Comment


        #4
        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:
        SolidGasGames

        Comment


          #5
          Sorry again, but what syntax should the other class have?
          Click image for larger version

Name:	-1.PNG
Views:	1
Size:	5.5 KB
ID:	1652954

          Comment


            #6
            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?
            SolidGasGames

            Comment


              #7

              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:
              Code:
              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  
                   }
              }
              Last edited by whitegоld; 08-14-2019, 05:50 AM.

              Comment


                #8
                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
                SolidGasGames

                Comment


                  #9

                  You probably misunderstood, I need to call a variable not from an actor that was overlapped, but from AAnTimActor

                  Comment


                    #10
                    Okay, but how does your current actor reference AAnTimActor? You need access to the instance of AAnTimActor to get the property values.
                    SolidGasGames

                    Comment


                      #11
                      Not yet. That’s the question, I need to get the value of the variable, but I don’t know how

                      Comment


                        #12
                        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:

                        Code:
                        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.
                        Able Ability System - A high performance, robust ability system for UE4. Now Available!

                        Comment


                          #13
                          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?

                          Comment


                            #14
                            Originally posted by whitegоld View Post
                            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:

                            https://docs.unrealengine.com/en-US/...nts/index.html

                            https://docs.unrealengine.com/en-US/...ing/index.html

                            Able Ability System - A high performance, robust ability system for UE4. Now Available!

                            Comment


                              #15
                              Thanks, I will try to do the first way

                              Comment

                              Working...
                              X