Realtime Digital Clock

I’m trying to approach a problem of creating a realtime digital clock that you can set with a arbitrary time. (In the end the clock will effect the day/night cycle of the game)

My first thought was creating a texture with the numbers 0-9 and a colon like this-

0123456789:

then “sliding” several squares over the UV texture based upon a text attribute updated by a python script.

I have a screenshot showing me about 50% there…


Now the question is how two fold…

  1. How do I move the UV “squares” around based on the text attribute? (Circled)

  2. More generically, the game always starts a 7:15am and I’m thinking I have to use some time function of python to save what time it is and then adjust my clock.

I saw a post around here for a day/night cycle so I’ll append to that when I get the core clock figured out.

I, once again, answered my own question. Sometimes you have to power through the problem to come up with a solution.

Here is my very ugly and very hard coded answer to my own problem.

from bge import logic

uv_size=0.0797
cont = bge.logic.getCurrentController()
clock = cont.owner

mesh = clock.meshes[0] #access the mesh of the clock

clock_disp = clock["time"] 

char_len=len(clock_disp)

#if the clock is 5 characters long, there is a "1" in the front
#else it's blank

if char_len==4:
    first_uv=0.92
else:
    first_uv=uv_size

#calculate the offsets on the UV map
fourth_uv = float(clock_disp[-1:])*uv_size
third_uv = float(clock_disp[-2:-1])*uv_size
second_uv = float(clock_disp[-4:-3])*uv_size

#map the UVs
#1st number
vert = mesh.getVertex(0,87)
uv = vert.getUV()
uv[0]=first_uv
vert.setUV(uv)

vert = mesh.getVertex(0,86)
uv = vert.getUV()
uv[0]=first_uv+uv_size
vert.setUV(uv)

vert = mesh.getVertex(0,84)
uv = vert.getUV()
uv[0]=first_uv
vert.setUV(uv)

vert = mesh.getVertex(0,85)
uv = vert.getUV()
uv[0]=first_uv+uv_size
vert.setUV(uv)

#2nd number
vert = mesh.getVertex(0,91)
uv = vert.getUV()
uv[0]=second_uv
vert.setUV(uv)

vert = mesh.getVertex(0,90)
uv = vert.getUV()
uv[0]=second_uv+uv_size
vert.setUV(uv)

vert = mesh.getVertex(0,88)
uv = vert.getUV()
uv[0]=second_uv
vert.setUV(uv)

vert = mesh.getVertex(0,89)
uv = vert.getUV()
uv[0]=second_uv+uv_size
vert.setUV(uv)

#3rd number
vert = mesh.getVertex(0,99)
uv = vert.getUV()
uv[0]=third_uv
vert.setUV(uv)

vert = mesh.getVertex(0,98)
uv = vert.getUV()
uv[0]=third_uv+uv_size
vert.setUV(uv)

vert = mesh.getVertex(0,96)
uv = vert.getUV()
uv[0]=third_uv
vert.setUV(uv)

vert = mesh.getVertex(0,97)
uv = vert.getUV()
uv[0]=third_uv+uv_size
vert.setUV(uv)

#4th number
vert = mesh.getVertex(0,31)
uv = vert.getUV()
uv[0]=fourth_uv
vert.setUV(uv)

vert = mesh.getVertex(0,30)
uv = vert.getUV()
uv[0]=fourth_uv+uv_size
vert.setUV(uv)

vert = mesh.getVertex(0,28)
uv = vert.getUV()
uv[0]=fourth_uv
vert.setUV(uv)

vert = mesh.getVertex(0,29)
uv = vert.getUV()
uv[0]=fourth_uv+uv_size
vert.setUV(uv)   

For those who wish to follow in my footsteps, what you need to do is find the vertexes you want to change. I did this by moving a “square” off the texture to lower right and see what uv verts went negative. Then I had to figure out the size of each number on the VP map and coded everything up. It’s messy, but it works.

There are some clock threads in the resource forum.