Trouble with replicating a class variable


So I have a menu that I choose a turret from, this sets the class for which is spawned but it doesn’t work for the client. It works if I just set the spawn actor node but I want to be able to spawn different turrets.

The methods I have tried are through the player pawn, player state as it’s replicated and the gamestate which for some reason worked better as if I chose the turret on the server I could then spawn them through the client.

Are there any other methods that I can try? I’ll post some screenshots of what I’ve got if the explanation is confusing.

I didn’t understand, you wish to spawn on client, and that is replicated to server?
In fact, replication, is only from server to client… so spawn must be located on server side, set variable on server side… and those information will be automatically replicated to client.
You can’ t, you must’nt modify these information on client.

To modify information, you have to call RPC function

Yes I wish for the client to be able to spawn a turret. Is that not possible? Everything executes on server so should that not make it work?

It depends of what you want, if you want turret is only visible on your client, and is only interacting with your client (no network, no replicaiton and so on), you can spawn it on your client. like the ui/hud is only on your client, because it is local purpose, not concerning server, or other clients.

But it is the only case, where you create object on client… all about networking, replication and so on, must be done on server, and will be replicated to client.

from client, you will call a “server function” making spawn the turret from client (and it will be like it was client was making spawn a turret). But in fact, it will server which will make spawn, and the turret will be replicated to your client.

you see?

The “Server Function” is what I’ve got now though isn’t it? The only difference is the class which it spawns which isn’t getting replicated across.

the first blueprint seems to be correct. Do you define in turret class, that it will be replicated (generally, I create a BP from the class, visually it is easier to parameter the BP, you have just to check replicate :p). The second I can’t get what you want to do… Not sure of this one

Yeah the turret BP is set to replicate. Here’s a better image for the second one. But it basically work by once the button has been pressed it sets the local turret class to “BasicTurret”, then calls the server function which sets the players turret class to the local one.

PLease don’t name a custom event as set, it gives misunderstanding on internal set, and your set :frowning:

I didn’t understand why there is two blueprint in fact.
Turret spawn is to be possessed by a player, or it is just an AI Turret, or just spawn something that player can possess when it clicks on, or just something would be not used? because you are trying to retrieve pawn possessed and set its class? weird thing, I think.
If you want to spawn a new pawn for user, you have to unpossess actual one (and maybe destroy it) and possess the new pawn spawned.

For my mind, how I would do:
– create a custom event, execute on server, with input parameters (location)
– in this event, a simple box, spawnactor (you choose in listbox, which class), and you link location, to location pin
– on event clicked, you retrieve location where you want to spawn your turret, and you just call your custom event, and that’s all

ok in this case, you can use BP I gave to you, just remove the possess part.

I’m not even sure if you understand my issue here. I’ll run through it again.

I have a player who’s a commander of a team. Their role is to place turrets down (Like a tower defence) that will shoot the enemy. They click what turret they want to spawn from a list of buttons (This will set the class variable) and then once they click on the map it spawns the correct turret in the mouse location.

I’m not creating a new player or anything that needs possessing. All I’m doing is spawning a turret based on a class that’s given to it.

The Issue is that the class isn’t being replicated to the server so I need another way of doing it that I haven’t tried already

That won’t fix it either