#ServerRpc can't see updated property

206 messages · Page 1 of 1 (latest)

pastel dagger
#

As my understanding, when you create networkbehaviour class with property without syncvar, class holds 2 different property on both client and server. If this assumption is true, why i can't see updated variable inside of ServerRpc function?

public class Test: NetworkBehaviour { 

  private List<int> list = new();

  void Start()
  {
    list.Add(1);
  }

  [ServerRpc]
  public void GetValue()
  {
    Debug.Log("list count: " + list.Count); // why it prints 0?
  }

  void Update()
  {
     if (!IsServerInitialized) return;

     if (Input.Triggers) {
        GetValue();
     } 
  }
}
#

ServerRpc can't see updated property

hard lintel
#

Cause server do not know which value to get.. you need to put the value throgh server to update then server will know

[serverRpc]
ObserverUpdateMyValue()
[observerRpc]
Value +=1

then ask for the value

#

it is better to use syncvar if it is constantly changing. Also you can update direcly from ServerRPC .. if you do not need to change anything on observer

pastel dagger
#

just testing around and trying to syncvar bevahiour without syncvar. So what i trying to achieve is server update value time to time, client calls serverrpc when value needed. but server not accessing its value.

hard lintel
#

void Start()
{
list.Add(1);
}

this is called on locally .. not on server.. inside server also locally

pastel dagger
#

yes, but doesn't that GetValue function bind to local instance class?

hard lintel
#

locally you can ask... but not inside serverRPc

#
 void Start()
  {
   if(isOwner)
    GetValue()
  }

  [ServerRpc]
  public void GetValue()
  {
    list.Add(1);
  }
pastel dagger
#

i understand that behaviour, but i want list updated outside rpc.

#

list is updated on the server only, then i want values on client.

#

i know its perfect case for sync var, but without sync var how i get it? possible?

hard lintel
#

this is not syncvar .. this is just asking server to change my value. syncvar will autometically do that

#

but locally . do you mean in server ?

#

[Server]
void Changemyvalue(){}

pastel dagger
#

client is not asking change value just getting value? server updates value on its own.

hard lintel
#

yes now i understood

#

this is what you need

[Server]
void Changemyvalue(){
list.Add(1);
} 
#

so it will only change value in server

#

now u can call inside client

#
void Update()
  {
     if (!IsServerInitialized) return;

     if (Input.Triggers) {
        GetValue();
     } 
  }
[ServerRpc]
  public void GetValue()
  {
    Debug.Log("list count: " + list.Count); // why it prints 0?
  }
pastel dagger
#

let me try quickly on my end.

hard lintel
#

sure

#

but this will not change value in client.. only in server

pastel dagger
#

yes only on server, so client can get that updated value?

hard lintel
#

yes

#

I think you want to make it so user can;t cheat.. am i right ?

pastel dagger
#

no i doesn't work as expected. yeah basically trying out server auth cases and figure out how fishnet works overall.

#

by the time of calling serverrpc, server local values always empty, even it is updated locally on server before serverrpc call.

hard lintel
#

make sure you client is not calling this . server should exist and can get this msg

[Server]
void Changemyvalue(){
list.Add(1);
} 
#

then you are calling serverRpc from server itself

#

ServerRpc needs to call from a client

pastel dagger
#
if (!IsServerInitialized) return;

this prevents it right?

hard lintel
#

no

#

there are two ways

#

one is using
[Client] from client

#

another is you can call form may be
public void overrive OnStartClient()
{
}

pastel dagger
#

how do you differentiate inside Update?

hard lintel
#

update will call all the time

#

but make sure you have a client who is calling from update

#

do you have a player ?

pastel dagger
#

yes i have

hard lintel
#

then update will work

pastel dagger
#

update function will called on both server and client all the time right?

hard lintel
#

and from where are you calling the [Server] ??

#

update will call on client only not on server unless [ServerRpc]

pastel dagger
#

i see, i thought if (!IsServerInitialized) return; this differentiate that update call of server and client.

hard lintel
#

no

pastel dagger
#

so there is no Update method on the server?

hard lintel
#

[Server] can be called only if server has the object
[ServerRpc] will be called from client to server.
So Update to ServerRpc is like a update on server

pastel dagger
#

gotcha. trying on my end

hard lintel
#

There is an update method on server.. but its for different purpose . like authoritive movement.. TickUpdate..

You should use it only if you are using rigidbody movement

#

also update is bad on server.. it will take too much bandwidth.. so its better not to call on everyframe on server

pastel dagger
#

ok, lets say server updates its own value inside tickupdate. how client gets that updated value?

hard lintel
#

