C++ Call variable/function from another class

So i’m trying to call a variable and a function from another class. But i don’t even know if the way im trying is possible… I tried many ways and some my unreal just crashed

Screenshot 2022-12-15 051547

i want to acess this variable ↑ from here ↓

And why I think this is very wrong, well… apart from the fact that I’m very new to unreal and c++, I’m not feeling this a decent code

The way i tried and crashed is this ↓
1

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.

#include "Kismet/GameplayStatics.h"

void UcppUserWidget::on_clicked_button()
{
    if (auto Character = Cast<Amain_character>(UGameplayStatics::GetPlayerCharacter(0)))
    {
        ammo->SetText(FText::AsNumber(Character->ammo_clip));
    }
}

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.

1 Like

Dude thank you, this is even motivational hahaha

1 Like

The last person who told me I motivated them when they were a beginner went on to do a lot of great work on the Call of Duty series :slight_smile:
Your results may vary. :smiley:

FYI, GetPlayerCharacter(0) always returns the local player. Even in multiplayer. So it’s fine. The only exception to this is split-screen, where you have multiple player controllers locally.

yes, that would be consistent in a widget, but it would be wholly inappropriate in something that is not directly owned by a Player. Better to be in the habit of getting your way to the thing you want via some method that relates them together, rather than shortcutting to “Get Local Player” or “Get Local Character” or whatever other shortcuts will blow up in your face when you don’t know any better how to find a connection between two objects.