18
\$\begingroup\$

I'm developing a game engine. It is supposed to be an entity/components based one. For developing my game, itself, I thought to use some scripting language to actually create the entities.

For example, if I want to add a kind of monster that is aggressive to the player, it will be an entity with several components; those components will change with the monster type, so if I have one hundred different types of monsters, in my game, I will not have to create a new method for each of them straight in my game code.

Should I use a scripting language to describe this entity, in term of components or is there something else that would work better? More generally, how should I use scripting, in my game?

\$\endgroup\$
3
  • \$\begingroup\$ For just entity description, you can use only xml or something similar, no need for scripting. For scripting i would use C#, but only if your engine is in .NET... \$\endgroup\$
    – Kikaimaru
    Aug 2, 2012 at 10:37
  • \$\begingroup\$ @Kikaimaru C# Is not a scripting language. Using lua would be a better idea, or some other language that can be embedded in your engine. \$\endgroup\$
    – Jax
    Aug 18, 2016 at 18:12
  • \$\begingroup\$ @DJMethaneMan "C# is not scripting language" does not mean anything, There is absolutely no problem with writing scripts in C# in a game written in C# and using something like Roselyn for compilation... But after 4 years, I would use json a javascript instead of xml and C# :) \$\endgroup\$
    – Kikaimaru
    Aug 23, 2016 at 10:06

3 Answers 3

18
\$\begingroup\$

My game uses an entity component framework and uses scripts to define entities (this doesn't directly define behavior, I'll talk more about that at the end). The scripts define the actual components to be used for creating each entity. It uses a simple scripting language I created. Here is a simplified version of one of my scripts:

ENTITY:"Goblin"
{
    description="It's currently their age."
    commonname="goblin"
    pluralCommonName="goblins"
    childname="gob'in"
    pluralChildName="gob'ins"
    active=Nocturnal
    tags=Mobile
    baseAttributes="OrganicMobileCreature"

    [Model]{
            meshname="Goblin"
            texturename="GoblinTexture"
    }

    [Motion]{
            maxvelocity=0.01:0.015
            locomotion=Walk,Swim
    }

    [Skills]{
            ALL=0.01:0.05,Mine=8.3:8.8,PlaceCube=8.3:8.8
    }

    [Inventory]{
            maxItems=2
            Allow=ALL
            Disallow=NONE
    }
}

Much of that is self describing, but here are some highlights:

  • The first section describes the common information for the entity. That includes a description and display names for various aspects of the entity.
  • The baseAttributes tag references another script file that defines common components that I don't want to have to redefine multiple times. It contains components like position, liferequirements and so on. If any component is defined here again, this component will overwrite the common one.
  • Each [NAME] { } set defines a new component that will be added to these entities.
  • This description is not just for a single entity, it's for all goblins created. You'll see that some of the values have ranges (i.e. 0.01:0.015), when a new goblin is created it gets created with a component that has a random value in that range. So each goblin will have slightly different skills and slightly different speeds. This setup defines that all goblins will start with really good skills in placing cubes and mining, which is really just for my own testing purposes. But as I'm sure you can guess, it's very easy to change the values to whatever I want.

This whole thing involves creating a custom parser, some kind of structure to hold the entity definitions (I call mine the Lexicon!) and a factory for taking those entity definitions and generating new entities. For me this system is still in its early stages, but it's turning out really, really well. It's a pretty powerful system for quickly defining entities and allows you to make any entity you want using the components you've created. If you're not comfortable creating your own parser, I think XML will work just fine. I converted mine from a pushback recursive parser I wrote for a little made up programming language.

As you can see this defines the entity. I mentioned that it doesn't directly define behavior. It can, however, easily define such things as hated enemies and how aggressively to react to said enemies. This would be as simple as defining whatever component you use to control such behavior. My entities also have a intelligence component (not shown) that defines things like:

  • How they path find (simple line-of-sight movement, simple A*, predictive A*, etc.)
  • How aggressive/defensive they are. Entities can have home zones that will be defended, but maybe not aggressive outside those zones.
  • Technology awareness (open doors, use gadgets, avoid traps, etc)
  • And more...

However yours is defined, it's your system that will drive the data in that component, which in turn affects the behavior of your entites.

