Jump to content

  • Log In with Google      Sign In   
  • Create Account

Scripting solution for C# project


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 phil_t   Crossbones+   -  Reputation: 4094

Like
0Likes
Like

Posted 29 May 2014 - 05:47 PM

I'm trying to come up with a scripting solution for my entity component framework written in C#. This would be for specialized scripts attached to entities. Right now I have a Script component to which you can attach script ids. These ids map to C# classes that implement the scripts. There is only one instance of each script. When it runs it is presented with "property bag" state associated with the entity, and it can read and write values to that.

 

It's functional, but the way I get/set script properties in code (accessing them by id from a property bag) is ugly. I'd rather just be able to treat them as regular C# properties on the script object. The reason I can't do that is because I need to quickly serialize the state of the script. 

 

I have the following requirements:

- I need to be able to reasonably quickly serialize the state of the script and equally quickly re-hydrate it. The state should be platform portable, and generate no allocations during serialization (this I have now)

- Be able to store variables of simple data types (float, int, etc...) as part of per-entity per-script state. (this I have now)

- Access those properties with clear syntax (E.g. "DeathTimer = 1.5", instead of "propertyBag.SetFloatValue(DeathTimerId, 1.5f)"  (this I do not have now)

- Be able to access an entity's components through simple syntax (e.g. "entity.Transform.Position = new Vector3(0, 0, 0)"). (this I do not have now, but I could probably hack something together to give me this nice syntax).

 

 

1) I've though about using Lua, but I'm not sure how flexible it is. Can I customize how C# properties/methods are exposed in Lua? i.e. make my ugly PropertyBag object appear as a regular object to which I can get/set properties.

 

2) I could invent my own scripting language to accomplish everything I want. I started down this path for a while, but it is a lot of work.

 

3) Keep using C# and find some solution to make my syntax nice (basically this means finding a good way to handle quick serialization of script state). This is the most flexible coding-wise, and has the least dependencies (i.e. I don't need to depend on some 3rd party Lua library), and I'd like to go down this path if possible. If C# had macros, I could easily "auto-generate" object properties that have their data stored in my PropertyBag (ala DependencyProperty). I think this would solve most of my syntactic concerns. But alas...

 

Any thoughts? What are other people using?


Edited by phil_t, 29 May 2014 - 05:58 PM.


Sponsor:

#2 Brian Sandberg   Members   -  Reputation: 1365

Like
2Likes
Like

Posted 30 May 2014 - 11:14 AM

You could solve the clumsy-syntax issue by storing your state in a 'dynamic' object instead of your propertybag.  Serializing that should still be simple and quick.



#3 phil_t   Crossbones+   -  Reputation: 4094

Like
0Likes
Like

Posted 30 May 2014 - 06:19 PM

Hmm, I didn't even know about dynamic objects... I'll look into that and see if it's a workable solution for me. Thanks!



#4 phil_t   Crossbones+   -  Reputation: 4094

Like
0Likes
Like

Posted 12 June 2014 - 07:37 PM

From my brief research on dynamic objects, it doesn't look like it would meet my needs. First of all, I really want something that is resolved at compile time, not run time. Second, it looks like setting and getting PoD values will cause them to be boxed, and I want to avoid memory allocations.



#5 SeraphLance   Members   -  Reputation: 1454

Like
0Likes
Like

Posted 13 June 2014 - 01:02 AM

If you're asking what I think you're asking, this is one of the times multiple inheritance can actually be kind of nice.  Unfortunately, that doesn't exist in C# so the best you can manage is composition.  You might be able to roll some operator overloading to make it a bit prettier, but personally I think that falls squarely into the abuse side of things.



#6 frob   Moderators   -  Reputation: 22731

Like
1Likes
Like

Posted 13 June 2014 - 01:30 AM

While c# doesn't support multiple inheritance of concrete classes, it does support multiple interfaces (basically the pure abstract base class) and it also supports adding attributes (basically bonus classes that get attached similar to metadata). On our C# systems we rely on both of these.

You can in effect add a bunch of data to a class simply by writing [YourAttributeNameHere] above the class or a function. You can add blobs of data that work basically as was mentioned above, adding all kinds of functionality to a class without inheritance.

The built-in C# serialization system uses attributes in a similar way to what the OP is getting at. You could easily add a collection of information using an attribute that gets persisted along with the rest of the data.

Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I write about assorted stuff.


