Code and configuration for the game object library

I am working on a small online game where you need to store reasonable information about many (100+) different game objects.

I am trying to decide whether this data should be generated by code or stored in some kind of configuration file.

The approach to data generation will be similar to (in java-ish pseudo-code):

(within a set of functions executed once at program startup)
....
// create grass terrain
grass=new GameObject();
grass.inheritProperties(generic_terrain);
grass.set(NAME,grass);
grass.set(MOVEABLE,true);
grass.set(MOVECOST,10);
grass.set(IMAGE_INDEX,1);
....

While the configuration file approach probably just uses an XML type format, for example.

(within terrain.xml file)
....
<terrain name="grass">
    <inherit class="generic_terrain"/>
    <property key="NAME" value="grass"/>
    <property key="MOVABLE" value="true"/>
    <property key="MOVECOST" value="10"/>
    <property key="IMAGE_INDEX" value="1"/>
</terrain>
....

Some important points:

  • This information is always static. The game is completed (i.e. no change at runtime)
  • Property names (NAME, MOVECOST, etc.) are a relatively small list, but additional ones can be added over time.
  • , (. ).
  • (, / ).
  • "" , .. , generic_terrain, /.

? , ?

+3
5

. , - , -. , .

config , . , , "" . :

<object name="ball">
    <property key="shape" value="circle"/>
    <property key="movable" value="true"/>
    <property key="speed" value="10"/>
</object>

<object name="cup">
    <property key="shape" value="rectangle"/>
    <property key="movable" value="true"/>
    <property key="speed" value="6"/>
    <property key="catches" value="ball"/>
</object>

, - , "" . , - :

<object name="cup">
    <property key="shape" value="rectangle"/>
    <property key="movable" value="true"/>
    <property key="speed" value="6"/>

    <oncollision>
      if (collided.getName() == "ball") {
        collided.destroy();
        points++;
      }
    </oncollision>
</object>

, , , . , , . Lua . , . ​​, :

object {
  name='ball';
  movable=true;
  speed=10;
}

object {
  name='cup';
  movable=true;
  speed=6;
  oncollision=function(collided)      
     if collided:getName() == "ball" then
        collided:destroy();
        points++;
     end
  end;
}
+5

- . , . , , .

, xml, , , , .

+2

-.

, , , - .

, /, , . ( , , - ).

, - , -- ; (, ), , . ( .)

+2

, . Lua - , , , . , , . lua :

grass = {
    NAME = "grass",
    MOVEABLE = true,
    MOVECOST = 10,
    IMAGE_INDEX = 1
}

setmetatable(grass, generic_terrain)
+2

If you decide to use XML, at least try using a reasonable schema. There is no reason to embed your own little schema ("object", "property", "key", "value") in XML when it is intended to directly represent this particular material. What about:

<ball>
    <shape>circle</shape>
    <movable>true</movable>
    <speed>10</speed>
</ball>

<cup>
    <shape>rectangle</shape>
    <movable>true</movable>
    <speed>6</speed>
    <catches>ball</catches/>
</cup>
+2
source

Source: https://habr.com/ru/post/1751440/


All Articles