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 ()->GetFirstPlayerController() works but only for the base character not my custom one.

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.

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.

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.

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?

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 and link the code that way.

[link text][1]

Here is my code
[1]: 77372-fqchcuwd.txt (447 Bytes)

use 010101 icon to add code formating

I don’t understand what is code formatting

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

what is that icon?

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...
	}
}

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