Jump to content

Creating game-states using net_vars


Recommended Posts

I was getting into this topic in my previous thread, but I didn't want to keep bumping it after so long. I'm still trying to wrap my head around them though, and every time I think I've figured them out, something else comes along that I can't quite do with them.

In learning to use netvars, my previous misunderstanding was that a netvar was just one variable that became constant between the server and all clients, which I now know they are separate for each client. But now I actually am looking for a way to create a variable that is constant between the server and all clients, ideally. What I'm trying to do is create some sort of "game state" that defines the state of the current game, whether it be "waiting" or "running" or something of the sort. But I'm having trouble finding a way to get a gamestate value to the client quick enough (or at all). 

The mod "The Hunt" does something similar to this, but I don't think it's a method that will work for me, as the network handles everything, and it doesnt look like anything is actually pulled by any of the prefabs, just some values thrown into some hud elements created by the single component it uses. But I need the player's prefab to be able to pull this gamestate value right away, as it affects how they are created. What would be the best way to even store a variable like game state? The world?

Link to comment
Share on other sites

edit: In original post I mixed up RPC and netvars, therefore now the (I think now correct) description of both:

netvars are used for:
"client and server add at same time the netvar to any instance. If the server is changing the value of that netvar, it is propagated to all clients that know the same netvar and you can even make them to call a function"

And RPC are for:
"The client can make the Server to call a function"

So if you now want a value that is the same for all clients, I think you can simply add a netvar with the same name to every player in AddPlayerPositInit. When the server is changing this value, all clients with same netvar are informed of the change. And I think that is what you are looking for?
 

Edited by Serpens
Link to comment
Share on other sites

It's the "opposite". Quotes because the RPC are not for client to set value on the server side, but for the client to request the server to run a function for him.

What type of info are you exactly trying to share? I have trouble imagining what you want to achieve with this "waiting" or "running" example.

Edited by ZupaleX
Link to comment
Share on other sites

23 hours ago, ZupaleX said:

It's the "opposite". Quotes because the RPC are not for client to set value on the server side, but for the client to request the server to run a function for him.

What type of info are you exactly trying to share? I have trouble imagining what you want to achieve with this "waiting" or "running" example.

It's to determine how much of the player is spawned, basically. When the player logs in, if the game is currently in "running" state, only half of it's normal components are applied, and skipping a few other things like showing the character select screen and setting the build/bank anims. I'm having trouble getting the client side to recognize what state the game is in when joining, and I feel like I'm not even sure if I'm doing it right. I spent most of today messing around with a handful of different versions that didn't include a netvar at all, but all of them fell flat for different reasons that I never really understood.

Link to comment
Share on other sites

You want to check a netvar attached to the player instance during the constructor for that player? I might be wrong but I don't see how that be done cleanly.

What you might want to do is add a tag to something permanent, like TheWorld. Something like "GameState.RUNNING" or "GameState.WAITING".

Then in the player constructor, check for that tag.

Edited by ZupaleX
Link to comment
Share on other sites

17 hours ago, ZupaleX said:

You want to check a netvar attached to the player instance during the constructor for that player? I might be wrong but I don't see how that be done cleanly.

What you might want to do is add a tag to something permanent, like TheWorld. Something like "GameState.RUNNING" or "GameState.WAITING".

Then in the player constructor, check for that tag.

Okay, I tried that, but it didn't work. I've been messing around with it for a few hours to make sure. I've been trying a lot of similar approaches this past week using an anchor prefab that I use to handle most of the match function stuff like that, but none of the approaches seem to work. I've gotten most of those approaches, including this one, to work to the point where after a player has been logged in, matches are able to start and restart while players can de-spawn and re-spawn and the constructors can set everything up the right way. But as soon as the player logs out and logs back in, the tags that were supposed to be there just aren't visible.

Anyways, I thought you couldn't add tags to entities and then have them visible to clients who log on after they've been added? Wouldn't the tags need to be added after they've already joined the game? (which would be too late anyways) I'd assume that's why they couldn't see the tags on the world.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
  • Create New...