Creation of my own Inventory System in Unreal Engine
Some might ask, why would I start making an inventory system when there is so many of those on the marketplace already?
There are, yes, but none of them have all the features I want, and some of those are:
- Multiplayer Replication
- Client Prediction
- Server authoritative architecture
- Ability to make more inventories from one array
Yes, the inventory will have full multiplayer capabilities but multiplayer is not required.
Let's talk about these features and get a bit more technical.
Replication is handled using a FFastArraySerializer array which means that when changes happen, only the changed elements of the said array have to be sent over the network. This eliminates the need to send hundreds or even thousands of slots every time we move one item.
Client prediction is also implemented for all UI functions which ensures a smooth experience even with high latency.
Every step, while predicted on the client, is verified on the server to ensure no cheating happens. Inventories of item containers in a world are not replicated to clients at all before they open them to prevent cheating.
Distance checks are implemented to make sure that when the player goes away from the container, it automatically closes the UI and stops replicating the inventory contents.
Actually, let's talk about distance checks a bit more. In Unreal, the normal way to calculate the distance between two vectors is using FVector::Dist(Vector1, Vector2) which uses the following formula:
$$ d = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2 + (z_2 - z_1)^2} $$
This is actually the first time I learned something useful in high school.
Because of the square root, the formula is slow, so we can use FVector::DistSquared(Vector1, Vector2) and compare squared distances instead.
Now, what do I mean by " The ability to make multiple inventories from one array " ?
Imagine you have one inventory component on a player character. It only has one inventory array, but we want to have a hotbar and armor slots. We can just utilize the InventoryPart feature of the inventory, which enables us to define which inventory each slot belongs to. For example, indices 0,2 and 4 to 19 can belong to the backpack while indices 1 and 3 belong to the hotbar. Of course, we can order them, but we don't have to.
Alright, to finish off this first post, why am I even making a devlog?
The single biggest reason is that it will keep me motivated to work on the inventory system more.
We have reached the end of the first post, stay tuned for more!
Comments ()