Automatically and repeatedly executing a script

Help bring our custom Ages to life! Share tips and tricks, as well as code samples with other developers.

Automatically and repeatedly executing a script

Postby Locutus » Wed Mar 13, 2013 7:11 am

Hello!

I'm looking for a way to automatically and repeatedly executing certain Python commands. In this case, I want to adjust "Graphics.Renderer.SetYon" repeated to have the view distance adjusted depending on how far away my avatar is from certain coordinates.

I found the function "AtTimeCallback", but no real documentation on how to use it. What is a "PhythonFile component", how can I make a script file to be used with AtTimeCallback?

As usual, any help is appreciated!
Locutus
 
Posts: 29
Joined: Sat Mar 09, 2013 9:41 am

Re: Automatically and repeatedly executing a script

Postby Sirius » Wed Mar 13, 2013 7:36 am

PtAtTimeCallback(<python component>, <time in seconds>, <id>)

It calls the OnTimer(self, id) method of the class inheriting ptResponder (or ptModifier), pointed by the Python component (if you want your class to call its own OnTimer method, pass self.key as the argument).

The id you give to AtTimeCallback will be given to OnTimer, it allows you to know where in the script you called AtTimeCallback (for instance, you can use id 1 to indicate it is time to update the Yon distance, id 2 to indicate you want to change fog density, id 3 to indicate you need a pizza...)

Also, you have the function PtClearTimerCallbacks(<python component>), to clear all your timers.
User avatar
Sirius
 
Posts: 1506
Joined: Mon Jul 26, 2010 4:46 am
Location: France

Re: Automatically and repeatedly executing a script

Postby Locutus » Wed Mar 13, 2013 8:44 am

Oooh thanks, that helps!

I didn't know what this "PythonFile" thing was supposed to be; I found examples for the AtTimeCallback now in the game python code and am making something built on that.

Do you happen to know how I can get the game to recompile a .py file after I made changes (without restarting it)? It doesn't seem to notice when I changed the source, even if I delete the .pyc file, so I end up saving it under a new name all the time now, which is very uncomfortable.
Locutus
 
Posts: 29
Joined: Sat Mar 09, 2013 9:41 am

Re: Automatically and repeatedly executing a script

Postby Locutus » Wed Mar 13, 2013 9:01 am

There seems to be a problem with the "ptKey" parameter in my attempt. I don't know though what I did wrong - I think I followed the example of in this case the script for the Kadish door in Ahnonay correctly.

I have this Python code:

Code: Select all
from Plasma import *
from PlasmaTypes import *
import string
import copy

class timerTest(ptResponder):

   def __init__(self):
      ptResponder.__init__(self)
      self.id = 5633
      self.version = 3

   def onTimer(self,id):
      if id==1:
         av=PtGetLocalAvatar()
         p0=ptPoint3(35,3,0)
         p=av.position()
         p.setZ(0)
         d=p.distance(p0)
         PtConsole("Graphics.Renderer.SetYon {}".format(d))

   def timerOn(self):
      PtAtTimeCallback(self.key, 0, 1)

   def timerOff(self):
      PtClearTimerCallbacks(self.key)


I save that in "test.py". In the game console, I do:

Code: Select all
from test import *
t=timerTest()
t.onTimer(1)      # Works okay!
t.timerOn()       # Error!
TypeError: PtAtTimeCallback expects a ptKey, a float, and an unsigned long


The Kadish door example calls the function the same way though. Any idea what's wrong with the "self.key" here?
Locutus
 
Posts: 29
Joined: Sat Mar 09, 2013 9:41 am

Re: Automatically and repeatedly executing a script

Postby Christopher » Wed Mar 13, 2013 9:26 am

I don't know if it works this way. I think you have execute the file through a PythonFileMod (which means you have to export a PRP using this Python File).
It's also OnTimer (with capital O).

Christopher

EDIT: I forgot... Normally the Python file and the class has the same name in Uru. This could also be the problem...
EDIT2: I also forgot. If you execute Python files with the python console, there is a reload function. So you could do
Code: Select all
import test
*make some changes to test
reload(test)
User avatar
Christopher
 
Posts: 276
Joined: Mon Jul 19, 2010 3:25 am

Re: Automatically and repeatedly executing a script

Postby Locutus » Wed Mar 13, 2013 9:44 am

@Christopher: Thanks for your input!

I tried giving the .py file the same name as the class - same error. Also, "reload" gives me the error:

Code: Select all
from timerTest import *
<change stuff in timerTest>
reload(timerTest)
TypeError: reload() argument must be module


I tried "timerTest" and "timerTest.py" too, with and without quotes.

I found another function though that seems to work: PtSetAlarm. Seems though that it has a maximum resolution of about 0.5 seconds, which is too slow for my intended purpose. The Yon must be updated much faster.
Locutus
 
Posts: 29
Joined: Sat Mar 09, 2013 9:41 am

Re: Automatically and repeatedly executing a script

Postby Tsar Hoikas » Wed Mar 13, 2013 12:33 pm

If you're not going to be using a PythonFileMod, then PtSetAlarm is probably your best bet. You are correct that it is constrained to only update approximately every 0.5 seconds. This limitation is imposed on line 129 of pfPython/cyMisc.cpp. I'm sure you can remove that to get the precision you need ;)
Image
Tsar Hoikas
Councilor of Technical Direction
 
Posts: 2180
Joined: Fri Nov 16, 2007 9:45 pm
Location: South Georgia

Re: Automatically and repeatedly executing a script

Postby Christopher » Wed Mar 13, 2013 1:47 pm

Locutus wrote:TypeError: reload() argument must be module


try using
Code: Select all
import timerTest

instead of
Code: Select all
from timerTest import *


Only difference is that you have to call the functions with timerTest.function() then.

Christopher
User avatar
Christopher
 
Posts: 276
Joined: Mon Jul 19, 2010 3:25 am

Re: Automatically and repeatedly executing a script

Postby Locutus » Thu Mar 14, 2013 7:08 am

Tsar Hoikas wrote:If you're not going to be using a PythonFileMod


I'll gladly try the "PythonFileMod" method, but I have no idea how to do that. ;) Would you be willing to give me a quick pointer? Do I have to create a PRP file with PlasmaShop?
Locutus
 
Posts: 29
Joined: Sat Mar 09, 2013 9:41 am

Re: Automatically and repeatedly executing a script

Postby Mystler » Thu Mar 14, 2013 9:23 am

You will need at least a new page (PRP) file with an object and a PythonFileMod to your python file. You can use any URU exporter (3dsMax, Blender) but it should be possible with PlasmaShop too.
Maybe you should have a look at some simpler Cyan files as a reference.
User avatar
Mystler
 
Posts: 116
Joined: Sun Mar 22, 2009 4:55 am
Location: Germany

Next

Return to Scripting

Who is online

Users browsing this forum: No registered users and 0 guests