Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6864451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:54:48+00:00 2026-05-27T02:54:48+00:00

In the code for my networked game, the client receives a message that a

  • 0

In the code for my networked game, the client receives a message that a player entity is controlled by the user or solely by the server.

UserControlled = msg.ReadBoolean();

This variable is set in absolutely no other location other than when it is instantiated with a default value of false. I’ve double checked this with the “Find all references” feature in Visual C#. In addition, there is no local variable within the scope of this statement with the same name. I’ve found that within the scope of the statement above, everything is find and dandy; the variable is set to true as shown by printing it to the console directly afterwards. At this point, strange things happen. Any time the variable is referenced from outside my player’s class, it correctly displays as true – but any time it is referenced from inside the player’s code, it returns the default value that the Boolean was instantiated as; either true or false.

I’m clueless beyond measure as to the problem, and any information that would lead to the solution is much appreciated 🙂

If you’d like more code, I can certainly provide it. I just wasn’t sure what else to include other than random references to the variable, so I decided to leave them out.

Thanks!

Edit for more code:

UserControlled is defined as a normal class-wide variable.

public bool UserControlled = false;

The method that sets the value:

public override void ParseUpdateMsg(NetIncomingMessage msg) 
        {
            switch (msg.ReadByte())
            {
                case 0: // positional message
                    //snip
                    break;
                case 1: // other info
                    // The following simply reads in the information
                    // I'm using Lidgren Network Library. It's extensively tested so I don't suspect that it's the issue
                    Speed = msg.ReadInt16();
                    Username = msg.ReadString();
                    UserControlled = msg.ReadBoolean();
                    MovingDir = msg.ReadString();
                    Health = msg.ReadInt16();
                    Hunger = msg.ReadInt16();
                    Mana = msg.ReadInt16();
                    // The following is simply debugging information to the screen
                    // As said above, UserControlled at this point is correctly displayed as True
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("--------------------------");
                    Console.WriteLine("Update message received!");
                    Console.WriteLine("ID: " + ID + "\tType: " + this.GetType().ToString());
                    Console.WriteLine("Speed: " + Speed);
                    Console.WriteLine("Username: " + Username);
                    Console.WriteLine("UserControlled: " + UserControlled);
                    Console.WriteLine("MovingDir: " + MovingDir);
                    Console.WriteLine("--------------------------");
                    Console.ForegroundColor = ConsoleColor.White;
                    break;
            }
        }

The following is the client code which receives the message and passes it to the player entity to evaluate:

switch (msg.ReadInt16())
            {
                case 1:
                    // Entity message
                    switch (msg.ReadByte())
                    {
                        case 0: // Create entity
                            //snip
                            break;
                        case 1: // Update entity
                            int id1 = msg.ReadInt16();
                            Entities[id1].ParseUpdateMsg(msg); // the player entity is passed the information
                            // At this point, there is only one entity, and that is the player.
                            // More debug information. Still displayed correctly as true.
                            Console.ForegroundColor = ConsoleColor.Magenta;
                            Console.WriteLine("--------------------------");
                            Console.WriteLine("-analysis 1-");
                            Console.WriteLine("(Within scope of update-entity message)");
                            Console.WriteLine("UserControlled: " + ((ClientEntPlayer)Entities[id1]).UserControlled);
                            Console.WriteLine("--------------------------");
                            Console.ForegroundColor = ConsoleColor.White;
                            break;
                        case 2: // Destroy entity
                            //snip
                            break;
                    }
                    // Displayed correctly here as well!
                    Console.ForegroundColor = ConsoleColor.Magenta;
                    Console.WriteLine("--------------------------");
                    Console.WriteLine("-Post-analysis 2-");
                    Console.WriteLine("(Within scope of generic entity message)");
                    Console.WriteLine("UserControlled: " + player.UserControlled);
                    Console.WriteLine("--------------------------");
                    Console.ForegroundColor = ConsoleColor.White;
                    break;
            }

All debug code above is correctly displayed as True after the value is set. However, once we start getting into timed checking, it starts to become incorrect (but still only from the player’s code!)

The following is from the client’s update code. It displays correctly the UserControlled value every 5 seconds.

if (gameTime.TotalGameTime.Seconds % 5 == 0 && player != null && debugTime != gameTime.TotalGameTime.Seconds)
            {
                Console.ForegroundColor = ConsoleColor.DarkMagenta;
                Console.WriteLine("--------------------------");
                Console.WriteLine("Client's Timed Post-Analysis");
                Console.WriteLine("Elapsed time: " + gameTime.TotalGameTime.Seconds + " sec");
                Console.WriteLine("(Within scope of client update)");
                Console.WriteLine("UserControlled: " + player.UserControlled);
                Console.WriteLine("--------------------------");
                Console.ForegroundColor = ConsoleColor.White;
                debugTime = gameTime.TotalGameTime.Seconds;
            }

This is the part that shows the player’s value is incorrect after the function is left. It displays false (and other code that relies on the player being controlled, like responding to keyboard input.) It’s essentially the same code as the client’s timed debug information, only instead it’s inside of the player’s update function.

if (gameTime.TotalGameTime.Seconds % 5 == 0 && debugTime != gameTime.TotalGameTime.Seconds)
            {
                Console.ForegroundColor = ConsoleColor.DarkYellow;
                Console.WriteLine("--------------------------");
                Console.WriteLine("Player's Timed Post-Analysis");
                Console.WriteLine("(Within scope of player update)");
                Console.WriteLine("Elapsed time: " + gameTime.TotalGameTime.Seconds +" sec");
                Console.WriteLine("UserControlled: " + UserControlled);
                Console.WriteLine("--------------------------");
                Console.ForegroundColor = ConsoleColor.White;
                debugTime = gameTime.TotalGameTime.Seconds;
            }

That’s about it. Thanks for helping.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:54:48+00:00Added an answer on May 27, 2026 at 2:54 am

    As my comment above says, I can’t tell you what is happening, but can only give advice on how to debug.

    Firstly, you say you’ve checked this but ensure that you are setting the correct UserControlled. If it’s a member of the class (assume it’s a property?) then prefix it with this.

    this.UserControlled = msg.ReadBoolean(); 
    

    From what I understand in your question, UserControlled is true when interrogated inside the class, but false when queried from outside the class. This can only be two things:

    1. You have two different properties, or the property is evaluating differently due to side effects.
    2. You have two instances of your class

    To investigate further, is your property doing anything clever – i.e. is it doing anything other that setting and getting a backing field? Are you sure that you’re using a single instance of your class?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Code for server: http://stikked.com/view/64826511 Network Code for client: http://stikked.com/view/38974838 Basically, the client connects to
I'm writing a UDP server that currently receives data from UDP wraps it up
I'm programming an online game for two reasons, one to familiarize myself with server/client
I'm writing a small game in java. There's a server and a client module.
I am trying to write a game server that supports multiplayer over a network.
I am modifying my PHP network's code to have user roles like wordpress here
I want to test a piece of code that uses network (the NSURLConnection class,
I get a 500 internal server error when I try to run the code
I'm using a ServerSocket on my server and Sockets that use ObjectIOStreams to send
I made a networked game in Java using Sockets. It works great, except... only

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.