\$\endgroup\$
4
  • \$\begingroup\$ Thanks for sharing. I actually think i'm going to try to do it using XML format. I have think a lot about that (thanks to AbstractChaos btw) and it should feet my needs (at least for entity descriptions). \$\endgroup\$
    – nathan
    Aug 2, 2012 at 19:05
  • 1
    \$\begingroup\$ @nathan I agree you should go with XML. The reason for my post was more what data to include in your XML and how to use it. The format of the data can be whatever you like. The data you choose to include and how you implement its use is far more important. \$\endgroup\$
    – House
    Aug 2, 2012 at 20:21
  • \$\begingroup\$ @Byte56 I know this post is old, but how might you handle parent child relationships? Say we have a Skill tree and you need 10 points in skill A[0] to enable A[1], and 10 points in that to enable A[2], etc. Should I nest these, or flatten them and key on a ParentId? Obviously they're logically equivalent, but I hoped you had some real-world insight. \$\endgroup\$ Apr 5, 2015 at 18:32
  • \$\begingroup\$ @Superstringcheese if player.hasPoints(10) then i++ end skillTree[i] would be some pseudocode. I have no idea how that question is relevant to the post, however. \$\endgroup\$
    – Jax
    Apr 26, 2016 at 21:25
4
\$\begingroup\$

If all you really need is a way of defining the components of a Monster then XML would work well both C# and java have a lightning fast implementation of it.

Your xml could be

<?xml version="1.0" encoding="UTF-8"?>
<mobs>
  <mob>
    <personality>Aggressive</personality>
    <intelligence>20</intelligence>
  </mob>
</mobs>

Then your Mob class could look like. (Java)

public class Mob {
  private IPersonality personality;
  private Integer intelligence

  //**  Getters & Setters **//
}

Where IPersonality is an interface.

You could then load your xml and parse each value through a factory.

for example Parse the Personality Value into the PersonalityFactory which is simply:

public IPersonality getPersonality(String personalityName) {
  if(personalityName.equals("Aggressive")) {
    return new AggressivePersonality();
  }
  else if(personalityName.equals("Passive")) {
    return new PassivePersonality();
  }
  else {
     //Maybe allow for no personality (We all know monster like that ;) )
     return null; 
  }
}

Then you could set up a mob like so

Mob mob = new Mob();
mob.setPersonality(getPersonality(xmlValue));
mobList.add(mob);

The Key is that you engine knows the format of the xml and has a factory for all it needs.

One advantage of xml is that you can define your own schema to ensure that the format is always correct see here.

Hope this helps

\$\endgroup\$
2
  • \$\begingroup\$ Actually i need to find a way to easily create new entity over game the game development process. Is xml will be flexible enough? I ll need to add scripting anyway for internal game logic. \$\endgroup\$
    – nathan
    Aug 2, 2012 at 12:57
  • \$\begingroup\$ If you read the Mob class as Entity then you are creating a new Enitity(Mob) using XML with different components (IPersonality,Intelligence [example of data for that mob]). And unfortunatly I can't answer if it will be flexible enough as I dont know what more you wish it to do other than what you specified, However XML is a format where the only boundries are how you interpret each section. Update you Question with a detailed example and I will show an xml that can handle that. Internal game logic sounds like it should be internal? \$\endgroup\$ Aug 2, 2012 at 13:25
0
\$\begingroup\$

Python is fine I think. Often LUA is also a good alternative if you want to add scripting capabilities to your programs.

You could either use XML to describe the behaviour of your monsters, this would involve some coding in the actual game code because you'll save only the "name" or some attributes according to the behaviour (speed, type of weapon the monster uses, etc.) of the behaviour to use in your game code.

If you use a Scriptengine (e.g. LUA) you could transfer this code out from your precompiled program to scriptfiles that are loaded during the runtime. To do this you have to expose the API of your "monsters" to the scriptengine. That allows you to call the methods of the monster-game code from the outside.

\$\endgroup\$
3
  • \$\begingroup\$ The API of my monster? I mean, i will have to be able to create new components from the script (instanciation). Is it possible? \$\endgroup\$
    – nathan
    Aug 2, 2012 at 11:51
  • \$\begingroup\$ Afaik this should be possible. You could also use a mixed approach of external storage (like mentioned by abstractchaos or byte56) and scripting language (LUA,Python ...). The main advantage of e.g. LUA is that you can change your code at runtime and it is instantly available in your running game/engine \$\endgroup\$
    – Aron_dc
    Aug 3, 2012 at 9:42
  • \$\begingroup\$ Ho seriously? It's a huge advantage indeed. Also i have though of LUA (or other scripting language) for "game timeline". I mean, to create some scenarised scenes where the player have to be blocked, this sprite have to move here, light here and there... So maybe i could also use a script language for entity loading? Also i'm going to post another question to expose my current way to manage entity/components over what i call "manager" to see if i'm doing well. \$\endgroup\$
    – nathan
    Aug 3, 2012 at 9:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .