I introduce to you, the (hopefully) next step forward in BGE logic: components. I was playing around with Unity and I liked how their component system worked, so I scratched my previous game object class work, and decided to go with components instead.

So, what exactly is a component?

The idea of a component is a simple one. They are modules that can be attached to game objects. You can attach as many as you want, and each one serves a specific purpose such as third person character movement with WASD. After a component has been attached to an object, it can have various exposed settings that you can edit. In the case of a third person movement component, this could be things such as movement speed and turn speed.

So, how do I make components?

Components are Python classes that subclass KX_PythonComponent. For settings they have a class level “args” dictionary with the keys being the name of the property/setting and the values being the default values for that setting. The type of the value also determines what type the property will have (e.g., a value of 2.6 will mean that the property will be a float property). Currently, only integer, float, string and boolean are supported. A component also has a start() method which accepts a dictionary. This dictionary will match the args dictionary, but will have values from the user instead of the defaults. Every frame the engine is running, a components update() method is called. This will most likely be the bulk of most components. In the movement example this includes checking for user input and acting accordingly. And lastly, an example component:

import bge

class ThirdPerson(bge.types.KX_PythonComponent):
	"""Basic third person controls

	W: move forward
	A: turn left
	S: move backward
	D: turn right

	"""

	args = {
		"Move Speed": 10,
		"Turn Speed": 0.04
	}

	def start(self, args):
		self.move_speed = args['Move Speed']
		self.turn_speed = args['Turn Speed']

	def update(self):
		keyboard = bge.logic.keyboard.events

		move = 0
		rotate = 0

		if keyboard[bge.events.WKEY]:
			move += self.move_speed
		if keyboard[bge.events.SKEY]:
			move -= self.move_speed

		if keyboard[bge.events.AKEY]:
			rotate += self.turn_speed
		if keyboard[bge.events.DKEY]:
			rotate -= self.turn_speed

		self.object.setLinearVelocity((0, move, 0), True)
		self.object.applyRotation((0, 0, rotate), True)

Cool, have a video or something showing this?

Yup, right here.

Nice, when can we expect this in trunk?

Short answer: when it’s ready.

Longer answer: More tests need to be run and code needs to be cleaned up. From there I will have to get someone to review this as it’s a larger patch. And from there, I don’t know if it will make it in before 2.6 as there is a semi-freeze in affect for the SVN. The focus is supposed to be on stabilizing and bug fixing, not adding new toys. If the patch only affected BGE code, then it would be easier to get in, but it has Blender code changes, which people are a bit stricter about.

Well, do you have any tests builds then?

Nope, not at this time. But, you can grab the patch in it’s raw form from here. I say raw because I haven’t gone back over it to remove no longer needed stuff (commented out code, debug prints, etc).

Stay tuned for updates and possibly some builds!

 

EDIT: The previous patch was missing files. I’ve uploaded a newer version.

Cheers,
Moguri

Advertisements