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 reference a separate class in C++ Only

I want to use strictly code and i want to find the player in the pickup class i know GetWorld()->GetFirstPlayerController() works but only for the base character not my custom one.

Product Version: UE 4.11 Preview
Tags:
more ▼

asked Feb 03 '16 at 03:01 AM in C++ Programming

avatar image

Skullzz098
6 2 5 7

avatar image Matthew J Feb 03 '16 at 07:48 PM

Hello Skullzz098,

There are multiple ways to do this, but it would depend on when you need it inside of the pickup class. Do you need it when the player picks up this pickup? If so, you can take the reference to the character and get the PlayerController from that. There are other ways too, but I'll need to know in what context you need the reference.

avatar image Skullzz098 Feb 04 '16 at 02:11 AM

I have a class called ASkullCharacter and i have a variable in my pickup class. I would like to say that My variable of ASkullCharacter is referencing the ASkullCharacter in the world. Consider there is only one ASkullCharacter in the world.

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

1 answer: sort voted first

If you already have a reference to the ASkullCharacter stored, you could get a reference to its controller like this:

 AMyCustomController* ControllerReference = Cast<AMyCustomController>(CharacterReference->GetController());
 
 if(ControllerReference)
 {
 //What you need to use the reference for here
 }

The "if" is there to ensure that the cast succeeded, otherwise you'll be making references to a null pointer which isn't a good idea.

more ▼

answered Feb 04 '16 at 03:44 PM

avatar image Skullzz098 Feb 04 '16 at 09:50 PM

Where do you put this i am getting errors still. I am using an actor class to access my player. Does that matter at all?

avatar image Matthew J Feb 04 '16 at 09:54 PM

You would want to put it wherever you need the reference. Can you post your code so that I can get a frame of reference on what you have and don't have so that I can assist you properly? If it won't fit here, you could use pastebin.com and link the code that way.

avatar image Skullzz098 Feb 04 '16 at 10:10 PM

[link text][1]

Here is my code [1]: /storage/temp/77372-fqchcuwd.txt

fqchcuwd.txt (447 B)
avatar image Shadowriver Feb 04 '16 at 11:48 PM

use 010101 icon to add code formating

avatar image Skullzz098 Feb 04 '16 at 11:53 PM

I don't understand what is code formatting

avatar image Shadowriver Feb 04 '16 at 11:54 PM

I seen you pasted code and you delete it since it had odd formating, if you want to paste code and have it nice look select it and click that icon

avatar image Skullzz098 Feb 04 '16 at 11:59 PM

what is that icon?

avatar image Shadowriver Feb 05 '16 at 12:01 AM

It formats code like this:

 // Fill out your copyright notice in the Description page of Project Settings.
 
 #include "Beth.h"
 #include "BethCharacter.h"
 #include "FlashLightBattery.h"
 
 
 AFlashLightBattery::AFlashLightBattery()
 {
     GetMesh()->SetSimulatePhysics(true);
     
 
 }
 
 void AFlashLightBattery::OnPickedUp()
 {
     ABethCharacter* MyBeth = Cast<ABethCharacter>(MyBeth->GetController());
     if (MyBeth)
     {
         //Trying to do something here with the ref...
     }
 }
avatar image Matthew J Feb 05 '16 at 01:26 PM

Thank you for the code, but you're using the Cast function incorrectly. I'll try to break it down before linking to documentation for it.

I'm going to assume that ABethController is the name of your custom player controller class and post the correct function.

 ABethController* MyBethController = Cast<ABethController>(MyBeth->GetController());

In this case, MyBeth is a pointer to a ABethCharacter reference that has been previously declared/assigned. You're using "GetController" to get the Player Controller that is assigned to the MyBeth character. You're then Casting to ABethController using the reference from the "GetController" call to ensure that what you're getting is an ABethController and not another type of Player Controller. As for the first part of this line, you're declaring a new ABethController pointer to store the reference for later use.

You would also want to change the if statement afterwards to be checking for MyBethController instead of MyBeth.

We currently don't have any documentation for casting in C++ but this should be able to explain what casting is, even though it is in Blueprints.

https://docs.unrealengine.com/latest/INT/Engine/Blueprints/UserGuide/CastNodes/index.html

(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