• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
phil_t

Scripting solution for C# project

8 posts in this topic

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
0

Share this post


Link to post
Share on other sites

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.

2

Share this post


Link to post
Share on other sites

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!

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites


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.

1

Share this post


Link to post
Share on other sites


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.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0