OK, so, i’m sorry if this comes off harsh, but you need to learn a bit about “Object Oriented Programming” here. Please don’t take it harshly, but you are completely not grasping the principles of OOP here.
So, each class represents a thing that COULD be in the world. When you create something in the world, you create an instance of that class.
You need to access a variable inside an INSTANCE of Amain_character (shouldn’t widgets have a prefix of some kind? i’ve never written a widget, but like Actors all start with A, shouldn’t Widgets all start with something specific? that’s an aside topic…) from inside an instance of cppWidget.
OK, so you are CPPWidget A, but you need to access Amain_character C. How do you get there?
I don’t know. You don’t know. And you probably never should use the solution of “GetPlayerCharacter(0)” unless you are absolutely positive your code will never ever be used in a multiplayer situation, and you’re taking a shortcut to get there because you want to ensure you’re never ever multiplayer.
So, a Widget is owned by a Player. A Player owns a PlayerController, and the PlayerController owns the Character.
So, from the Widget, you can use GetOwnerPlayer() which will give you the Player that owns it. Then from that, you can get it’s PlayerController, and you can use that to get the Character that that Player is controlling through it’s PlayerController.
So, from Widget, you call GetOwnerPlayer(), from that you call GetPlayerController(), and from that you can call GetPawn() (which returns the Character, but it’s called GetPawn because Pawn is a superclass of Character)
So, this should look something like
ALocalPlayer* Player = GetOwnerPlayer();
if (IsValid(Player)) {
APlayerController* Controller = Player->GetPlayerController();
if (IsValid(Controller)) {
Amain_character* Character = Cast<Amain_character>Controller->GetPawn();
if (IsValid(Character)) {
ammo->setText(Character->ammo_clip);
}
}
}
Also, you should pretty much never have anything that even kind of resembles inline auto g_vars { Amain_character() };
… that doesn’t jive at all with any kind of good pattern of anything.
So that’s it, if the code is impossible to fix, or something like that, let me know and I give up trying to make games and programming.
Don’t do that. But, you definitely need to learn some Object Oriented Programming concepts, though, and learn how to access instances of things in the world, not the classes that they are created from.
Imagine that your Amain_character is a blueprint (a very good thing to imagine in Unreal, as blueprints are also a thing, and they work identically) for “how do i build a character”, but it is not a character. A character doesn’t exist until after a Player logs in, gets a Controller, and t hen starts the game. When the game starts, then a Character is created, and becomes Controlled by the Controller.
There could be any number between 0 and thousands of Characters. They could all be Amain_character. Same thing with Widgets. You have to know which widget you’re trying to get to, and which character you’re trying to get to.
You’re also going to want to read through Coding Standard | Unreal Engine 4.27 Documentation and consider adopting any/all of it’s recommendations. Some are requirements for operating in the engine, others are suggestions, but all of them have been adopted with 30 years of thought in the process.