Loading data that is common to client and server

Hello Heroic Labs community,

When it comes to make an online game, you always have items or characters that have stats.
Let’s say i have a sword, Excalibur, that have 40atk power.
Let’s also say that the stats of Excalibur cannot be changed by the player, it is a constant value.

The thing here is i have to store this data somehow both in server and client.
From a client perspective, i need to be able to check how much atk power my Excalibur sword have.
From a server perspective, i need to be able to compute how much damage my player did to its opponent.

Now let’s say i want to update my game and buff Excalibur to have 50atk power.
I then have to change my constant values both on server and client side. This can be a pain to change it on both side everytime you want to tweak a value or a stat when play-testing for example.
To solve this issue the obvious solution is to store this value in files shared by both server and client. This way i just need to tweak the stats of Excalibur in my weapon_stats.json and the modifcation is made in both ends.

So here is my question: what is the best way to store constant values like this in Nakama ?

More specifically i want to be able to load them automatically at each startup.
The game I’m trying to make is going to use the match handler feature. So i could just load this data inside the MatchInit() and store it for the duration of the match; but this would mean that everytime a match is created a whole copy of the data is loaded into memory and this bothers me for obvious optimisation reasons.

One approach i can think of is to make a module that would load the data and update some Collections.
I feel like this could work well but somehow i also feel like Collections are more made for storing player data or stuff like that and not constant values.

So yeah i don’t know if there is some sort of conventional way to do this or something, any insight or criticism on what i said is greatly appreciated.

(sorry if poor english i’m not native)
(also sorry if wrong category i didn’t know which one to choose between Game Design and Runtime Framework)

@Nellousan You don’t specify which Nakama runtime you use, but based on the function names I’ll assume you want to work with Go.

There’s a function available via the NakamaModule interface called ReadFile, you can use to read a file from disk in InitModule. This file might be a JSON file you can then parse and use however you need in your server-side game logic. :+1:

Collections could also work, and they would be fine for this use case, but I wouldn’t recommend that approach unless you need the data to be reloadable without a Nakama server restart. This has its own layer of complexity, so stick with the file read at startup if you can.

Hello, thanks you for the response.

Yes i indeed work with Go.

Thanks to you I just realized that i can simply read the file, store it in a variable inside my module at the MatchInit so i can access it within the MatchInit or MatchLoop functions.
Using a more complex framework made me forget basic solutions ahah.

Anyway, thank again i got what i wanted.

@zyro What about Javascript? I have the exact same issue, where I need to load data. Currently I’m loading this data on match init and keeping it in the match state (Makes my match state HUGE). But this data is constant. (For instance the list of achievement requirements.) Ideally, I would keep state, but since you tear down the VM between calls, is there any solution? I keep feeling I should have gone with Go :frowning:

@Meneleus There is an equivalent read_file function available to the JavaScript runtime, but you’re right it won’t quite solve your issue. JavaScript VM global states are not shared, and you cannot rely on them between executions of RPCs or matches. This is intentional so we can sandbox each VM.

Your best bet with the JavaScript runtime is to read the file into every match state. :+1:

@zyro Guess I’m good the way I’m doing it. It’s loading the data off storage just once.