http://jerome.le.chat.free.fr

Accueil > Blender > Scripts > Sun Engine - afficher la date et l'heure dans le jeu

Sun Engine - afficher la date et l'heure dans le jeu

Index de l'article
Sun Engine
latitude et longitude
configurer la date et l'heure
vitesse de révolution du soleil
configuration avancée
modifier la vitesse de révolution du soleil pendant le jeu
afficher la date et l'heure dans le jeu
intégration du script
mécanisme alternatif pour la position du soleil
réference des objets et des propriétés

 

use the game engine debug function (optional) :

  • select your sun object,
  • in the logic panel, add the following properties :
    . cyear (type:int)
    . cmonth (type:int)
    . cday (type:int)
    . chours (type:int)
    . cmins (type:int)
    . csecs (type:int)
    . cdaystring (type:string) - display the day of the week,
    . cmonthstring (type:string) - display the month full name,
    . cdayshortstring (type:string) - display the day of the week, short format,
    . cmonthshortstring (type:string) - display the month short name.
    . TZ (type:int) - Time Zone
    . DST (type:int) - Daylight Saving Time
    . localtime (type:string) - the current time in seconds since epoch (01/01/1970)
  • for each properties, activate debug ( D button ont the right of each props)
  • in the game main menu, enable show debug property
  • start the G.E.

the values should update in the debug


these properties are created on the fly by the script, so you don't need to configure them if you don't need to debug.

what if I want to use another language for months and days names than the one used by my system, or to force a language ?

  • select your sun object,
  • in the logic panel, add the following property :
    . uselocalstring (typr:Bool)
    . set it to False
  • in the sun_engine.py script, search the string days=[' (ctrl+F in the text window). you'll see the following lines :
    days=['lundi','mardi','mercredi','jeudi','vendredi','samedi','dimanche']
    months=['janvier','février','mars','avril','mai','juin','juillet','aout','septembre','octobre','novembre','decembre']
    shortdays=['lun','mar','mer','jeu','ven','sam','dim']
    shortmonths=['jan','fév','mar','avr','mai','juin','juil','aout','sept','oct','nov','dec']
  • edit each list with your own language

a trick has to be found for funny characters..


use an overlay scene (step 1/3) :

  • add an overlay scene (empty option), call it interface (not mandatory). a nice tutorial :
    http://www.tutorialsforblender3d.com/GameDoc/OverlayScene/OverlayScene_Steps_1.html
    the empty name that initailizes the overlay scene is init
  • create a blender game engine font. another nice tutorial :
    http://www.tutorialsforblender3d.com/GameDoc/Text/Text_GLSL_Steps_1.html
  • you have now a 'game engine text' object with a Text property. select it call it date (not mandatory). in the logic panel :
    . in the Text property type init...
    . locate it at the top left corner of the camera view.
  • for debug purpose, add a csecs property (type:int) to the date object, enable the D button and activate show debug.
  • back to the first scene scene, select your sun object,
  • in the logic panel, add the following property :
    . sundisplay (type:string)
    . in the value text field, type : interface/OBdate
  • set the realsunspeed property to True
  • run the Game engine.

if everything works fine, the csecs property should be updated every second


explanations :

sundisplay
tells the script which object to update the date and time with. the syntax is :

name_of_the_scene_where_the_object_is/game_engine_name_of_the_object

if the display object is in the same scene than the sun, you can type :

game_engine_name_of_the_object

if this property is omitted, the sun display object will be the sun configuration object (the one with lat, long, year etc..)

don't forget to add 'OB' at the beginning of the object name.

 

now you can turn off the time messages in the blender console :

  • select your sun object,
  • in the logic panel, add the following property :
    . sundebug (type:Bool)
  • set it to False

delay the script execution (step 2/3) :

in the Blender console, when the game just start, you can see the following error :

Python script error from controller "sun_engine.py#CONTR#1":
Traceback (most recent call last):
File "sun_engine.py", line 227, in <module>
KeyError: 'interface'

it means that the scene named interface has not been found, since the game engine didn't have enough time to attach the scene as an overlay. the error occurs just one time : once the scene has been attached, the script can find the display object to update and everything is ok.

to avoid this error, we'll ask the script to wait for the scene to be attached as an overlay :

  • select your sun object,
  • in logic panel, add a property sensor :
    . name it sunrun,
    . in the Prop text field type a property name like go (no need to create it in the properties),
    . in the 'value' field type True.
  • attach the sunrun sensor to the sun_engine.py python controler.

this will tell the script to wait that the value stored in the sunrun sensor becomes True before to execute itself.
precisely, the test is :

if co.sensors['sunrun'].positive : execute

so you can use any property name you want inside it.

in the following example I'll ask the game engine to wait a short time, then to set a 'go' property to True.

  • select your sun object,
  • in logic panel,
    . add a delay sensor, set Delay to 25,
    . add an AND controler,
    . add a property actuator, change type to assign, type go in the 'Prop' text field , type True in the 'Value' text field
    . link them together.
  • attach the sunrun sensor to the sun_engine.py python controler.

  • start the G.E.

the error disappears

 

use an overlay scene (step 3/3) :

  • copy the following code :
co = GameLogic.getCurrentController()
own = co.owner
sc=GameLogic.getCurrentScene()
if co.sensors['sensor'].positive and 'cday' in own.getPropertyNames() :
day=own['cday']
if day==1 : day='1st'
elif day==2 : day='2nd'
elif day==3 : day='3rd'
else : day=str(day)+'th'
TZ=own['cTZ']
if TZ > 0 : TZ='UTC+'+str(TZ)
elif TZ < 0 : TZ='UTC'+str(TZ)
else : TZ = 'UTC'
own['Text']='%s, %s of %s %04d %s %02d:%02d:%02d' \
%(own['cdaystring'],day,own['cmonthstring'],own['cyear'],TZ,own['chours'],own['cmins'],own['csecs'])
# another way to use :
#import time
#ctime=time.localtime(own['gametime'])
#print time.strftime('%c',ctime)
  • in a text window, 'add new', and paste the code in the text window,
  • call it display.py.
  • in the scene interface, select the date object.
  • in the logic panel :
    . add an 'always' sensor. activate TRUE level triggering,
    . add a 'python' controler. name it display.py. in the 'script' text field type display.py,
    . link the sensor to the actuator.

  • disable debug options (delete the csecs properties and disable the show debug option in the game menu)
  • run the Game engine.

 

you should obtain something that looks like the image above

 

you can build a date string thanks to to the cyear, cmonthstring, cmonthshortstring etc. but you can also use the localtime property and use the strftime function of the time module. the display code above provides an example, see the commented the last lines.
strftime format




Mise à jour le Vendredi, 23 Octobre 2009 14:20