Tuesday, August 7, 2018

entity component - How can I implement smart scripting, in my game?


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?



Answer



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.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...