Jump to content

Recommended Posts

Okay here's the problem. In modmain, I created a bool variable for the player(inst.isbleeding). Client side, print shows it's true, but server side (using the remote deal), print shows the variable doesn't exist. Is there something special I need to do to get this one variable to sync properly with the server?

Code:

AddPlayerPostInit(function(inst)	inst:ListenForEvent("attacked", function(inst)		if math.random(3)==1 then			if inst.components.talker then				inst.components.talker:Say"I'm bleeding!"			end			inst.isbleeding = true			inst.isbleedingnotified = true		end	end)	inst:ListenForEvent("killed", function(inst)		inst.isbleeding=false	end)	inst:DoPeriodicTask(1, function(inst)		if inst.isbleeding == true then			if inst.components.health then				inst.components.health:DoDelta(-2)			end			if inst.components.sanity then				inst.components.sanity:DoDelta(-1)			end		end	end)	inst:DoPeriodicTask(5, function(inst)		if inst.isbleeding == true and inst.isbleedingnotified == false and inst.components.talker then			inst.components.talker:Say"I'm still bleeding!\nI need to heal!"		else			inst.isbleedingnotified = false		end	end)end)

P.S. Sorry if this question has already been answered. I don't come here often. I tried searching but no keywords I could think of proved fruitful.

Edited by mouse

Nothing is synced between server and client there.

That code running client side would yield a inst.isbleedingnotified = false on the player entities.

 

And nothing more. Clients don't get the attacked or killed events pushed, or have health and sanity components.

If anything it should have happened the other way, that server side exists but client side doesn't.

 

And there is nothing to sync here, really, the calculations and messages pushed only need to be server side.

 

For syncing data, you use netvars (in netvars.lua), example:

-- This is ran on a server mod required by clients-- Thus, server side players and client side players get both the code ranAddPlayerPostInit(function(inst)	-- Both server and client entity get a net_bool	-- A netvar must exist on both sides, else crash	inst._isbleeding = GLOBAL.net_bool(inst.GUID, "player._isbleeding", "isbleedingdirty")	inst._isbleeding:set_local(false)		-- Server side	if GLOBAL.TheWorld.ismastersim then		inst:ListenForEvent("attacked", function(inst)			inst._isbleeding:set(true)		end)		inst:ListenForEvent("killed", function(inst)			inst._isbleeding:set(false)		end)	-- Client side	else		inst:ListenForEvent("isbleedingdirty", function(inst)			inst.components.talker:Say("Ouch")		end)	endend)

Okay let me see if I'm understanding all of this. Server side is where the player exists in full. But client side the player is just a sort of mock entity with only a few components right?

Another question, using the example you gave, how would I check if the value server side is true or false? Would inst._isbleeding return it's value or is there another method for getting that information?

By that I mean, would I do it like this?

if inst._isbleeding==true then--code hereend
Also, for periodic tasks, is that something that needs to be done client side or server side?

One more question, what is meant by "dirty"?

Server side is where the player exists in full

 

Yes.

 

But client side the player is just a sort of mock entity with only a few components right?

 

Yes, servers add components, then add replicas to the replicable components.

The client sticks to the replicas. The replicas hold networked info.

 

By that I mean, would I do it like this?

 

It's right in the example:

if inst._isbleeding:value() == true then-- code hereend

Periodic tasks can be ran client side or server side.

The calculations are going to be done server side, so you are going to use a server side task.

 

When a netvar changes its value, if there's an event specified (like "isbleedingdirty"), then it gets pushed to the entity.

So, for example, if you make a client side HUD to display bleediness, you may want to change its number when a dirty event gets pushed, instead of having a periodic task on the client constantly updating the displayed number.

Alright I think I've got the hang of it now. Thank you ladies and/or gentlemen.

On a side note, I was getting a lot of weird problems but when I deleted the cached_mods folder, all the weird inconsistencies cleared up. No idea if it's only me experiencing this or not.

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
×
  • Create New...