How to get the player index of a spawned character?

So I have been working on a project for a few weeks on and off and got to the point that I have AI from a derived class and want additional players in my game also. But I have been hitting a snag which I have worked around in some places but seems to be causing me some pain and there must be a good solution.

So I have been using the “get player controller” node as a way of casting things between the player and the items like weapons but at the moment I am just using the initial id so 0 to do this which is the player once they spawn. In my AI it is using this same id so when the player shoots the AI will shoot and there in lies at least one problem.

Is there any good way of getting the id of whichever “player” or in this case AI player has spawned a weapon (so the original owner/creator of this new object) and passing this id to the weapon for instance. This feels like something that should be simple/obvious but I have no ideas at the moment where to look.

Okay I feel stupid it was simple, but if anyone else has this issue you want to use “get owner” it will trace back up to the actor who created/owns the weapon which in the player or AI who spawns it.

I did see that the “get player controller” is not recommended as a good way to get the player unless your at the base level or I guess only have one actor i.e. a single player game that is using this class.

Thank you for this. Helped me to figure out why my code wasn’t working after I spawned a new player character into the world. Turns out, after you “respawn”, if you’re not destroying the original player (I want my bodies to stay in the world), then your newly spawned character isn’t index 0 anymore.

I wanted to add this for anyone else because I ran into a similar issue again where I needed an Instigator for my projectile, and Get Instigator would fail.
Instead of using get player character at Index 0, you can instead Get Player Controller and then Get Controlled Pawn and then cast this to your character and set this as a reference. Then you always have the most relevant character at whatever index it happens to be.
GetControlledPawn