x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Office Holiday

Epic Games' offices will be on holiday from June 22nd to July 7th. During this period support will be limited. Our offices will reopen on Monday, July 8th. 

How to connect an actor with pawn to access the Pawn variables?

Hello guys! I have a problems with variables of a Pawn. I have a Pawn where i change different variables and inputs (like a bool variable of left click mouse). I have a Static Mesh actor. I want to interact with actor using variables from my Pawn. I tried to cast it with my pawn, but when i trying to get access to my variable my project crashes. (Also i know that in blueprint cast node is easy to use. In blueprint we can make a actor reference variable. but in C++ there is nothing like that I think).

 #include "BuildYourOwnClassC.h"
 #include "ObjectTemplate.h"
 #include "CursorPawn.h"
 // Sets default values
 AObjectTemplate::AObjectTemplate()
 {
      // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
     PrimaryActorTick.bCanEverTick = true;
     MyMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyMesh"));
 }
 
 // Called when the game starts or when spawned
 void AObjectTemplate::BeginPlay()
 {
     Super::BeginPlay();
 ACursorPawn* MyPC = Cast<ACursorPawn>(UGameplayStatics::GetPlayerController(GetWorld(), 0));
     if (MyPC->bRMB) {
     UE_LOG(LogTemp, Warning, TEXT("IT WORKS! (of course it doesn't:( )"))
     }
     
     
 }
 
 // Called every frame
 void AObjectTemplate::Tick(float DeltaTime)
 {
     Super::Tick(DeltaTime);
 }

Help pls! How can I connect my CursorPawn with this actor??

Product Version: UE 4.15
Tags:
more ▼

asked May 10 '17 at 08:45 PM in C++ Programming

avatar image

lefteyetakes
7 1 1 5

avatar image Roi Danton May 10 '17 at 09:18 PM

ACursorPawn is derived from APlayerController?

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

GetPlayerController doesn't retrieve a Pawn, it retrieves a Player Controller (PC); is your Pawn a Pawn, or is it a Controller? These are different things. The PC receives input from the player and controls the behavior of a Pawn (which can also be controlled by other Controllers, like an AI Controller).

If you want to retrieve a Pawn, you can get it from the PC:

 APlayerController* MyPC = UGameplayStatics::GetPlayerController(GetWorld(), 0);
 
 //always check that the pointer is correctly assigned before using it
 if (MyPC)
 {
     ACursorPawn* MyPawn = Cast<ACursorPawn>(MyPC->GetPawn());
     if (MyPawn && MyPawn->bRMB)
     {
         UE_LOG(LogTemp, Warning, TEXT("IT WORKS NOW!"));
 
      }
 
 }

"(Also i know that in blueprint cast node is easy to use. In blueprint we can make a actor reference variable. but in C++ there is nothing like that I think)."

Everything in BP is possible in C++; Blueprint is written in C++. There are things that can be done in C++ that can't be done in BP, but not the other way around.

more ▼

answered May 10 '17 at 10:19 PM

avatar image

GigasightMedia
1.7k 43 12 61

avatar image lefteyetakes May 11 '17 at 08:13 AM

Thanks for answering! It helped me! A created a variable ACursorPawn, and casted to my Pawn in BeginPlay. Now it works! (Of Course before it I got a few crashes. but it works now.) thanks a lot, dude. May the Force be with you!

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question