Sign in to follow this  
WhiteAutumn

Calling a function on the client from the server? [Solved]

Recommended Posts

WhiteAutumn    2

Hi!
How does one call a function on the client from the server?
Thanks in advance!

Edited by WhiteAutumn

Share this post


Link to post
Share on other sites
Serpens    521

offtopic:
Are topics here first hidden or sth like this? I'm pretty sure I checked new topics here around ~13hours ago and your thread was not here, although it says it was posted 19 hours ago.

I think your question is not clear. Who wants to call and where is the function to call (one way uses netvar and the other RPC)

Edited by Serpens
  • Like 1

Share this post


Link to post
Share on other sites
WhiteAutumn    2
1 hour ago, Serpens said:

offtopic:
Are topics here first hidden or sth like this? I'm pretty sure I checked new topics here around ~13hours ago and your thread was not here, although it says it was posted 19 hours ago.

I think your question is not clear. Who wants to call and where is the function to call (one way uses netvar and the other RPC)

You're right!
For whatever reason since I created my account my posts need to be approved by mods before they become public :/
KiC1qex.png
edit: Looks like a mod while reading this message for approval removed my restriction! That or you need 20 posts approved before you can post without that limitation.

Let me elaborate. I have the a listener for whenever shelter changes however that event only gets fired on the server and I need to update a thing on the client. I had the impression RPCs only work from client -> server but perhaps that's wrong?

Edited by WhiteAutumn

Share this post


Link to post
Share on other sites
Serpens    521
1 hour ago, WhiteAutumn said:

Let me elaborate. I have the a listener for whenever shelter changes however that event only gets fired on the server and I need to update a thing on the client. I had the impression RPCs only work from client -> server but perhaps that's wrong?

are you sure you need to update sth on the client? client is mostly for visuals.

RPC is called from a client to make the server call a specific function.
netvar is set from the server and all clients (and also the server) will get kind of event that this value was changed and the new value of it.

AddPlayerPostInit(function(inst)
    inst.youruniquenetvarname = GLOBAL.net_bool(inst.GUID, "youruniquenetvarname", "youruniquenetvarEventName") -- true or false
    inst.youruniquenetvarname:set(false) -- set a default value
    if not GLOBAL.TheWorld.ismastersim then -- add listeners to this event only for clients
        inst:ListenForEvent("youruniquenetvarEventName", function(inst)
            local youruniquenetvar_newvalue = inst.youruniquenetvarname:value()
            -- your code to do stuff at client depending on the new value 
        end)
    end
end)

and anywhere at server (you may check ismastersim if you are not sure if it is server or client) you can now do:
inst.youruniquenetvarname:set(newvalue)
to make the clients get your event and new value

There are differnt kinds of netvars. above I use "net_bool" which can only have true or false as value. If you set it false, while it is already false, no event is fired (same for true). This can be used for an on/off switch.
See scripts/netvars.lua to find more kinds of netvars. It is best to use the tiniest one of them, to save ressources. So first try to achieve your gaol with bool, or net_tinybyte with numbers from 0 to 7

Edited by Serpens
  • Like 1

Share this post


Link to post
Share on other sites
WhiteAutumn    2

Something visual is exactly what I need updated sadly, a widget. I have modified the bloodover widget to show red if the player is in daylight and not sheltered. This works fine when the user is the host however when they are a client the sheltered event only gets fired on the server which means while the user does not take damage while standing under a tree they still see the red overlay because the client has not had a chance to update it.

I suppose I could have a periodic task on the client checking if it needs to update the overlay ever second but that seems dirty.

Edited by WhiteAutumn

Share this post


Link to post
Share on other sites
Serpens    521
12 hours ago, WhiteAutumn said:

Something visual is exactly what I need updated sadly, a widget. I have modified the bloodover widget to show red if the player is in daylight and not sheltered. This works fine when the user is the host however when they are a client the sheltered event only gets fired on the server which means while the user does not take damage while standing under a tree they still see the red overlay because the client has not had a chance to update it.

I suppose I could have a periodic task on the client checking if it needs to update the overlay ever second but that seems dirty.

yes, you can use the net_bool in this case to switch it on/off :)
I'm not familiar with the widget, I only saw your thread about it, but since I have no clue about it, I did not response... but isnt there really no way to use existing functions, called on server, that already include this overlay? Eg. you could simply start the "overheat" mechanic, this will handle everything on its own.

Edited by Serpens
  • Like 1

Share this post


Link to post
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

Sign in to follow this