its just like normal update.. but inside server.. there is a video on Fishnet official youtube channel to understand it better

pastel dagger
#

sorry, if its obvious, but i'm preparing my mental set for coming project 😄

hard lintel
#

its called Authorative movement

#

thast great to hear 😄

pastel dagger
#

so that movement, combined with transform component it gets value of the position right?

hard lintel
#

I am also working on my own shooting project

#

Network transform will only sync the player possition .. but still player can cheat with lets see speed 100 in their local machine.. how can server detect ?

#

thats when tickupdate needed

pastel dagger
#

sure, there needed some verification before executing.

hard lintel
#
#

so it will roll the player back where it should be

pastel dagger
#

i watched it but haven't get my hands on it yet. need to fundamentally understand it correctly first

hard lintel
#

i think its better not to touch it at first.
first make something with normal Rpc calls and syncvars .
then when everything is almost done then impliment this

pastel dagger
#

yeah thats what i'm trying to do it. while testing those this issue arise

hard lintel
#

ahh i see

#

server calls can be tricky sometimes

pastel dagger
#

i tested all rpcs and sync var it works ok. but soon i changed syncvar to regular property this comes.

hard lintel
#

ohhh

#

are you trying to change name or something ?

pastel dagger
#

just tinkering around when i should use syncvar when not

hard lintel
#

its not possible to do it like that

#

i see

#

its impossible to set name with that from client

#

you have to have syncvar

#

cause it doesnt update fast enough

#

there is a small bug

#

from mirror

pastel dagger
#

just trying to server updates its own value, client get that value when needed. without syncvar

hard lintel
#

then you can but need a coroutine or something.. waitforseconds(2) .. because your player do not spawn before you are trying to access the value

pastel dagger
#

thats why i put input triggers there wait for while and hit the trigger

hard lintel
#

ahh haha

#

anyway is it working ? [Server]

pastel dagger
#

it seems not sadly

hard lintel
#

make a empty Gameobject with networkobject. then add a script to it.. call from there

#

it will work

#

or have to use [serverRpc]

pastel dagger
#

is it related to reference types, cause primitive types working

#

if i put primitive type property it can be read inside serverrpc

hard lintel
#
List<int> myint = 0;
public override void OnStartClient()
{
  ServerChangeMyServerValue();
}
[ServerRpc]
private void ServerChangeMyServerValue(){
myiny +=1;
}

void Update(){
  if (Input.Triggers) {
      ServerGetValue();
  } 
}

[ServerRpc]
private void ServerGetValue(){
debug.log(myint[0]);
}
#

try this

pastel dagger
#

i don't want to change my value from client

#

server changing value

hard lintel
#

then you have to call from somewhere else

pastel dagger
#

server changing value on its own. without client

hard lintel
#

wait let me try

pastel dagger
#

i tried on fresh project and it works with list.

hard lintel
#

Well i tried many ways.. but in in update it will not work. but [ServerRpc] is working fine. only changes the value in server not in client

#

so u need some trigger in update may be

#
public List<int> myValue = new();

public override void OnStartClient()
{
    base.OnStartClient();
    if(IsOwner)
        StartCoroutine(Delay());
}

private IEnumerator Delay() {
    yield return new WaitForSeconds(3);
    ServerChangeValue();
}   
[ServerRpc]
private void ServerChangeValue() {
    myValue.Add(1);
}
pastel dagger
#

it works on my end. maybe because of server client starts in same editor. let me try with seperate builds.

hard lintel
#

server value has changed but this is very bad practice i would say

#

you need syncvar for this

pastel dagger
#

in your example you still changes value inside ServerRpc, it should just gets value

hard lintel
#

it should with a trigger

#

but what is the purpose for this ? i dont think you will use something like this

pastel dagger
#
public class Test : NetworkBehaviour
{
    private float test;
    private List<int> list = new();

    public override void OnStartServer()
    {
        base.OnStartServer();
        test = 10f;
        list.Add(1);
    }

    // Start is called before the first frame update
    public override void OnStartClient()
    {
        base.OnStartClient();
        GetValue();
        GetClientValue();
    }


    [ServerRpc]
    public void GetValue()
    {
        Debug.Log("count on server: " + list.Count + " " + test);
    }

    [Client]
    public void GetClientValue()
    {
        Debug.Log("count on client: " + list.Count + " " + test);
    }
}
#

there is no purpose just look into how it behaves.

hard lintel
#

ohh

#

we usually dont use OnStartServer

#

very rare cases

#

for getting events

pastel dagger
#

on the example, both debug prints same value. why?

hard lintel
#

my example ?

