Physical Counter System, Rotation According To Variables?

Alright. So, I have some 10 sided counters that rotate that will be used to show ammo in magazine, number of magazines/ bullets remaining, HP, and various other applications.

I’m having trouble getting it started though. I’m still a noob in Python, and I don’t have the know-how to get these working how I need them too.

Basically, this is what I need to begin with:

I want the first three counters to be linked, in a way, so they function as one unit, or seemingly as one. Meaning, It will be a three digit counter, max of 999 and I want it to count down in order descending to 0. So three separate objects, once the far right reaches from 9 back to 9, the next one over needs to go down one in value, and so on for all of them.

I hope this isn’t too complex.


-------------------- Fire One Shot-----------------------


Can Someone give me a slight idea of how to do this. It would be better to have it explained, but just a code snippet will do as well.

Thank you to all who reply!

Do you want to do it in python to learn or are you after a solution?

This can be done easily with drivers without the need for any python script.

You can add a driver to the x rotation of a counter.

First add a customer property to the counter object called something like ‘unit’. You should limit it to between 0 and 9.

Then on the X rotation enter a driver. In the properties panel in the driver space set a variable which comes from the unit custom property of that object. The formula for the rotation is then ‘6.28 * unit / 10’

You can now spin and animate the counter by using the custom property. You then duplicate the counter twice and each should be adjustable with the custom property.

To get them working together add a custom property to the scene. Name it something like ‘count’ and copy the data path.

For the 100s counter set a driver for the unit custom property with the formula being: floor(count/100)
For the 10s the formula is: floor(count % 100 / 10)
And for units is: count % 10

You can then keyframe just the count custom property in the scene and the counters will do the right thing.

You could also apply this to a python script but it seems overkill.

Hope this helps

Stuart

Thanks! I had totally forgot about Drivers, this seems like a much easier solution. I wanted a Python code so I’d have something that would be harder to lose track of, but this makes more sense in simplicity.

I figured Python would be less of a headache, though. Down the road, I’m going to need it to reset to a certain number that is given by a user-chosen in-game variable (weapon selection).

I suppose this forum should have been placed under Game Engine Support, now that I think about it.

Thank you for your help, I really appreciate it!

Ah. I should have spotted from the screenshot that you are in the game engine.

You could do it just with logic blocks but that would be a bit of a spiders web.

I had a go and this is how I did it.

I made my three counters and called them counterUnits1, counterUnits10 and counterUnits100

I then chose one as the main object and added an integer counterVal property to it.

I made a property sensor for when counterVal changed and linked that to a python script. This script reads the counterVal property and then sets the counters to the right rotation (assuming that they are oriented with 0 initially). It uses hardcoded names for the counters so you would have to either change your objects names or the script to match each other.

import bge.logic

cont = bge.logic.getCurrentController() # bge.logic is automatically imported
scene = bge.logic.getCurrentScene()
obj = cont.owner
counterVal = obj.get('counterVal')
count1s = counterVal % 10
count10s = int(counterVal / 10 % 10)
count100s = int(counterVal / 100)
scene.objects['counterUnits1'].localOrientation = (3.14/5 * count1s,0,0)
scene.objects['counterUnits10'].localOrientation = (3.14/5 * count10s,0,0)
scene.objects['counterUnits100'].localOrientation = (3.14/5 * count100s,0,0)

Now whenever the counterVal is changed the counters will be placed correctly. You can the use logic blocks to change the value of counterVal by key or whatever method you want and the counters should match. My logic blocks look like this:


Thanks again! I was gone for a little while, I haven’t been on since. I really appreciate the help!