Monday, March 9, 2015

Simple Script API Documentation

Script API Framework

Don't think I covered this so now is a good time!  I have implemented abstract script support that I can attach any .Net supported scripting language too.  Currently, I have support for Lua.  It is fairly easy to use.
            using (var script = new LuaScript())
                script.AddAPI(new LoggingAPI());
                script.AddAPI(new AddonAPI());
                script.AddAPI(new DocsAPI());
                script.AddAPI(new CsvAPI("csv/"));
function main()
    log.print('hello world');
    foreach a in addons.All do
        log.printf('Addon: {0}', a)
    log.print(docs.GetMethod(docs, 'GetMethod'))
 Showcased in that little script is also the recent addition of Documentation through the use of Attributes.  Makes life a little easier.  It prints out something like this.

Adding additional languages is easy.  Just create a class with the IScript interface and use it in a using statement.  Call the AddAPI methods to add functionality and Load a stream of code than call Run.  That easy.
public interface IScript : IDisposable
    bool Run(params object[] args);
    void Load(Stream sourceStream, string name = "chunk");
    // ReSharper disable once InconsistentNaming
    void AddAPI(IScriptAPI api);
    T GetVariable<T>(string name);
    void SetVariable(string name, object value);
I hope this has given someone some ideas about adding scripting support to their framework.  I know that there are questions about the logic of using a scripting language.  In a game it makes a whole lot of sense to use scripting for behaviors, user interface management, and even configuration.  That is enough reasoning for me.  I hope it is for you.

Tuesday, March 3, 2015

Dictionary - Lesson Learned

Sometimes what appears to be the right way to handle a Dictionary isn't always the"right" way.  In .NET a Dictionary is a key/value pair collection of objects that you can store and than later retrieve by key or iterating over the contents.  If you want to see if the key you want is even in the dictionary you call

If the dictionary has your key you can execute the contents of the "if" block. Is this the right way to do it?

Well, you could just catch the exception that is thrown when you try to add an entry.

But exceptions are costly because it forces the stack to be unwound so that you can trace the method hierarchy.

What I learned from a little research into Dictionary is that the "correct" way to do it is to use TryGetValue method.

TryGetValue attempts to get the value of a key in the Dictionary and returns a boolean (true/false) result. The above example tries to get the value of "myKey", which obviously won't work because the Dictionary had nothing added to it, fails and executes the dictionary.Add code.

TryGetValue is a much faster and safer approach then the other 2. To someone who gets caught up in the idea that ContainsKey shouldn't be expensive, you are wrong. TryGetValue method is the right way.

This lesson allowed me to trim a hefty bet of seconds off a big iteration of entities, allowing me to outperform the Java version.

Sunday, March 1, 2015

Asset Management!


Asset management is an important part of ensuring that your content is delivered to the client in a non-intrusive and professional way.  Asset management shouldn't be hard.  Going to introduce you to the way I handle it but not divulge my actual code, sorry.

The Worker Bee

It is important to have one central class that you can deal with to load assets.  An asset manager, although you do not need to call it that, that handles loading, unloading and caching of assets.  Caching is important because you do not want to load the same thing twice.  The design should be simple and easy.  You shouldn't have to extend your asset manager EVER.  Asset manager should be transparent to the components inside.

Something like...

It should be THAT easy.  I may come back to this and add more insight.