#7 phil_t   Crossbones+   -  Reputation: 4094

Like
0Likes
Like

Posted 13 June 2014 - 12:11 PM

I'm not sure how attributes will help my scenario. I basically have something like this for a script that makes a flame "flicker" by adjusting light intensity:

    class FlameFlickerScript : EasyScript
    {
        private static int MaxBrightnessId = "FlameFlicker_MaxBrightness".CRC32();
        public float MaxBrightness
        {
            get { return propertyBag.GetSingleValue(MaxBrightnessId); }
            set { propertyBag.SetValue(MaxBrightnessId, value); }
        }

        private static int MinBrightnessId = "FlameFlicker_MinBrightness".CRC32();
        public float MinBrightness
        {
            get { return propertyBag.GetSingleValue(MinBrightnessId); }
            set { propertyBag.SetValue(MinBrightnessId, value); }
        }

        private static int SecondsToNextBrightnessId = "FlameFlicker_SecondsToNextBrightness".CRC32();
        private float SecondsToNextBrightness
        {
            get { return propertyBag.GetSingleValue(SecondsToNextBrightnessId); }
            set { propertyBag.SetValue(SecondsToNextBrightnessId, value); }
        }

        public override void Update(Entity e, float ellapsedTime)
        {
            SecondsToNextBrightness -= ellapsedTime;
            if (SecondsToNextBrightness < 0)
            {
                SecondsToNextBrightness += MathHelper.Lerp(0.1f, 0.25f, em.Universe.Mersenne.NextSingle()); 
                float brightness = MathHelper.Lerp(MinBrightness, MaxBrightness, em.Universe.Mersenne.NextSingle());
                PointLightSource.Intensity = brightness;
            }
        }
    }

My goal is to have some kind of syntactic sugar so that I don't need to write out all those property declarations in long form. In C++ this would be trivial, I could just make a macro that has the property name and type, and be done with it.

 

Yes, I could make these regular C# properties, and add some attributes that tell me how to serialize them. But then I'm using reflection to get and set properties, and I incur all the performance penalties associated with that.



#8 frob   Moderators   -  Reputation: 22731

Like
1Likes
Like

Posted 13 June 2014 - 03:07 PM


and I incur all the performance penalties associated with that.
Like most costs, it does not need to be very big.  Pay the cost once and store the result forever. 

 

 


My goal is to have some kind of syntactic sugar so that I don't need to write out all those property declarations in long form.

Is that out of laziness? Since you mention doing the same thing for a bunch of classes, just do it once and copy/paste the results. Or use tools that do the hard bits for you, like CodeRush or even just simple Visual Studio macros with regular expressions in them. Or write a quick and dirty python script to generate all the lines for you.

 

Sure that kind of thing takes a few minutes to set up, that little bit of work can enable your laziness into the future. I've been on projects where every time we create a new game object we just click the "NewGameObject.py" script, type in the name of the object, and the script does the grunt work of adding it to the project and build scripts in all the right places, fills out all the boilerplate parts of the code, and bundles it all up in a p4 changelist ready for submission.

 

Basically one person said "This process is long and tedious and I don't feel like doing it today.  Instead I'l cobble together some scripts to do all that work for me and let me be lazy in the future." And then the script was used hundreds of times by all the developers on the team who thanked him profusely.


Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I write about assorted stuff.


#9 phil_t   Crossbones+   -  Reputation: 4094

Like
0Likes
Like

Posted 13 June 2014 - 04:08 PM


Like most costs, it does not need to be very big.  Pay the cost once and store the result forever. 

 

It's more than just the up front cost of reflecting the class. I'll need to serialize the scripts (to memory) fairly frequently as the player travels through the world and portions of it are de-activated as he leaves them. This means boxing and thus memory allocations. It's possible that's not a big deal, but I've been taking a general strategy of avoiding spurious allocations during gameplay (in case this ends up on a platform with less than ideal garbage collection performance). 

 


Is that out of laziness? Since you mention doing the same thing for a bunch of classes, just do it once and copy/paste the results. Or use tools that do the hard bits for you, like CodeRush or even just simple Visual Studio macros with regular expressions in them. Or write a quick and dirty python script to generate all the lines for you.

 

Yes, it's mainly out of laziness. Thanks for the suggestions, a VS macro might work.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS