Scripting for Games

by calbert 8/26/2008 1:02:00 AM

Scripting in games is not a new concept, most games do this on some level. Scripting allows game objects to execute code that is not compiled as part of the object. In the realm of roe playing games scripting gives objects the ability to react to the world around them. For instance, an object could execute script whenever a player gets near it, allowing a vender to hawk their wares, creatures to attack, etc. There are a variety of scripting languages currently being implement in games such as Lua and Python. Recently I have been researching these two for possible addition to Perenthia to provide me the ability to add custom behavior to objects and NPCs.

Edit: Thanks to some helpful insight and information from Michael Foord I was able to get an IronPython sample working. The sample adds scripting support to an object; the code is very basic but accomplishes the task. Thanks Michael!

Here is a snippet of a console app using the latest IronPython Beta4:

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Scripting;

using IronPython;
using IronPython.Hosting;
using IronPython.Runtime;
using IronPython.Runtime.Exceptions;

using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;

namespace IronPythonGameScripting
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Actor player = new Actor("Cam");
                Actor mob = new Actor("Mob");
                mob.Script = @"class Mob:
    def onEncounter(self, *value):
        value[0].Name = ""Mr Groovy""";

                Console.WriteLine("Player name was '{0}'", player.Name);

                mob.Execute(Actor.EVENT_ON_ENCOUNTER, player);

                Console.WriteLine("Player name is now '{0}'", player.Name);

                mob.Execute("Test", player);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                Console.WriteLine("Press any key to exit...");
                Console.ReadKey();
            }
        }
    }

    public static class Engine
    {
        private static ScriptEngine _engine;
        private static ScriptScope _scope;

        static Engine()
        {
            _engine = PythonEngine.CurrentEngine;
            _scope = _engine.CreateScope();
        }

        public static void Initialize()
        {
        }

        public static void Execute(string script)
        {
            ScriptSource source = _engine.CreateScriptSourceFromString(script, SourceCodeKind.Statements);
            source.Execute(_scope);
        }

        public static void Execute(string script, string className, string methodName, params object[] args)
        {
            ObjectOperations ops = _engine.Operations;
            ScriptSource source = _engine.CreateScriptSourceFromString(script, SourceCodeKind.Statements);
            source.Execute(_scope);

            if (_scope.ContainsVariable(className))
            {
                object @class = _scope.GetVariable(className);
                object instance = ops.Call(@class);
                if (instance != null)
                {
                    if (ops.ContainsMember(instance, methodName))
                    {
                        object method = ops.GetMember(instance, methodName);
                        ops.Call(method, args);
                    }
                }
            }
        }
    }

    public class Actor
    {
        public const string EVENT_ON_ENCOUNTER = "onEncounter";

        public string Name { get; set; }
        public string Script { get; set; }

        public Actor(string name)
        {
            this.Name = name;
        }

        public void Execute(string methodName, params object[] args)
        {
            Engine.Execute(this.Script, "Mob", methodName, args);
        }
    }
}

 

As it turns out I was attempting to use the IronPython libraries compiled against the Silverlight framework rather than the libraries compiled against the normal .NET framework. I was also using an older beta 1 version of the IronPython library which may have attributed to the exceptions I was seeing. Got to make sure you reference the correct assemblies when working in both .NET and Silverlight. Surprised this is the first time I've done this...

I reviewed IronPython because it has Silverlight support with the DLR but it is really designed to be used instead of C# not as a scripting language executed from within C#. With Lua though I may be able to get it working although it still seems like everything was designed to simply execute the game in lua scripts using C# as the core instead of augmenting C# classes.

I really want to just be able to raise events on the mobile objects and have instances handle those events in a custom way. I certainly don't want to compile C# on the fly either as that has its own set of issues. Compiling C# on the fly is like running an exe for each script that is executed and would be way too costly in both execution time to compile and memory consumption.

Also, since both the client and server will need to execute scripts the scripting language needs to work in both Silverlight and ASP.NET.

Perenthia currently handles commands using a Dictionary<int, Command> where the int is command value, from a list of constants, and the Command is a method pointer (delegate). When the game loads up the first time the list of commands is constructed and the methods pointers are set to methods that related to the command. An example would be a chat command. When the command comes in and is validated the method pointer is found using this dictionary and then executed in the context of the connected player. I was thinking of doing the same thing with mobiles and the actions they can take and respond to based on the world around them. It is a bit more complex with mobiles than commands since commands do a specific thing whereas every mobile could execute a different block of code for a given event. I still have some work to do on it but this will probably be what I use in place of scripting. I loose the flexibility of writing quick scripts, changing them on the fly, etc. but gain the ability to have it work within my code framework, rely on only .NET and work in both Silverlight and ASP.NET.

Currently rated 1.0 by 1 people

  • Currently 1/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Game Development | General | Perenthia PBBG | Silverlight 2 Development | Silverlight Games

Silverlight Game Contest

by calbert 8/23/2008 12:01:00 AM

I thought I would take a little break from working on Perenthia and take part in a Silverlight Game Contest for a sports theme game. This will push out the alpha release date a little but I need a little change of pace.

I am a little late in deciding to do this but figured I would give it a try and see if I can come up with something fun by September 8th. I have an idea for a simple game, hopefully I can make it fun to play because I know it will be fun to build.

Once the game is finished I will also post the source code.

Silverlight 2 Farseer Physics Geom Visual Helper Method

by calbert 8/21/2008 9:43:00 PM

I ported some code from the Farseer Physics Engine XNA demos that displayed  a debug view of the geom vertices used with the Physics Simulator. This is useful if you want to see where your geoms are being rendered to the screen to make sure you have everything in the right place. I know I need this a lot, helps me to understand where I am placing body geoms.

Anyway, here is the method, the _debug variable is just a Canvas I added at the top of the UI. Keep in mind that this method will slow your game WAY down so be sure and use it to test where geoms are positioned and if everything is moving properly. I use a static variable for the PhysicsSimulator, hence the cleverly named Physics.Simulator.

        private void DrawVertices()
        {
            _debug.Children.Clear();
            int verticeCount = 0;
            for (int i = 0; i < Physics.Simulator.GeomList.Count; i++)
            {
                verticeCount = Physics.Simulator.GeomList[i].LocalVertices.Count;
                for (int j = 0; j < verticeCount; j++)
                {
                    Line line = new Line();
                    line.Fill = new SolidColorBrush(Colors.Transparent);
                    line.Stroke = new SolidColorBrush(Colors.Magenta);
                    line.StrokeThickness = 1;
                    if (j < verticeCount - 1)
                    {
                        line.X1 = Physics.Simulator.GeomList[i].WorldVertices[j].X;
                        line.Y1 = Physics.Simulator.GeomList[i].WorldVertices[j].Y;
                        line.X2 = Physics.Simulator.GeomList[i].WorldVertices[j + 1].X;
                        line.Y2 = Physics.Simulator.GeomList[i].WorldVertices[j + 1].Y;
                    }
                    else
                    {
                        line.X1 = Physics.Simulator.GeomList[i].WorldVertices[j].X;
                        line.Y1 = Physics.Simulator.GeomList[i].WorldVertices[j].Y;
                        line.X2 = Physics.Simulator.GeomList[i].WorldVertices[0].X;
                        line.Y2 = Physics.Simulator.GeomList[i].WorldVertices[0].Y;
                    }
                    _debug.Children.Add(line);
                }
            }
        }

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Game Development | Silverlight 2 Development | Silverlight Games

More Perenthia Screen Shots

by calbert 8/9/2008 1:36:48 AM

Here are a few more screen shots of the Perenthia UI. The three screen shots are dialog windows that popup as a result of clicking on icons or buttons in UI. They are use a reusable control I created that has the title bar and X button and then just has a ContentControl for the guts of the dialog. The dialog window is draggable and does the hide and show thing. I need to do some cleanup on it and then I will post the source. I will also post the source for the stat bars shown on the skills dialog screen shot.

Also, I am hoping to have some time to blog about some of what I am doing with the overall game engine. I wanted to provide a development update but haven't moved the development along all that much due to working out some networking issues and trying to clean up some of the code.

The goal for Alpha is to get a working zone complete with quests, monsters, npcs, etc. However, I may do some smaller tests with the alpha registrants before all of this is complete. I was thinking of setting up a tiny zone just for testing where players could login, create a Character and accept a quest, which would be to enter a room where enemies spawn randomly and in various numbers. This would allow me to test a lot of things in one place; Character Creation, Quests, Spawning, Combat and NPC interaction.

Anyway, here are the screen shots; this first one is the Character Sheet that will display your character stats, equipped items and skills. The empty box will show the skills once I bind them to it. :)

This next screen is the actual skills screen where you can see you skill rank and all available skills. If you have Skill Points you can increase or learn new skills from this window.

This last image is just the backpack dialog, all I have is a starting candle right now. :)

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Game Development | General | Perenthia PBBG | Silverlight 2 Development | Silverlight Games

Perenthia UI Screen Shot

by calbert 7/27/2008 10:57:52 PM

As promised here is a screen shot of the main game UI, click on the image for a full size version. The top left is the current player's avatar and details. The map in the top right corner shows you the current zone, in relation to your z-index. The icons scattered around open other windows that allow you to view your character sheet, skills, spells, backpack, etc. The interface is built in Silverlight 2 Beta 2.

Perenthia UI

Currently rated 3.0 by 1 people

  • Currently 3/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Game Development | General | Perenthia PBBG | Silverlight 2 Development | Silverlight Games

Skills Based Game Play

by calbert 7/27/2008 10:27:01 PM

Skills are the fundamental system in Perenthia, they govern all actions you can take from slaying a mighty beast to casting a healing spell to piloting a ship on the open ocean. What this means for players is that you can train up any skills and create a unique game play experience. For instance, one of the Professions in the game is a Mage; if I want my Mage Character to be able to wear plate armor while raining down destruction on my foes. Since I chose the Mage profession I will get bonus skill points in magical related skills at level one with additional skill bonus points added to new skills at various levels. However, since I want to be a plate armor wearing Mage I will spend some of my initial bonus Skill Points in the Plate Armor skill. Now, I can wear some of the low level plate armor and continue to train that skill as I advance or earn additional Skill Points. Likewise, if I am a Knight and want to gain the ability to shape shift like a Ranger I can spend points in the Shape Shifting skill.

Of course, you just won't have enough Skill Points through out the game to spread them around too thin, probably only about 4 or 5 additional skills could really be trained to usefulness so players will need to choose carefully. I am still thinking about the ability to buy Skill Points back after they have been used so you can re-train, in case you didn't really understand the skill system when you started playing. Still not sure about this, I will see how the players do in the Alpha and Beta stages and see what the feedback is for it.

And while I don't currently have crafting complete, nor will I for the Alpha, I think the skills system will provide a fun and challenging environment.

Currently rated 3.0 by 1 people

  • Currently 3/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Game Development | Perenthia PBBG | Silverlight 2 Development | Silverlight Games

Quests

by calbert 7/23/2008 1:26:29 AM

For those unfamiliar with the fantasy role playing genre; quests are simple to complex tasks that players complete in order to advance, get sweet gear, etc. They usually involved raiding a dungeon, slaying some terrible beast and wondering about the countryside trying to find the entrance to the hidden caverns.

What I hope to accomplish with quests is to provide a goal beyond just slaying monsters. Quests will actually move your Character through the game, while training and advancing them along the way.

One of my primary goals with Perenthia is to provide a different game play experience as players mature within the game. The first phases of the game will include some of the normal grind, killing monsters to get better gear, to level up, to kill monsters, etc. This gives the players immediate satisfaction while getting them used to moving around, using commands, spells, equipping items, buying and selling and everything else that goes along with eradication of the demon beasts of the underworld. The second phase of the game will follow a bit of a storyline, requiring players to venture into dungeons and caverns to retrieve special items they will need in order to move into the next phase of the game. While this involves a lot of the same type of situation, you have a reason to go into the dungeon and complete it, rather than just killing 10 rats because some dude is freaking out. The third phase will require the player to use the items created from the previous phase to solve puzzles, riddles and the like, which will take them into the next phase. The fourth phase will require the player to purchase some form of transportation in order to visit very far off places and located individuals who can further their progress with quests of their own. This will continue in this manner for the remainder of the game. I currently have 4 phases planned and ideas for 3 more that I hope to get written down and planned out before too long, although I will probably wait until after the alpha an beta test phases.

I want to try and provide a fun game and I feel that doing this phased game progress could provide an enjoyable gaming experience. I know when playing other games of the genre I usually burn out in the high levels because I worked so hard to get there and it is just more of the same. I would also like the world to progress forward around the players so that new challenges are introduced to both high and low level characters that are in keeping with the overall storyline. I want players to have as much fun playing the game as I am having making it. :)

I hope to provide a robust questing system that can handle these phases and still maintain a clean and easy way for players to know what to do next. For the alpha I have planned out some quests that take you around the alpha starting area and should advance the alpha players enough so that the next area will introduce another phase in the game experience.

I will do another development update after this weekend.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Game Development | Perenthia PBBG | Silverlight 2 Development | Silverlight Games

Perenthia World Builder Screen Shots

by calbert 7/22/2008 1:32:35 AM

I am not quite ready to post the screen shots of the actual game UI, still need to do some cleanup. So, since I promised I would post some screen shots here are a few of my world builder tool. This is a windows application that just reads and writes to the database.

This first screen is the map builder and as you can see I can draw the actual places/rooms in which players will adventure. The map that is displayed will be the Alpha starting map, it consists of a small town, a forest and a small sewer system. The sewer nodes are not displayed because they are one level down.

This is the creature manager window that allows me to create creature "templates", from which actual creature instances can be created and placed onto the map from above. (Using the Add Mobile button).

This is just a shot of the item manager window, displaying some of the items I've added so far, got a long way to go on these. The items also follow the same "template" pattern that the creatures do. I still need to add in support for placing the drop items on Mobiles.

I hope to have the game UI screen shots ready soon.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Game Development | Perenthia PBBG | Silverlight 2 Development | Silverlight Games

Perenthia Pre-Alpha Registration

by calbert 7/12/2008 2:42:17 PM

I have opened up the Pre-Alpha Registration on Perenthia. The Alpha should be starting at the beginning of September and will only be open to Alpha players. The Perenthia web site has also received a new design.

For those who don't know; Perenthia is a persistent browser based game set in a fantasy world utilizing Microsoft Silverlight 2 on the "client" and using C#/ASP.NET on the "server".

I am going to try and get out regular updates on the development process leading up to Alpha, time permitting. As such this post will also server as the first of those updates. I will also post some screen shots of the UI soon, just want to finish and few more things and remove some debug windows and labels.

 

Perenthia Development - 88%

User Interface (UI) - 89%

  • Main Chat Window - 92%
    • Colored Text - 100%
    • Clickable Text - 85%
  • Mini-Map - 100%
  • Who's Online - 87%
    • Display Online Users - 95%
    • Clickable Users - 80%
  • Character Creation - 80%

Server (Includes Authentication, World State and Object Model) - 81%

  • Authentication - 100%
  • Character Creation - 95%
  • Content (Rooms, NPCs, Monsters, Quests) - 60%
  • Combat System - 75%
  • Magic/Ability System - 75%

Database - 95%

 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Game Development | Perenthia PBBG | Silverlight 2 Development | Silverlight Games

Perenthia - Pre-Alpha Details

by calbert 7/10/2008 11:41:48 PM

I have made so many changes to Perenthia since the alpha/beta release I did last year that I am actually going to take the game back to Alpha and go through the whole process again. Most of the data elements remain the same, the story is the same, the goals, etc. but the server and client pieces have been modified so much its not really the same code base.

Anyway, I am hoping to get a pre-alpha signup form on the site this weekend and get the closed Alpha live by the end of summer.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Game Development | Perenthia PBBG | Silverlight Games

Powered by BlogEngine.NET 1.3.1.0
Theme by Mads Kristensen

About the author

I am Senior Software Engineer specializing in the Microsoft .NET Framework and PBBG development.

E-mail me Send mail

Calendar

<<  August 2008  >>
MoTuWeThFrSaSu
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567

View posts in large calendar

Recent posts

Recent comments