Getting Reference to a class once its created

Hey guys, I know this is a newb question, I should change my name to newb, but ! I wanted to know

With this current setup

in the constructor for my weapon system class I have this

AWeaponSystem::AWeaponSystem()
{
	weap_damage = 0;
	weap_knockBack = 0;

	//Dynamic WeaponClassification
	CurrentWep = new WeaponClassification();
	*CurrentWep = WeaponClassification::Katana;
	canStun = true;
	referenceToSelf = this;
}

So the last line gets a reference to itself, so I can then pass it via a function

AWeaponSystem* AWeaponSystem::referenceToCurrentWeaponInstance()
{
	//check to see if we have a reference to pass
	if (referenceToSelf != NULL)
	{
		return referenceToSelf;
	}
	//or send in null
	else
	{
		return NULL;
	}
}

Now I wanna pass this to my player controller because I want the player controller to change the weapon via the mouse wheel.

This is kinda like an idea (which I think static classes might have something to do with this implementation).

AWasabi_HorizonPlayerController::AWasabi_HorizonPlayerController()
{
	bShowMouseCursor = true;
	DefaultMouseCursor = EMouseCursor::Crosshairs;

	if (AWeaponSystem::referenceToInstance != nullptr)
	{
		referenceToWeapon = AWeaponSystem::referenceToInstance();
	}
}

So what I want to do is be able to call the function referenceToInstance(), and get the reference that is created when the character who has this component in the game.

I was wondering if I should make this a static class as well?

I guess what I really want is just to know how to access this data in c++, knowing that it was created at some point. and that its reference in memory exists, you know ?

Also to note, this is being created as a child actor in a BP.

UE4 reflection system does not support static objects other then static functions. You should spawn your AWeaponSystem (from way you writing this and fact you want have static class make me worried you not doing it), spawning will return pointer to object which you can send to other objects that will use it like PlayerController, you need just one more global class to keep and then you can reference it in any class via that object. Thats how most classes in UE4 do, they keep pointers of themselves.

Also this does not look good:

CurrentWep = new WeaponClassification();
*CurrentWep = WeaponClassification::Katana;

Constructor in UObjects works different as UE4 handle production of those object differently, it’s called once when so called CDO (Class Default Object) is made, which is master copy of object and when you spawn actor or create new object, UE4 copy CDO in to memory creating new object like a stamp, it’s a hack which let UE4 to create object faster then C++ normally do, it also lets engine (and you code to) access default variables of object like static class, so this might be something that interest you, you can access CDO via UClass of object.

But as result, if you ever gonna make 2nd AWeaponSystem, CurrentWep will point to same WeaponClassification which was made during creation of CDO instead of making new one for new object. Any code that generate anything individually for a object should be called in BeginPlay (executed only in game) or PostInitializeComponents (executed after components are done, executed also in editor) or other creation stage event function which is few.

Thank you very much for the insight! as you can probably tell, its my first time applying c++ as well as applying it in the ue4 environment. To re-iterate to see if I understood correctly. I need to create a global class that holds the reference to the weapon system, so that I may access that global class in C++?

To give a certain idea of what I truly want to achieve is creating a weapon system that basically informs the character as well as the UI what weapon is equipped, and the character changes his sprite sheet according to the weapon equipped, as well as the stats to the damage and if the weapon can stun.

I want the player controller to have mechanisms that allow the player to change the current weapon equipped with the mouse wheel.

so Weapon System for me has a enum with how many weapons I currently want to prototype, it has a function that sets local variables inside of it with active stats like weapon damage, stun and knock back.

Now since I want weapon system to be referenced in the UI, Player Controller, and Player Character so that it can manipulate data inside those classes.

For now only one should exist in the world, and it should only belong to the main character (not planning multiplayer.)

What suggestions would you make for me to achieve this? Thank you again for your time!

Where do you currently spawn AWeaponSystem? Since it’s player related i think it should be in APlayerController or if you want it to reset it with death if APawn/ACharacter place it there so it’s liked with pawn and reseted with pawn. So you could hold refrence of it inywhere there and as engine have ways to easilly access to those classes you can access AWeaponSystem instance from there. For example you have it in PlayerController, if your game is single player you can easilly access PC via UEngine which is global engine class and you can access it via global GEngine pointer:

GEngine->GetFirstLocalPlayerController(());

from there you can access owned pawn via GetPawn() or GetCharacter() (depending if you use either APawn or ACharacter) and if you place pointer to AWeaponSystem there, you cast PC to your PC class and access it to and so something, and you can do that from anywhere, including UI so it can read it and display state of AWeaponSystem. Check API refrences and study how to navigate in framework objects

As for weapon themselfs, i personally prefere to use classes as weapon, make base class for item and then for conviniace use blueprints to make weapons themselfs based on that class (blueprints are are valid classes same as C++ class, they work the same), but if you prefare doing some kind of table with weapon information i can give you a hint. There something called UDataTable it a asset you can make in editor, check it out. You can create custom table types by creating structs extended from FTableRowBase, keep in mind they need to be visible in reflection system so don’t forget to add USTRUCT() and UPROPERTY()s you also need some place to point to that asset which could be or AGameMode blueprint or AWorldSettings (reponcible for world settings propeties, yes you can add extra options there :slight_smile: here you got docs about it:

Thank you very much for this information! I’m going to restructure accordingly and attempt the data table and see what results I can get. :smiley: thank you!