As is often the case when taking online classes about programming, the instructor, and probably most of the class, use Windows. Usually, I am one of the few, if not only, Linux user(s).
This typically doesn’t present too much of a problem, but can occasionally cause an issue, especially when it comes to installing a certain software, or version thereof, though I typically stick to classes that have Linux versions of the programming tools available, or else I wouldn’t be taking the class… Sometimes, however, you can run into very interesting little items that will cause you to have unexpected results. Such is the case with a class I am taking about programming in Godot.
The particulars of this instance was that the instructor, in the prerecorded video, was showing different ways to determine if your computer was the server in a network using Godot’s network stack. During the class, he mentioned that you can use this code to make that determination:
if local_player_id == 1:
# Then do this stuff, because you are the server.
Essentially, he stated that the server is always local player ID number 1. And so if you check to see if you are local player ID number 1, and you are, then you must be the server. He then went on to demonstrate this, and sure enough, it worked great in his demonstration.
However, I tried it in my instance of Godot running on Linux, and it didn’t work. Since I was just learning the Godot programming language, I didn’t know why exactly, so I decided to do a little troubleshooting with this debug statement:
print("My id is " + str(local_player_id))
And what I found in the standard output was that my server ID was always 0.
Interesting! Then it would appear that in Windows, if you are the server, your ID is always 1, and in Linux, your server ID is always 0. That seems like a big problem in the code, because it will depend upon which machine operating system you use to compile it.
Now, in the instructors defense, he did show other ways to programmatically determine if you are the server. Here is the one that I think works best, because it will not depend upon a hard coded number, nor will it require you to check what you are compiling on:
if get_tree().is_network_server():
# Then do this stuff, because you are the server.
What I’m learning from the course is really fun and great things relating to making video games. What I’m learning as a Linux user in a typically Windows environment is to be careful about hard coded numbers. Especially since Windows’ first number is 1, and Linux’s first number is 0 in Godot’s network stack.
Linux – keep it simple.