Trying to replicate my weapons system, issues with replicated map of strings

Basically I have a data table for my weapons and a struct for the stats. This map of strings makes it easy to type in the column from the data table that i need. For instance this check is seeing if the item we detect from the line trace is already one of the current class we have (ie secondary) but clients always return false to this check and thus, they report that they don’t have that weapon already and just keep picking up weapons of the same class. Does anyone know how I can adjust my logic so that this can be replicated? Below is some more of the flow.

Basically I am running a line trace every 10ms to detect items, once a player interacts with the item, it is attached to correct mesh. Picking up new weapons is replicated fine. But for swapping, the server functions and replicates to clients properly, but for clients the code doesn’t make it past the check in the first posted code.


Clients are able to use this code but always return false, thus they dont know they already have the weapon. I want the check to determine if the class of the weapon matches the one we currently have equipped (ie seconday), and if true, we run swap logic. But being that you cannot replicate maps of strings variables, my isvalid check returns false for the clients.

Any insight on how to make this function work for clients would be greatly appreciated.

Well, maps cannot replicate, so you’ll have to find a different way to do this.

Looking at your graphs, using a map and having as many branches as map keys, seems counterproductive to me. The whole point of using such a data structure would be to refactor everything into a single execution flow. In your graph, you’d be better off with using direct variables instead of a map, ie. one variable for the Primary weapon struct, one for Secondary weapon struct, and one for the Nuke weapon struct.

Another point worth mentioning is, checks should always be made on the server. Do the client(s) really need to make them ? As a general rule, server should never trust clients, but on the opposite, clients can trust the server.

Finally, if you really wanna stick to such a data structure, simply switch to Array instead. Arrays replicate fine and in an optimized way. So instead of having the key in the map, put it in the weapon info struct. Then when you need to find “Secondary”, iterate the array (foreach) and look for the first item with key “Secondary”. This may look suboptimal, but keep in mind accessing a Map is not instantaneous either, it requires hashing the key which may even be slower than iterating array when the array is small.
Alternatively, instead of referencing by string “Secondary” “Primary” etc, assign an arbitrary index to each weapon type, ie. 0=Primary, 1=Secondary, 2=Nuke, etc. then just access the array at desired index. Just make sure to pre-define array size at initialization to avoid indexing out of bounds.

1 Like

I appreciate the response. I have made another replicated weapon system that uses physics and info is stored in the class defaults of the weaponbp rather than a struct, which used a simple integer equip index and an array of current weapons which would set the “PrimaryWeapon”, “SecondaryWeapon” etc, that all works fine, but I get confused when trying to merge this logic from another project.

So you are saying inside my detectediteminfo struct, i can store the keys and pull them from there rather than using the map? I am very new to game dev and coding. These both i scraped together from numerous tutorials. You’d think, why don’t I just run with my old system, the problem is, i don’t want to use physics on my pickups or drops (ie player dies or drops, gun just goes to the floor below them or replaces old actors location and rotation), and I want to use the line trace detection to avoid pickups being to close together when trying to interact. I also like the simplicity of using a struct and data table, but how much more efficient is that than just using class defaults?

I tried implementing basically the same logic as far as setting the indexes and stuff with this project, but haven’t got the desired result with using the data from my struct that is filled on the line trace detection. I’m probably asking too much here lol. I do have it working now replicated with sphere collisions, i just want to make that line trace work and I just don’t know how to implement that here instead of sphere: