Forum C# Are const fields fair game?

Are const fields fair game?

Postby Vania » April 12th, 2010, 10:35 pm

I know playerio disallows the use of static fields, however it doesnt throw any errors for const fields.

My understanding is however than C# const fields are inherently static, so I just wanted to know if I could use const fields safely or if the server is going to blow up.

Playerio doesnt throw errors for static methods either, is it safe to use those? Cause if I get to use static methods I can at least make a Singleton to hold my global variables.

Thanks.


PS: I was trying to use a third party library for data-structures (C5) but had to quit because it uses 'ref' and 'out' too much, I wonder if using System.Collections.Generic will be allright.

PPS: Can anyone recommend a PriorityQueue(BinaryHeap) implementation that is compatible with playerio?
Vania
 
Posts: 198
Joined: March 24th, 2010, 9:01 pm

Re: Are const fields fair game?

Postby Oliver » April 13th, 2010, 9:27 am

Hey,

Static fields are disallowed because we'll (potentially) run your game across different servers, and developers would experience really weird behavior if they tried to synchronize global state with static fields, because that variable would only be visible on the machine hosting the game, and the game might be running on multiple different servers at the same time.

Constant fields are allowed because their value is fixed at compile time, and can only be of the immutable types: byte, char, short, int, long, float, double, decimal, bool, string, an enum type, string or null (http://msdn.microsoft.com/en-us/library/e6w8fe1b(VS.71).aspx)

Static methods are perfectly okay, since they won't share any kind of state. It's completely safe to use those.

Playerio doesnt throw errors for static methods either, is it safe to use those? Cause if I get to use static methods I can at least make a Singleton to hold my global variables.


You're not supposed to be able to do that, to avoid the global state issues described above. How would you do it? Are you sure it will work? i don't see how that would work...

Hope that helps. Let me know if you have more issues.

- Oliver
User avatar
Oliver
.IO
 
Posts: 1159
Joined: January 12th, 2010, 8:29 am

Re: Are const fields fair game?

Postby Vania » April 13th, 2010, 3:49 pm

Oliver, I think I understand.

I usually use a "Globals" class where I store all my constants, global variables, etc... and all it members are static.
I guess now I need to think of something else.

What do you think is the best way to share global state across classes?
(I thought about it and Singleton is not going to work, you re right)
Vania
 
Posts: 198
Joined: March 24th, 2010, 9:01 pm

Re: Are const fields fair game?

Postby Oliver » April 14th, 2010, 9:15 am

Just have a class with your variables, make an instance of it and pass the instance around.

However, if the data is immutable, you can just as easily make const fields :-)
User avatar
Oliver
.IO
 
Posts: 1159
Joined: January 12th, 2010, 8:29 am

Re: Are const fields fair game?

Postby Vania » April 14th, 2010, 5:10 pm

Ok, cool. Im making the necessary changes to the code right now.

I bumped into a problem though, while using System.Collections.Generic.List<T>

Playerio doesnt allow me to use the ElementAt method:
ServersideGameCode.Pathfinder.findPath(...) uses the non-allowed method: !!0 System.Linq.Enumerable::ElementAt<ServersideGameCode.MapNode>(System.Collections.Generic.IEnumerable`1<!!0>,System.Int32)


This is the method's signature:
public static TSource ElementAt<TSource>(this IEnumerable<TSource> source, int index);


I dont see anything weird there, except the use of "this", which makes it an extension method.

Maybe it would be a good idea to provide basic libraries for game developers, with data structures, math, vectors, matrices, etc... that are fully compatible with playerio, that would make things a lot easier.

EDIT: I just found out you can just access elements like an array.
Vania
 
Posts: 198
Joined: March 24th, 2010, 9:01 pm

Re: Are const fields fair game?

Postby Oliver » April 16th, 2010, 8:37 am

Maybe it would be a good idea to provide basic libraries for game developers, with data structures, math, vectors, matrices, etc... that are fully compatible with playerio, that would make things a lot easier.


Actually, everything in System.Collections and System.Collections.Generic are supported. The method you tried to use is in Linq, which we currently don't support.

- Oliver
User avatar
Oliver
.IO
 
Posts: 1159
Joined: January 12th, 2010, 8:29 am

Re: Are const fields fair game?

Postby Vania » April 16th, 2010, 6:34 pm

Allright.

I'm doing good progress now that the initial bumps are behind.
Thanks for your help Oliver!
Vania
 
Posts: 198
Joined: March 24th, 2010, 9:01 pm

Re: Are const fields fair game?

Postby Vania » April 16th, 2010, 9:32 pm

ServersideGameCode.CSharpQuadTree.QuadTree`1.Query(...) has varible of the non-allowed type: System.Comparison`1<T>
ServersideGameCode.CSharpQuadTree.QuadTree`1.Query(...) uses the non-allowed method: System.Void System.Comparison`1<T>::.ctor(System.Object,System.IntPtr)


Here's the method:

Code: Select all
public List<T> Query(Rect bounds)
        {
            lock (syncLock)
            {
                List<T> results = new List<T>();
                if (root != null)
                    Query(bounds, root, results);
                if (sort)
                    results.Sort((a, b) => { return objectSortOrder[a].CompareTo(objectSortOrder[b]); });
                return results;
            }
        }


Says System.Comparison (used in the Sort) is not allowed.
Code: Select all
public delegate int Comparison<T>(T x, T y);


But if I declare a similar delegate in my own code it wont complain...
Vania
 
Posts: 198
Joined: March 24th, 2010, 9:01 pm


Return to C#



cron