Control UV map with Game Property


(Goratrix) #1

Hi,
I have a small problem with Python. In front I have say my Python skill is very poor as well as my English, please excuse that.

In the bge I have created a life and mana bar by shifting the UV map, at the x axis for visual effect, and at the y axis as equivalent to the hitpoints. For the x axis I use the following script that I have found in this forum, posted by HG1. (I just understand the begin and the last 4 lines)

from bge import logic as GL
cont = GL.getCurrentController()
own = cont.owner

#The recomended max. speed is 0.10 and the min. is 0.009
mesh = own.meshes[0]
array = mesh.getVertexArrayLength(0)

for v in range(0,array):
vert = mesh.getVertex(0,v)
uv = vert.getUV()
uv[0] += 0.001
uv[1] += 0
vert.setUV(uv)

For the y axis I use an animated material that changes the y mapping value in an other material. This way I can control the animation with the logicbrics. That works, but I would like to have a more ‘clean’ solution. To do that I would like to alter the script, to animate both axis with the script, but I do not know how to do that.
So I think, at first I have to include the GameProperty (Life) that represent the hitpoints into the script. So I guess I have to add the following line to the script: life = float(own[‘Life’]).Than I have to get the original location of the UV map and store that in away that it is not altered by the script anymore, and than I have to add ‘life’ to the original location and write that value back to the UV map. But I don’t know how. Can some help me with this problem please.


(Goratrix) #2

Update

Thanks to HG1 who changed the script to solve my problem.

from bge import logic
cont = logic.getCurrentController()
own = cont.owner

#The recomended max. speed is 0.10 and the min. is 0.009
mesh = own.meshes[0]
array = mesh.getVertexArrayLength(0)

Normalerweise sollten die Werte 0.1 und 100.0 betragen. Aber wegen der

skalierten UV-Map müssen die Werte angepasst werden.

life = 0.49 - own[“Percent”] / 199.0

if not “uv” in own:
own[“uv”] = [0.0]*array
for v in range(0,array):
vert = mesh.getVertex(0,v)
own[“uv”][v] = vert.getUV()

for v in range(0,array):
vert = mesh.getVertex(0,v)
uv = vert.getUV()
uv[0] += 0.001 # normale UV-Scroll auf X-Achse
uv[1] = own[“uv”][v][1] + life # Prozentwert über Y-Achse
vert.setUV(uv)