Important! Please read the announcement at http://myst.dustbird.net/uru-account.htm
Also! Please read the retirement announcement at http://myst.dustbird.net/uru-retirement.htm

Dustin's Sixth Tutorial

From UamWiki

I think these might not work perfectly with the newer Pyprp versions, but they're here for posterity :P

Introduction

This is an advanced topic, so don't worry if you can't follow it.

The Python hook allows you to program things in your Age. It allows you to find out when the user presses a button, or clicks the mouse. It allows you to move objects, move your avatar, or Link to another Age. It allows you to open a book or journal. When clickables work, it will allow you to take an action, when the thing is clicked. There are plenty of other things it will allow you to do, as well.

Getting set up

  1. Install the Python hook, as described in AgeScripting.
  2. Install Python v2.2 It must be version 2.2. you can install it to a different folder than v2.3 or v2.4 .
  3. Install something to decompile with. There are several options, you can go to Cobbs to find one. I use UruPython v1.2 . You may want to try PlasmaShop. If you can get UruPython v3.1 to work for you, that may be the easiest way to go.
  4. Decompile all the files to some folder(say src, in the Python folder).
  5. Create a text file called "agename.py" where agename is your Age's filename.(e.g. for Dustin.age, use Dustin.py), and save it in the same folder.
  6. Here is a link to a template: http://www.agebuilder.org/~dustin/uploads/tut.py
  7. It must have that huge "glue" section at the bottom; and don't get excited, Cyan didn't write that code, it is simply appended by the IDE.
  8. You must change the
class tut(ptResponder,):

to

class agename(ptResponder,):

and the

print 'Dustin whatever'

to

print 'YourName whatever'
  1. Save, and compile the file, using the compile_all.py file that will have been decompiled with the others.
  2. Now you should have an "agename.pyc". You will need to place it inside of "agename.pak" using the tool.
  3. Copy "agename.pak" to the Python folder, and run Uru, and link to the Age, then quit Uru.
  4. All "print" commands are output to a log file.
  5. You can use the ELF Viewer tool, available at http://huru.aegura.com to view the log files. In particular, the output from the last time Uru was run can be found in Python.0.elf . The output from the previous time that Uru ran, can be found in Python.1.elf . The time before that, in Python.2.elf; and before that in Python.3.elf .
  6. Apparently, you can use PtPrintToScreen("string") to print directly to the screen, but I haven't tried it.
  7. Your entries should be at or near the bottom.

Things you can do

Getting notified of keystrokes

You can use this to tell when the user has pressed a key.

Linking to an Age

You can use this to link to an Age.

Moving an Object

Moving an object

Using the timer

Using a timer

Neat things that you can do with these.

Cause an object to orbit

from math import *
curStep = 0
numSteps = 0

In the OnServerInit section, you can:

global curStep
global numSteps
#set the timer for, say, 1/30 of a second.
curStep = 0
numSteps = 1000

In the OnTimer section, you can:

global curStep
curStep = curStep + 1
x = cos(2*pi*curStep/numSteps)
y = sin(2*pi*curStep/numSteps)
#then move the object to the position (x,y,z), where z is its (constant) z position.
#set the timer for, say, 1/30 of a second.

Note that the object may move faster or slower, depending on how hard your computer is working! If you want its speed to be independent of your computer's load, use the clock() function in the "time" module. It returns a float that allows you to measure time in seconds(the fractional part measures partial seconds).

from math import *
from time import *
period = 0.0
startTime = 0.0
objHeight = 0.0
t = None

In the OnServerInit section, you can:

global curStep
global numSteps
global startTime
global t
global objHeight
startTime = clock()
period = 60.0
t = PtFindSceneobject("DustinSphere1", "Dustin")
t.physics.suppress(true) #turns off physics for the object(so that it doesn't fall, for example)
pos = t.position()
objHeight = pos.getX()
PtAtTimeCallback(self.key, 0.03, 1)

In the OnTimer section, you can:

curTime = clock()
x = cos(2*pi*(curTime-startTime)/period)
y = sin(2*pi*(curTime-startTime)/period)
#then move the object to the position (x,y,z), where z is its (constant) z position.
p = ptPoint3(x, y, objHeight) #names a coordinate point. x=0.0, y=1.0, z=2.3
t.physics.warp(p) #Moves the object t to position p.
PtAtTimeCallback(self.key, 0.03, 1)

More things you can do.

More things are available in the API: UruAgeManager API.
For a tutorial on using them, see Dustin's Seventh Tutorial.