Boxycraft is a physics-based puzzle platform game, where you use your skills of running, jumping, grabbing, swinging, pushing or pulling objects to make your way through the completely interactive levels.

Report RSS Scripting in games in .Net

What I've done is put together a couple of tips for people who are looking to implement a scripting language into a game written for the .Net framework. The majority of resources on the web about scripting are centered around C/C++, so it can be difficult to find relevant information when you are using C# or VB.Net.

Posted by on


What I've done is put together a couple of tips for people who are looking to implement a scripting language into a game written for the .Net framework.

The majority of resources on the web about scripting are centered around C/C++, so it can be difficult to find relevant information when you are using C# or VB.Net.

Firstly, why use scripting?


To allow others to modify and extend your game!

Map editing is fun enough, but scripting takes that experience to the next level by allowing as much customization as the developer cares to expose. Heres a video showing objects which are created entirely from Lua scripts being used in Boxycraft 2.

Even better is that because scripting languages are so simple, almost anyone can use them, and this opens up your user base to a much larger community.

Tip 1: Do things in as few lines as possible


Calls between language are slow, so make it possible to accomplish what you want with as few calls as possible.

For example, take the following draw function from a lua script:

lua code:
function Draw()
    local pos = BodyManager.GetPosition(bodyindex)
    local angle = BodyManager.GetAngle(bodyindex)
    SpriteManager.Draw(spriteindex, pos, angle)
end
 

If you have a large number of these objects in a scene, those 3 calls to the game engine will devour your frame rate!

A much better way to do this would be to attach the sprite to the body when you create it, and have all the drawing handled by the game engine.

Tip 2: Make code easy to maintain


A good rule of thumb here is that every function you expose to your scripts should correspond directly to a function in your engine (This relates more to languages such as Lua, where you can only expose functions in a specific form).

vbnet code:
'This is a wrapper function which is exposed to Lua
'from VB.Net

Public Function luaCreateSprite(ByVal L as IntPtr) As Integer
    'Your code
    Return 0
End Function
 

Instead of creating the sprite inside the funcion above, I would call an already existing function whose job it is to do just that, and return the result.

Failure to follow this can mean a lot of work if you change anything related to that function!

Tip 3: Pick the right language


There are lots of .Net compatible scripting languages, and they all have their own advantages. Here are a couple of things to look for:

  • Syntax
  • Speed
  • Functionality

If you use scripts just for yourself, pick a language that you are familiar with. If others will be using your scripts, then make sure you pick one with a well known and easy to follow syntax.

Using a a true .Net language such as C#, Vb.Net or Boo will give you the fastest performance on the .Net framework, as they are compiled directly to a .Net assembly.

Tip 4: What languages are available


Managed Scripting and the Mono framework are not actually languages, but libraries that allow you to run scripts written in C#, VB.net and Boo.

Windows

XBOX

Windows/Mono

What do you think?


Anyway, this is just what I've come across in the short time I've been experimenting with scripting languages. Do you have experience with implementing a scripting language under .Net?

Post comment Comments
xXMaNiAcXx
xXMaNiAcXx

Woo nice :)

Reply Good karma Bad karma+1 vote
Post a comment

Your comment will be anonymous unless you join the community. Or sign in with your social account: