sprint bars algorithm?

I have a sprint mechanic on my character and basically what i want to do is to have 10 bars at the top of the screen, and depending on the value of the sprint variable(1-10) a certain number of bars would be displayed, i’m using globalDict to send the value across scenes, but i have not been able to think of an effective way to determine which bars should be displayed and which shouldn’t. What i could do is check the value and have a separate “if-else” statement for each bar, but that amounts to more than 60 lines of code, which i don’t think is necessary, there must be a way to create some sort of algorithm to determine which bars to display based on a value of 1 to 10.

any help appreciated

Thanx
Tim

Place references to your bar items in a list. Iterate through the list a number of times equal to your stamina, and make those bars visible.
(you may want to have your stamina value go from 0-9, rather than 1-10. If you want to display the number as 1-10, add 1 to the display’s value.)


bars = [Bar, Bar, Bar, ...]

def set_bars(stamina):
    #flush all bars first
    for bar in bars:
        bar.visible = False

    #make stamina bars visible
    for i in range(stamina):
        bars[i].visible = True

It doesn’t seem to be doing anything, but i am not receiving errors in the console either

here is the script that saves the stamina value to globalDict:


import bge
import GameLogic


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


if own["sprint"] == True and own["timer"]>0:
    own["timer"] -= 1
    GameLogic.globalDict["number"] = own["timer"]


if own["sprint"] == False:
    if own["timer"] <10:
        own["timer"] +=1 
        GameLogic.globalDict["number"] = own["timer"] 

and here is the script which retrieves and applies the value:


import bge
import GameLogic


number = GameLogic.globalDict["number"]


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


scene = bge.logic.getCurrentScene()


bars = [scene.objects["1"], scene.objects["2"], scene.objects["3"], scene.objects["4"], scene.objects["5"], scene.objects["6"], scene.objects["7"], scene.objects["8"], scene.objects["9"], scene.objects["10"]]


for bar in bars:
    bar.visible = False
    
for i in range(number):
    bars[i].visible = True


have i done something wrong?

[EDIT: this post got screwed up somehow]

I did post a possible fix, but I’m going to just go ahead and build a prototype instead. I’m not 100% certain that what I posted is going to work…:o

Give me a minute…

stamina_bar.blend (497 KB)

A real simple setup.
Hold down left mouse button to drain stamina. It will gain 1 point every second.

It is messy. You probably want to set it up as a class, keep the bars list as a property of that, and run everything as built-in methods of that class.

Thank you for the help man, really appreciate it! I am still learning python and the one thing i cant get my head around is classes, so i wouldn’t know how to set one up, but the .blend is really helpful, thanx.