A short question. Where is the flag to check if the code runs on server or client?

Place to get help with not working mods / modding interface.
User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 3017
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

Re: A short question. Where is the flag to check if the code runs on server or client?

Post by boskid »

There is no such thing as a flag that tells you if code runs on server or not. There was never a need for such a thing and there is no reason to have something like that. Reason is simple: you should never ever change your script behavior on that, because in multiplayer your script is in fact being executed in muliple instances synchroniously at the same time: on the server and on all of the connected clients. If those multiple instances would at any point in time produce an observable difference in game state, then the game desyncs and a client for which script gave a different result is disconnected from the server.
yagaodirac
Fast Inserter
Fast Inserter
Posts: 152
Joined: Sun Jun 16, 2019 4:04 pm
Contact:

Re: A short question. Where is the flag to check if the code runs on server or client?

Post by yagaodirac »

boskid wrote: Fri Apr 23, 2021 4:55 am
Thanks for replying.
The purpose is that, I need to display something for players. They are trivial and not worthy any memory on server.
But if there is not such a mechanism, it's the same for me to do it in a unified script and cost some extra memory on server.
User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 3017
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

Re: A short question. Where is the flag to check if the code runs on server or client?

Post by boskid »

yagaodirac wrote: Fri Apr 23, 2021 5:09 am The purpose is that, I need to display something for players. They are trivial and not worthy any memory on server.
But if there is not such a mechanism, it's the same for me to do it in a unified script and cost some extra memory on server.
By doing that you would literally produce a desync. Things that are displayed to the player are (almost) always a part of game state so if an instance on the player to which you want to print would game.print a message (=change game state) while the server would not follow the same print code, game state would diverge and desync would occur.

I see no reason in trying to optimize memory usage so much on particular instances. Having all instances produce the same output is by far the most important thing and if that requires more memory then i am fine with that.
yagaodirac
Fast Inserter
Fast Inserter
Posts: 152
Joined: Sun Jun 16, 2019 4:04 pm
Contact:

Re: A short question. Where is the flag to check if the code runs on server or client?

Post by yagaodirac »

boskid wrote: Fri Apr 23, 2021 5:24 am
OK, thanks.
The feature is already done and works properly and not causes any heavy performance issue.
Post Reply

Return to “Modding help”