C++ inheriting variables from other classes(beginner)

alright, first off Ive only started learning c++ since I got into the Rocket Beta. My previous programming experience is pretty much limited to python so forgive me.

I have a character class called SteamyCharacter(extends from ACharacter) and an item class called SteamyItems(extends from AActor)
I have added #include SteamyItems.h into my SteamyCharacter

Steamy character has a variable called ItemHealth, MaxHealth
SteamyItems have a variable called BonusHealth
both of these variables are floats

I would like for IteamHealth = BonusHealth - but I cant get it working, entirely because my c++ book and cplusplus.com dont really cover class inheritance that work with Rocket.
Ive tried:
ItemHealth = SteamyIteams::BonusHealth;
ItemHealth =
ItemHealth = (class ASteamyItems::BonusHealth);
ItemHealth = BonusHealth;
and a couple others.
With that said, how do I go about getting variables from one class to another?

ack! one more question is how to access the pawn’s stats from another class. I cant just spawn a new instance of the pawn class because the stats wouldnt be the same because they can change in-game and the default values are whats spawned correct? So how would I go about accessing the spawned in-game but not in-code Pawn/player?

Your Other Question

Made this a separate answer for better code coloring

“Ack! one more question is how to access the pawn’s stats from another class. I cant just spawn a new instance of the pawn class because the stats wouldnt be the same because they can change in-game and the default values are whats spawned correct? So how would I go about accessing the spawned in-game but not in-code Pawn/player?”

Again, a critical part of the UE4 C++ logic you need to get is how pointers work with in game Actor instances.

Once an actor is created, however it is created, the way you get access to it is through a pointer.

AActor* PtrToAnActor;

research C++ pointers for more info

PtrToAnActor is something you create in your code, but it does not automatically point to anything

you must obtain a reference to your actor and then give it to PtrToAnActor.

PtrToAnActor

then once you have that done, you can your custom created stats for that actor

#Obtaining an Actual In-Game Actor

Here’s some sample code for finding actual instances of Actors during Runtime

//Search through all actors currently existing in the world

//note that object iterator will find actors in the pre-PIE world
//	if you run this code in a PIE session.

//can avoid this by running your game as independent instance

//right click on .uasset and play game


AMyActor * PtrToAnActor = NULL; //create your ptr in your code

//run this loop in your code, 

you can run objectiterator anywhere, even static functions

for ( TObjectIterator Itr; Itr; ++Itr )
{
	//any coditions on choosing which actor, such as name or other things;
	
	//if (Itr->GetName() == "MyActorPlacedAndNamedInEditor")
	//{
	
	//if (Itr->Health < 50)
	//{
	
	//Assign your created Ptr to actual Actor in game world
	PtrToAnActor = * Itr;
	return;
	//}
	
}

//no actor found, return
if(!PtrToAnActor) return;
//~~~~~~~~~~~~~~~~~~~

//Get Actor's Location
FVector Loc = PtrToAnActor->GetActorLocation();

//Access and Change the Stats of the Found Actor
PtrToAnActor->Stats.Health = 50;

#The

Notice since we’re using a pointer you use the → operator instead of the . operator

PS: if this gets you rolling again please mark as green until you have a further related question (or are good to go)

It might be better to use the newish nullptr keyword instead of NULL. In a completely unrelated project, I’ve had some absolutely mystifying bugs due to the fact that NULL == 0 or 0L depending on compiler, and is not actually a pointer type.

Thanks for sharing this Eric!

I will keep an eye out in regards to this issue :slight_smile:

Rama

Dear Nathan,

I presume your goals is for your Character to have an Inventory of SteamyItems?

If so a good route to go, assuming you want your items to be ingame actors and thus they have to be extending AActor,

is to make a Dynamic Array in your Character Class that links to world instances of these items.

TArray Inventory;

I think the * in ASteamItems* is part of the UE4 C++ logic you were missing,

you are not storing the actual items in the Inventory, only pointers to the actual items.

To add a SteamyItem to the player’s inventory after it has already entered the world (via SpawnActor or if it was placedi n the world;

//spawn the item in an actor-extending class
ASteamyItem* NewSteamyItem = GetWorld()->SpawnActor(...);

Player->Inventory.Add(NewSteamyItem);

#Instances

The most important thing for your immediate progress is to realize you cannot use things like ASteamyItems::BonusHealth because these are non-instanced versions of the data.

Every AActor has to be instanced / spawned / placed in the world before you can access it’s members.

And then in that case you use →

AInstanceOfSteamyItem->BonusHealth;

#Subclassing SteamyItems

You should probably be sublcassing SteamyItems for each type of Item

so BonusHealth is not a member of SteamyItems, BonusHealth becomes a SUBCLASS of SteamyItems

UCLASS()
class ABonusHealth  :  public ASteamyItems

Then you could have mutliple BonusHealth Actors with different actual health values

using a member property on BonusHealth like

UPROPERTY()
float HealthValue;

#Game Programming Tip for Items: Sublcass Often!

Store as much of the base logic for items and things that player can pick up and interact with in your base level classes,

then sublcass those base classes for specific types of items like bonus health

so bonus health item would not store the logic for how it can be picked up by player, or the fact that it has a static mesh component representation int he world.

Bonus health item would only store the logic for what happens when player picks it up, and the fact that health is given to the player.


If you subclass your item types a lot
then you can make sweeping changes to your entire game code all at once.


#For example,

if you have every type of item extend a base class like ASteamyItems, and only ASteamyItems has the logic for what happens when player gets close to the item to pick it up (the world static mesh component disappears, and a sound plays, etc),

then if you decide 200 weeks from now, after you have 57 items,

that you ALSO want a standard particle effect to play every time player picks up an item,

well now!

you have to change only 1 class!

your base class for all items!

(Rather than meticulously editing 57 item classes

soooo

subclass things like items often :slight_smile:

:slight_smile:

PS:

If this sufficiently answers your question to get you going in right direction please mark the checkmark by my answer so others know this issue is resolved / well under control :slight_smile:

So a couple things, it would seem I cant do this in the main constructor. Is the postBeginPlay() a good spot to have it? The only reason I ask is because theres an initial ‘tick’ i guess where its value is zero until it gets initialized. I tried it in the main constructor and my game crashes(bug or ?).

Second thing is with your mention of using BonusHealth as a seperate class entirely, what benefit would that have? you make it sound like any subclasses of SteamyItems wouldnt be able to have custom values attached to them.

Hi Nathan,

Have Rama’s answers been beneficial to you in answering your questions?

Feel free to follow-up if not.

Thanks