Set the Actors Owner in C++

How do i set the actors owner in c++, I’ve tried

HoldingActor->SetOwner(this);

and that doesn’t work.

I don’t know if what you want is to attach an actor to another one, if this is the case, you can use :

HoldingActor->AttachRootComponentToActor(this); //this being an AActor

SetOwner sets the base actor for a given Actor. If you want to hold a hierarchy you have to use AttachRootComponentToActor as @Michaël said.

Some info on handling the hierarchy:

To get the connected actor to a given actor. If you attache Actors using their components and a socket using AttachTo the new parent will hold a list of children AttachChildren, you then only have to go from the root actor and iterate all its AttachChildren and compute their masses. You can get the root using GetAttachmentRoot to get the root component of the attachment or GetAttachmentRootActor which gives the root. This method is working when using AttachRootComponentToActor which attaches components of different AActors.

I have a server function in my actor and i can only execute that function if i’m the owner of that actor, so i need to know how to set the owner on a separately spawned actor that my player is holding.

The server has has ROLE_Authority and you should be able to change the base on the server, you can not call that server methods from another client that is not the owner that’s totally true and critical (think of cheating). But what you can do is to send the actor reference through a server function of your PlayerController, then once on the server you can change the base there.

It’s hard to explain, here is my original post C++ Server functions can only be called by the owner - Programming & Scripting - Epic Developer Community Forums
I’m not calling the function directly from another actor. But i need to know how to set the actors owner for example

CurrentWeapon->SetOwner(this);

but that doesn’t work for some reason.

That’s exactly what you can not do directly: call server function on an Actor that you do not own. So the way of doing it to route the call though an Actor you do own, like your PlayerController, that’s the example I explained in the comment. Here’s some pseudo-code:

// In your player controller

UFUNCTION(Server, Reliable)
void AMyPlayerController::ServerCallSpecialFunctionOnActor(class AMyActor* myActor, int someArgument)
{
	if (myActor)
	{
		myActor->SpecialFunction(someArgument);
	}
}

// In your actor

UFUNCTION(Server, Reliable)
void AMyActor::ServerSpecialFunction(int someArgument)
{
	SpecialFunction(someArgument);
}

void AMyActor::SpecialFunction(int someArgument)
{
	// Here is your logic
}

// Use cases:

//  1) Calling it from the owner as simple as calling the actors server method
myActor->ServerSpecialFunction(12345);

//  2) Calling from a non owning actor though the PlayerController
myPlayerController->ServerCallSpecialFunctionOnActor(myActor, 12345);

Note that routing such logic could be eventually exploded by cheaters!

But the thing is, i have an object in my inventory that i spawned and attached to my player so all i basically need to do is
Weapon_Im_Holding->ServerStartFire();
But it doesn’t work and i was told it’s because i’m not the owner even though its attached to my player.

You as a client are only the owner of your own Pawn, your player controller or any locally spawned actors (not spawned in the server). So to execute stuff that you do not own you have to bridge it.

How are you spawning your inventory item? You would normally do the ServerStartFire through your pawn, which in turn on the server calls weapon->StartFire. That would be the normal flow.

It’s not finished but i spawn it locally and i use it through the pawn. How can i fix this because even if i call a standard function that executes within the weapon that calls the server function within that actor it still doesn’t work

If you spawn it locally than your weapon wont be in the server, and so you can not call any server functions on it. The way to go is always to spawn on the server stuff that must be distributed and functional in multiplayer and using your Pawn or your PlayerControler to execute client actions. The StartFire example I posted is a common pattern even used in many UE3 games (which uses the same replication paradigm).

I’ve just spawned the weapon on the server and the client and it didn’t fix anything, i don’t know if i spawned it wrong or…

Also just to keep in mind that, when i disable “Run Dedicated Server” in the editor everything works but when its enabled it doesn’t.

But you are still trying to replicate through the weapon. You should replicate through the pawn or through the PlayerController. If you spawn the weapon in the server and you spawn the weapon in the client they are two different instances.

But if i only spawn the weapon on the server the client cant see it because its not spawned on there client.

So you have to spawn the weapon on the server and to send the weapon class to the client, so he can spawn a fake weapon for all kinfd of effects, but the start fire should go through the server.

I think the topic is drifting to a different topic. You can not do what you want the way you are doing it. I think starting a forum thread would be better because we are starting to get out of space here.

The answer for the initial question is that SetOwner and AttachTo works but you are not using the way it is supposed to be used. You are more in a client/server logic scenario.

Thank you for your time, but could you redirect me to where ever i could learn about “faking” a weapon because when i search for stuff i can never find what i need to learn. Thanks

Post a topic in the forum. I’ll be there to help you ^^. Remember to accept the answer to help t community (it gets unchecked each time some one posts a new comment/answer).

Use AttachToActor, AttachRootComponentToActor was deprecated now.