pastel dagger
#

my example

#

it should be different right

hard lintel
#

so basically you are first adding 1 to list to client and server both

#

even when you are using OnStartServer, it is running on the player which is a client

#

no my mistake

pastel dagger
#

i'm seeing this

hard lintel
pastel dagger
#

yeah

#

so unless i fully understand those i can't go further properly. its main purpose kk

hard lintel
#

but server may be setting the value before spawning may be.. not sure how it is interacting

#

see my example

#
public List<int> myValue = new();

public override void OnStartClient()
{
    base.OnStartClient();
    if(IsOwner)
        StartCoroutine(Delay());
}

private IEnumerator Delay() {
    yield return new WaitForSeconds(3);
    ServerChangeValue();
}   
[ServerRpc]
private void ServerChangeValue() {
    myValue.Add(1);
}
pastel dagger
#

i just create fresh project and attach script got gameobject. which is spawnable prefab

hard lintel
#

this is updating on server only

#

it will not change on client

#

but reading from it is another challenge

#

cause update will throgh error

#

try my code and build 2 instances

#

you will see it is updating on server only

pastel dagger
#

can you try my example, its clearly showing that value is changed only on server, but client also prints same.

#

i'll try with different build

hard lintel
#

ohh

#

ok wait a sec

#

your code is working fine

#

only server printing the value

#

client is 0 0

#

which is suppose to happen

pastel dagger
#

mine prints same values. how?

hard lintel
#

build 2 instances

#

run 1 server 1 client

pastel dagger
#

ah i see. it prints same on editor

hard lintel
#

ofc client is the server there

pastel dagger
#

unless i have 2 editors, can't truly rely on what i code hehe

hard lintel
#

not really. there is a tool

#

search in Asset Store "IngameDebugConsole"

#

this will help

pastel dagger
#

nice

hard lintel
#

or if you have good pc you can use parrallel sync

pastel dagger
#

i'm using parrelsync

hard lintel
#

ohh

#

then it will work 😛

#

just dont use server and client at the same time

pastel dagger
#

yeah, just some of the parts i'm thinking that first editor is server and client, but it turns out it become client host i guess.

hard lintel
#

it runs little slow on my pc.. so i use that debug console

#

yes if you run both it will become host and will have the power of server

pastel dagger
#

so need to those flags heavily kk

hard lintel
#

in parrallel sync one can be server another can be client

pastel dagger
#

yeah thats how i testing mostly. so need to blockout that client host features on my code and try again on my original issue kk

hard lintel
#

cool

pastel dagger
#

have you done that client predicted movement?

hard lintel
#

I have tried it but did not use it on my game

#

using custom character controller

#

so i am doing it little differntly

#

more over same thing

pastel dagger
#

i'm about to try it on my end. currently movement happens on server. client moves like glitchy. i thinking that it moves smoothly once i add the prediction

hard lintel
#

cool try it. its little tricky. also try collider rollback

#

for bullets

#

there is some examples

pastel dagger
#

sure thing. i wish that documentation be little bit more extensive

hard lintel
#

same here 😛

#

but only few ppl are contributing mostly 1 on fishnet.. so its hard for them to keep making tutorials while updating fishnet

pastel dagger
#

yeah understand that.

hard lintel
#

There is a lobby project and a Fps land project that can help a lot

#

lobby project going to be hard to understand for sure

pastel dagger
#

i just get pro yesterday to try out how it deals with network delay etc

hard lintel
#

i made my own lobby .. cause it was so intense it could not fit on my brain

pastel dagger
#

i remember it was pain, when i tried to have proper chat application over the websocket with nodejs.

hard lintel
#

You might need it later

#

my lobby project

pastel dagger
#

nice man. thanks. it will help definetely

pastel dagger
#

i just wonder, is it proper place to chat like that hehe

hard lintel
#

This is a little template i have although it might have very small bugs

#

well It is your post. if you need help we can talk about anything regarding help

pastel dagger
hard lintel
#

Should be interesting to work on

#

is it multiplayer or just bots ?

pastel dagger
#

setup everything on backend part. so dealing with unity part.

#

it is multiplayer

hard lintel
#

Wish you the best of luck for this game

pastel dagger
#

thanks, same to you. thanks for taking your time and supporting me.

hard lintel
#

i would suggest you to start with small templates.. and make templates always so you can use it for further proejcts

#

while making the game

pastel dagger
#

noted. i'm just starting play around with different engines. So unity reminds me that good old jquery stuff. lots of plugin kk.

hard lintel
#

lol jquery xD

pastel dagger
#

same feeling lol

hard lintel
#

anyway have a great day