From BlenderWiki

Jump to: navigation, search
Note: This is an archived version of the Blender Developer Wiki. The current and active wiki is available on wiki.blender.org.

BGE Components Proposal

Overview

The BGE has no way to create a game with only Python. Instead, at least some number of logic bricks must be used (at the very least an always sensor and a Python script controller). Logic bricks can also not be shared (like a datablock) across multiple game objects, so they have to be setup again for non-instanced objects. Unity’s approach to scripting is to use what it calls “Components.” To use components, you write a class that subclasses a specific Unity object. Multiple components can be attached to an object, and components can even have exposed variables. I’d like to implement something similar to this in the BGE.

User Interface

Outliner

A new “Data Mode” will be added to view loaded Python packages. While in this mode, the Outliner could possibly get a button in the header to add new Python packages. “__internal__” will be loaded by default and will represent scripts in Text datablocks that end in “.py”. Within each package will be listed the classes found in the package that inherit/subclass from KX_Component.

Logic Editor

After a package and it’s classes have been loaded via the Outliner, the classes can be used as components for objects. In the properties panel of the Logic Editor, there will be a new panel devoted to assigning components to an object. When a component is added, a new “empty” one will be added (thus allowing any number of components on one object). Also when a component is added, argurments/parameters from the component will be shown and the user can adjust them. The arguments will be pulled from the component’s args property if it exists (see the example). Furthermore, there will be an Nth frame option to adjust how often the update method of the component will be run (0 for every frame).

Example Component

import bge

class PlayerComponent(bge.types.KX_Component):
	# The args dictionary tells Blender what variables we want the user
	# to be able to edit in the editor
	# The key will be displayed, and the value will be used as the default value
	# as well as the type
	args = {
		"Health": 10.0,
		"Energy": 6.0,
		"Regen": 2.0
		}
	
	# This is called when the object the component is attached to is created
	def start(args):
		self.health = args['Health']
		self.energy = args['Energy']
		self.regen = args['Regen']
		
	# This is called every frame
	def update(delta_time):
		# Check to see if the player is "dead" and end the object
		if self.health < 0.0:
			self.object.endObject()
	
		# Add 2.0 health every 5 seconds
		if delta_time > 5.0:
			self.health += self.regen

Implementation

Blender/DNA/RNA

Structs will be added to represent packages, components and arguments. Packages will have components, which in turn will have arguments. Objects will keep a reference to attached components or make a copy so arguments can be edited on a per object bases. However, this topic will need to be explored a bit more thoroughly as we want to be able to update defaults if a user hasn’t touched them.

BGE

At conversion time, the objects in the active layer will have a Python objects attached to them representing the components. When an object is added, the Python objects will be created from the components and their start() method will be called. Then, every frame during the logic updating step, each component’s update() method will be called. If the Nth frame option is used, then then a counter will be used to determine if the update() should be called (if counter > N: component.update(); counter=0; else: counter++).