Need help understanding how to access variables in ue4 c++

Hello, unreal community I have a problem that I have been trying to understand for a few days now and the problem that I I’m having is accessing variables in c++ from player to actor, for example, I have a class called weapon which is an AActor and I want to be able to get a variable called gun ammo but this is where I get stuck on how would I go about getting access to this variable in the player class or for an example another actor and check the image below it shows an example of what I mean any help would be great and as well I have done the ue4 tutorials but they seem outdated and does not explain a lot of things.

.

Hello, Rekenq, could you give me an example of what you mean when you are saying “On your weapon class, create a variable that references the owner of the weapon, for example, an ACharacter* owner variable, (you could use your custom character class too). Then whenever you create this weapon you set the owner variable so it points to your character.” so do you mean create a variable in the weapon class like this for example Aweapon weapon = GetOwner(); or do you mean something else? and thanks in an advance.

Hello, Rekenq, could you give me an example of what you mean when you are saying “On your weapon class, create a variable that references the owner of the weapon, for example, an ACharacter* owner variable, (you could use your custom character class too). Then whenever you create this weapon you set the owner variable so it points to your character.” so do you mean create a variable in the weapon class like this for example Aweapon weapon = GetOwner(); or do you mean something else? and I I’m a bit confused on the part where you said

can find the reference for any actor
you want, in almost any part of your
code."

so what do you mean by reference do you mean using the Aweapon variable that is set to the owner of the class? And if so can you give a short example of the both answers so I can see it in code thanks.

It all depends on your system and what type of game you want to make but here are two suggestions

  • On your weapon class, create a variable that references the owner of the weapon, for example an ACharacter* owner variable, (you could use your custom character class too). Then whenever you create this weapon you set the owner variable so it points to your character.
  • Use the actor iterator if there’s only one player. You could iterate while looking for a specific class (yout character class), this way you can find the referece for any actor you want, in almost any part of your code.

Here’s a link that explains actor iterators

Hope this helps!

Yes that’s exactly what I mean. Create a variable in the weapon class. I don’t know if GetOwner() exists or if you invented it but anyway what I mean with set it when you create it is to go to the place in your code where you spawn this weapon, wherever that is, and in that place you would then do something like NewWeapon->Owner = this; (if you are spawning it inside the character class).

so what do you mean by reference do you mean using the Aweapon variable that is set to the owner of the class?

Reference is just another name for pointer. So yeah by reference I mean using the proposed owner variable in the weapon class to get the information that you want (the gun ammo for example) from that variable.

As for the part where I talked about finding the reference (pointer) to any actor, it’s exactly that. Actor iterators are the C++ equivalent of the Get All Actors Of Class node in blueprint and similar ones.

Edit: Question. Do you have any experience with programming?

Hello, Rekenq, yes I have experience with java c++ HTML CSS javascript PHP and MySQL and I only started to learn the unreal engine 4 a few months ago so I understand a bit about it and how a lot of it works in c++ as well from what I understand is that this code in the image below allows me to find the class Aweapon then allows me to access variables and methods of that class? is that right? as well I wish there were more videos for c++ in ue4 then blueprints because I understand a lot more when I watch videos then I do reading text. as well I do understand what you mean by a reference it was just late when I was reading the question. .

Yup, that’s right!

If you’re just starting out programming, C++ is just too hard for now;
You’ll have to first learn and be comfortable with Object Oriented Programming, Pointers and Memory Management.

With Blueprints you most likely can skip all that; Pointers and references, references to pointers, pointers to pointers, copies and passing by ref or passing by copy… this stuff is real complicated and you MUST have them sharp before programming in C++ unfortunately.

Your crash when calling the ammo there on header is probably because you try to access a NULL value (there’s nothing there yet).
Epic’s tutorials doesn’t teach you the requirements mentioned above, you’ll have to study C++ by yourself while learning UE4.
I suggest for now to just concentrate on learning Object Oriented Programming and its concepts and rules… then master Pointers and References (they are very confusing), understand when you are accessing an object or a pointer to it or a reference to a pointer of it or a pointer to a pointer or a copy of that object… If you don’t understand pointers in C++, you’re going to cause memory leaks; this is super super important to learn before using C or C++!

Hello BrunO XaVleR I do understand pointers and passing by reference in c++ so I am taking my time but I got the answer now so thank you for the advice.