Filling the map

After we are able to load maps we now want to be able to have objects on them with which we can interact or control.

Entities and Components

FIFErpg uses a modified version of the Grease Framework which uses a Component Based Entity System. Components simply store various data values. The component classes used do not have any methods to work with these values. They are like database tables. An entity is a container that has components attached to it. What component an entity has determines what it can do, or what can be done with it or to it. FIFErpg entities have two mandatory components, namely the “General” component, which sets the unique identifier of the component and the “Agent” component, which contains the basic information needed to place the Entity on a map. Agents that are actually displayed on a map also have a “FifeAgent” component, but that is automatically created as these are placed on the map. All other components can be set freely for each entity.

Setting up Components

We already added the “Components” setting in the previous tutorial, but did not explain what it does. FIFErpg allows one to customize what components are actually wanted and also to add custom components. The actual details for adding components will not be discussed in this tutorial, but may be in a separate one. Basically, though, each component needs to register itself to the ComponentManager. To make that easier FIFErpg can read the available components from a file and it then it just needs a list of the components that it should register. The easiest method to set the list is the “Components” setting in the FIFE settings file. Then you just need to call

app.register_components()

In the last tutorial we need to register the Agent component since FIFErpg looks for entities with that component if a map is loaded.

To actually place entities on a map we need to activate two more components. They are, as already said in the previous section, General and FifeAgent. You need to change the component setting to this:

General ; Agent ; FifeAgent

Note that the delimiter for lists in FIFE setting files is ” ; ”. The spaces are important, if you forget them you will get an error.

Behaviours

Behaviours are FIFE InstanceActionListeners thus have a method that gets called when the instance has finished doing an action, allowing one to set what the agent does next and create daily routines. They also manage the animation queue of an agent, which allows the agent to queue animations.

Currently FIFErpg only has one behaviour: The Base Behaviour. This is the most simple behaviour which only provides the basic functionality.

Like components behaviours have to be registered first. This can be done similar to components through the settings file. The structure is the same, a list that sets what Behaviours to register. The combined.yaml also contains the standard behaviours of FIFErpg. If you downloaded that file before this tutorial was made you may want to re download it though.

For this tutorial you will need to add the “Base” behaviour to this list.

You can review the changes in the settings file.

+ show/hide contents
<?xml version='1.0' encoding='UTF-8'?>
<Settings>
	<Module name="FIFE">
		<Setting name="FullScreen" type="bool"> False </Setting>
		<Setting name="PlaySounds" type="bool"> True </Setting>
		<Setting name="RenderBackend" type="str"> OpenGL </Setting>
		<Setting name="ScreenResolution" type="str">1024x768</Setting>
		<Setting name="BitsPerPixel" type="int"> 0 </Setting>
		<Setting name="InitialVolume" type="float"> 5.0 </Setting>
		<Setting name="SDLRemoveFakeAlpha" type="int"> 1 </Setting>
		<Setting name="GLCompressImages" type="bool"> False </Setting>
		<Setting name="WindowTitle" type="str"> FIFErpg Tutorial </Setting>
		<Setting name="WindowIcon" type="str"> </Setting>
		<Setting name="Font" type="str"> fonts/FreeSans.ttf </Setting>
		<Setting name="FontGlyphs" strip="0" type="str"> abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,!?-+/():;%&amp;`'*#=[]\"</Setting>
		<Setting name="DefaultFontSize" type="int"> 16 </Setting>
		<Setting name="LogModules" type="list"> controller ; script </Setting>
		<Setting name="PychanDebug" type="bool"> False </Setting>
		<Setting name="LogToPrompt" type="int"> 1 </Setting>
 		<Setting name="UsePsyco" type="bool"> False </Setting>
		<Setting name="ProfilingOn" type="bool"> False </Setting>
		<Setting name="LogToFile" type="int"> 0 </Setting>
    	<Setting name="Lighting" type="int"> 0 </Setting>
	</Module>
	<Module name="fife-rpg">
    	<Setting name="ProjectName" type="str">Tutorial 3</Setting>
    	<Setting name="Camera" type="str">camera1</Setting>
        <Setting name="Components" type="list">General ; Agent ; FifeAgent</Setting>
        <Setting name="Behaviours" type="list">Base</Setting>
    </Module>
</Settings>

Adding Entities

With these preparations complete we can move on to place entities on the map. FIFErpg offers a method to load entities from a file. Each entity is a YAML document in that file. The default file name and location is “objects/entities.yaml”.

The structure of each document is simple:

!Entity
Template: Name of Template
Components:
    Name of component:
      component field: value
      component field: value
      ...
    Another component:
      another component field: value
      ...
    ...

The Template line is optional. The ”!Entity” line is a special command for PyYAML, which FIFErpg uses. The Components part is basically a list of Components, using the name they are registered with, and their field values. The standard components are set to use their class name, which is also the name in the combined.yaml file.

With that we can add our first entity. Create a file named entities.yaml inside the objects subdirectory and put the following in it:

!Entity
Components:
  General:
      identifier: PlayerCharacter
  Agent:
      gfx: player
      map: Level1
      layer: actors
      position: [-5, 0]
      rotation: 180
      behaviour_type: Base

The field values are explained in the respective component modules.

To actually have the entity appear on the map we need to add more code to load and register the behaviours and load and place the agents.

The behaviours are loaded and registered similar to components, add the following lines before creating the world:

app.load_behaviours("combined.yaml")
app.register_behaviours()

The entities and components are managed by the world, which can be accessed through the application.

The following code is all that is needed to load and the entities:

world = app.world
world.import_agent_objects()
world.load_and_create_entities()

import_agent_objects() will import fife object definitions from a specific directory. By default it will use the “AgentObjectsPath” setting which defaults to “objects/agents”.

load_and_create_entities() will load the entities from the entities file and create them.

The actual placement of entities that are on a map is done when switching to the map. So, since loading the entities needs a world object, these lines have to be added after creating the world and before switching to a map. be loaded before doing that.

Here is the complete code with the changes:

from fife_rpg import RPGApplication
from fife_rpg import GameSceneView
from fife_rpg import GameSceneController
from fife.extensions.fife_settings import Setting

settings = Setting(app_name="Tutorial 3", settings_file="settings.xml")

def main():
    app = RPGApplication(settings)
    app.load_components("combined.yaml")
    app.load_behaviours("combined.yaml")
    app.register_components()
    app.register_behaviours()
    view = GameSceneView(app)
    controller = GameSceneController(view, app)
    app.create_world()
    app.load_maps()
    world = app.world
    world.import_agent_objects()
    world.load_and_create_entities()
    app.switch_map("Level1")
    app.push_mode(controller)
    app.run()

if __name__ == '__main__':
    main()

You will need the “player” object to be able to run that code. The one used for the tutorial is inside the agents.7z archive. Extract it into the objects subdirectory. It should create a agents subdirectory there.

If you run this code you should see something like this:

../_images/entities_screenshot.png

This may vary if you used a different map.

The next tutorial will show how to control entities.

Table Of Contents

Previous topic

Getting something displayed

Next topic

Accessing and controlling entities

This Page