April 11, 2010

Scripting AGE

AGE supports currently Python scripts. This means it gives you more rapid development cycles when you don't need to rebuild your c/#/++ project all over again.

Currently the system is as follows:

You make main.py that includes, at minimum, following lines:
# Import age library
import age

# Initialize AGE, give it title "AGE" and set
# window size to 768x480
age.Driver.initEngine("AGE", 768, 480)

# Loop driver update cycles as long as the engine
# is not "done" with the rendering
while not age.Driver.isDone()
age.Driver.update()


That's it.

Then, if you want to add items, like camera, you add following lines after age.Driver.initEngine:
# Create new scene, set it active and get it
curScene = age.Driver.getScene(age.Driver.addScene('Scene001', 1))

# Add a camera to our scene and make it active
cam = curScene.getCamera(curScene.addCamera('Cam001', 1))
cam.set( age.Vec3f(0.0, 0.0, 0.0), # Position
age.Eul(0.0, 0.0, 0.0), # Rotation
age.Vec2s(0.0, 0.0), # Viewport offset
age.Vec2s(768, 480), # Viewport size
age.Vec2f(1.0, 1000.0), # Clip distances
45.0) # FOV

# Set the script file camera uses, without the extension (.py)
cam.setPyScript("camera")


Then you need to create a new file, called camera.py that includes following lines to enable fly through:
import age

# Get the camera as global variable
me = age.Driver.getCamera("Scene001", "Cam001")

# Camera action
def on_update():
# Check if WASD or gamepad's Point Of View (Directional btns)
# are pressed and move accordingly
if age.Input.getKey(age.KEY_W) == 1 \
or age.Input.getPadBtn(0, age.BTN_POV_UP) == 1:
me.moveOriented(age.Vec3f(0.0, 0.0, -10*age.Driver.getStep()))
if age.Input.getKey(age.KEY_S) == 1 \
or age.Input.getPadBtn(0, age.BTN_POV_DOWN) == 1:
me.moveOriented(age.Vec3f(0.0, 0.0, 10*age.Driver.getStep()))
if age.Input.getKey(age.KEY_A) == 1 \
or age.Input.getPadBtn(0, age.BTN_POV_LEFT) == 1:
me.moveOriented(age.Vec3f(-10*age.Driver.getStep(), 0.0, 0.0))
if age.Input.getKey(age.KEY_D) == 1 \
or age.Input.getPadBtn(0, age.BTN_POV_RIGHT) == 1:
me.moveOriented(age.Vec3f(10*age.Driver.getStep(), 0.0, 0.0))

# Rotate camera 150 degrees per second in X and 120 degrees
# per second in Z
curRot = age.Eul()
curRot.x -= age.Input.getMouseForce().x*150*age.Driver.getStep()
curRot.z -= age.Input.getMouseForce().y*120*age.Driver.getStep()

me.rotate(curRot) # Apply rotation to camera


Not so hard, but alot code to take in at once.

The "def on_update()" is a function that is called automagically inside the engine. You can also create your own, but you have to call them inside one of the predefined, automatically called functions.
  • on_[event]() : when an event; collision, deletion, scan and so on happens. how will the entity react to it
Also on a related note, you would normally call the camera, etc. creation code inside the scene on_init() function, that is called when the entity/object is created.

NOTE: This is the current method, which I most likely will change later for more dynamic script usage.

No comments: