Changing color of object by script (tutorial script)

Hello,

First of all i didnt know where to place this so i hope i am at the right location here.

Ok my problem, i am following a tutorial for basic movement/schooting/hitting things.
(its called: Introduction to Scripting in the Blender Game Engine - part 2 )

Its as simple as the video everything is correct but i cant get the color to work.
i can move it, shoot balls, move object when pushing it, all things works exept the color and the hp part (when hp is low, color of the object change).

Can anyone help me out with this?

Here is the script:

obstacleScript


import bge
def main():
 
    cont = bge.logic.getCurrentController()
    obstacle = cont.owner
    keyboard = bge.logic.keyboard
    scene = bge.logic.getCurrentScene()
 
    if 'hp' not in obstacle:
        start(obstacle)
 
        obstacle.color = (1, obstacle['hp'], obstacle['hp'], 1)
 
 
            if obstacle['hp'] <= 0:
                die(obstacle, scene)
 
 
def start(obstacle):    
    obstacle['hp'] = 1  
 
 
def die(obstacle, scene):
    #expl = scene.addObject("ExplosionSound", obstacle.name, 60)
    #expl.actuators["Sound"].startSound()
    obstacle.endObject()
 
 
main() 
 

playerScript


import bge
def main():
    
    cont = bge.logic.getCurrentController()
    poppetje = cont.owner
    keyboard = bge.logic.keyboard
    scene = bge.logic.getCurrentScene()
    
    Movspeed = 0.08
    Rotspeed = 0.08
   
    kogelsnelheid = 1000
    
    if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.UPARROWKEY]:
        #vooruit
        poppetje.applyMovement((0, Movspeed, 0), True)
    
    if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.DOWNARROWKEY]:
        #Achteruit
        poppetje.applyMovement((0, -Movspeed, 0), True)
            
    if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.LEFTARROWKEY]:
        #links
        poppetje.applyRotation((0, 0, Rotspeed), True)
    if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.RIGHTARROWKEY]:
        #Rechts
        poppetje.applyRotation((0, 0, -Rotspeed), True)  
        
    if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.SPACEKEY]:
        #omhoog
        poppetje.applyMovement((0, 0, 0.1), True)   
    
    
    #kogel afschieten
    if bge.logic.KX_INPUT_JUST_ACTIVATED == keyboard.events[bge.events.LEFTCTRLKEY]:    
        kogel = scene.addObject("kogel", "kogellocatie", 240)
        kogel.applyForce ((0, kogelsnelheid, 0), True)
        kogel.color = (1, 0, 0, 1)
        
main() 

bulletscript

import bge
def main():
 
    cont = bge.logic.getCurrentController()
    kogel = cont.owner
    keyboard = bge.logic.keyboard
    scene = bge.logic.getCurrentScene()
    touch = cont.sensors['Touch']
    hit = touch.positive
 
    damage = 0.4
 
    if 'active' not in kogel:
        start(kogel)
 
 
    if hit and kogel['active'] == True:
        print(touch.hitObject.name)
        kogel['active'] = False
        kogel.color = (1, 1, 1, 1)
        touch.hitObject['hp'] -= damage
 
 
 
 
 
 
 
def start(kogel):
    kogel['active'] = True
 
 
main()

Go away from script mode. It is outdated. The module mode provides much more benefits.

You can perform the color change (even a nice color change animation) with a few logic bricks.

action: “object color action” should contain key frames at min hp at max hp (beside object color change it can even contain other animations)
property: hp (float)
property sensor “hp” changed (True Pulse) -> OR -> ActionActuator Property Mode property “hp” action “object color action”

Very easy, very flexible, no code needed, reusable even with other actions.

Your code:
Check the console. I’m pretty sure you will find some errors there ;). That is the reason why nothing runs as you expect.

Make sure “Object Color” is enabled in the object’s material. It is off by default:
http://www.pasteall.org/pic/32217

Go away from script mode. It is outdated. The module mode provides much more benefits
in the tutorial he say that its better to use scripts instead of adding a lot of modules.

Make sure “Object Color” is enabled in the object’s material. It is off by default:

i enabled it but for know only the bullet change colors.

Go away from script mode. It is outdated. The module mode provides much more benefits
in the tutorial he say that its better to use scripts instead of adding a lot of modules.

Make sure “Object Color” is enabled in the object’s material. It is off by default:

i enabled it but for know only the bullet change colors.
atm i have found a way to open the system console, it given me 2 errors, i have fixed one.
the other error i get is:
KeyError: ‘value = gameOb[key]: KX_GameObject, key “hp” does not exist’
File “kogelscript” line 35 in module and line 22 in main.


import bge
def main():
 
    cont = bge.logic.getCurrentController()
    kogel = cont.owner
    keyboard = bge.logic.keyboard
    scene = bge.logic.getCurrentScene()
    touch = cont.sensors['Touch']
    hit = touch.positive
 
    damage = 0.4
 
    if 'active' not in kogel:
        start(kogel)
 
 
    if hit and kogel['active'] == True:
        print(touch.hitObject.name)
        kogel['active'] = False
        kogel.color = (1, 1, 1, 1)
#22     touch.hitObject['hp'] -= damage
 
 
def start(kogel):
    kogel['active'] = True
 
 
#35 main()

anyone knows how too solve this problem?
Thank you.

solved a few things, but the color is not working.
At first post, u see the “if” and “die”? A few “tabs” to much in front of it. xD

Everything works, except the color from the hitted object.

I can shoot it and after a few hits it is gone, so thats working.
The color wont change, if i set the “hp” to 0.5 and hit “P” the color changes, but when the obstacle get hit, it wont change.

and i gained an error that there is no hp set for the kogel (bullet)?
tried to solve it, but it doesnt work as i wanted to so this error is still around.(only when a bullet hits another bullet)

Does anyone know how to solve it?

Thank you.

Sounds like a misunderstanding. The scripts tend to sum up as you need one script for one purpose. You can use one module for different purposes.

Without a demo file it is hard to tell. Your code is just half of your logic. We need to see all.

Btw. you can debug with print statements:


print("obstacle.color",obstacle.color)

Sounds like a misunderstanding. The scripts tend to sum up as you need one script for one purpose. You can use one module for different purposes.

ok i do not know that, i am just a starter. heard about this program and trying to make things / following tutorials.
and in that tutorial he says that it is better to have script instead of modules, becouse u can change it easier then with modules i think.

i am new to this program, and just trying to build things.
love the program already :smiley:

I have put my script online to download it, here it is:
http://uploading.com/files/mbb2c1f5/basis%2Bgame%2Bengine%252B%2Bscript.blend/

I am dutch and using some dutch words in the script, i made a hello script just to translate the words, and to let u know what my problem is. (it is still the color change, would be great if it works, and i got a little problem with the shards, but thats written inside)

Hopefully u can do something with this, and solve my problem.
And please if u can solve it let me know what u did :slight_smile:

Thank you.

If you want a comparison between the modes you can look at wiki.blender.org.

You can find some guides to the BGE in my signature incl. Python coding. They might help you to get familiar with the BGE.

Hey,

After looking around for a few hours i found a new tutorial.
its from a person at 7th degree making a game.

i must say that i now know what u ment with scripting is outdated, those modules/actuators etc are really handy and easy to do…
even got mouse movement know :D.

wel scripting is not a problem but i have never worked with .py :slight_smile: (php looks the same only u nead to leave alot of dots. [,], ;, behind haha.

so i am gonna say thanks alot for pointing me in the right direction.

Thanks.