Forum C# Not having statics is killing me

Not having statics is killing me

Postby dsaunder » December 15th, 2011, 11:13 pm

I know I already have a thread about this, but will statics ever be allowed in PlayerIO? It's gotten to the point where I'm considering other tech just to be able to write better code. This code should work:

Code: Select all
public struct CreateStructureMsg : INetworkMessageBase
{   
   public string StructureName;
   public int XPos;
   public int YPos;

    public static readonly CreateStructureMsg EmptyMessage = new CreateStructureMsg();

   public void PrepareForSend( ref Message message )
   {
      message.Add( StructureName );
      message.Add( XPos );
      message.Add( YPos );
   }

    public void Unpack( Message message )
    {
        StructureName = message.GetString( 0 );
        XPos = message.GetInt( 1 );
        YPos = message.GetInt( 2 );
    }
}


Is there any way I can allocate a struct on the stack without having to do this every time:

Code: Select all
CreateStructureMsg msg;
msg.StructureName = string.Empty;
msg.XPos = 0;
msg.YPos = 0;


As you can imagine, if I have to change the struct this becomes a maintenance nightmare.

-D
dsaunder
 
Posts: 23
Joined: July 22nd, 2010, 7:28 am

Re: Not having statics is killing me

Postby Henrik » December 16th, 2011, 12:40 am

dsaunder wrote:I know I already have a thread about this, but will statics ever be allowed in PlayerIO?

No.

dsaunder wrote:As you can imagine, if I have to change the struct this becomes a maintenance nightmare.

Code: Select all
var msg = new CreateStructureMsg();

The above creates a new instance of your struct where all the fields are intialized to their default values, which is null, 0, and 0, respectively. So that's a non-complicated one-liner for that, no matter how large your struct is.

If an empty instance of your struct needs its fields initialized to something other than the default values, make a constructor that takes a bogus parameter.

If you need to keep a single empty instance around for re-use, instantiate it in your Game<P> class.

Or just make the thing a class so you don't have to care about the special rules for structs.
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm

Re: Not having statics is killing me

Postby dsaunder » December 20th, 2011, 2:41 am

Using "new" with a struct allocates it on the heap, not the stack which is what I want to do. I just wasn't sure if there was some magic way of doing it that I wasn't aware of, seems like there isn't. Using new DOES initialize the struct, but for whatever reason the C# spec doesn't let you do a stack allocation with the generated default constructor.

This code doesn't even work because you can't use an unititialized struct:

Code: Select all
public struct TestStruct
{
    public int x;
    public static void Init( ref TestStruct t )
    {
        t.x = 0;
    }
}

...

        TestStruct test;
        TestStruct.Init( ref test );
...


I'm trying to write some shared code between the PlayerIO server and Unity client, and having heap allocatoins everywhere causes really nasty hitches on the iPhone during GC sweeps. I ended up just storing a bunch of "scratch" structs on the heap like you suggested.
dsaunder
 
Posts: 23
Joined: July 22nd, 2010, 7:28 am


Return to C#