how can add a delay to a game using python?

Hello

I’m trying to do one game (I’m an absolute beginner) and I want that using a python script some elements changes his colour, play a sound and finally return to his original colour, I get that the objects changes his colous as I want but I have problems to make that blender get a delay after change the colour, somebody can give me a tip? At this moment I have this code:


import bge
import random

from bge import logic
from bge import constraints

objects = logic.getCurrentScene().objects

global tiradas
global turno
turno = 0
tiradas = []

nivel = 4
i = 0
object_1 = objects["blue"]
object_2 = objects["red"]
object_3 = objects["yellow"]
object_4 = objects["green"]

for i in range(nivel):
    val = random.randint(1, 4)
    tiradas.append(val);
    
    if val == 1:
        object_1.color = (0,0,1,1)
        object_1.color = (0,0,0.75,1)
    if val == 2:
        object_2.color = (1,0,0,1)
        object_2.color = (0.75,0,0,1)
    if val == 3:
        object_3.color = (1,1,0,1)
        object_3.color = (0.75,0.75,0,1)
    if val == 4:
        object_4.color = (0,1,0,1)
        object_4.color = (0,0.75,0,1)

I think that is just add some code between for example


        object_4.color = (0,1,0,1)
        object_4.color = (0,0.75,0,1)

but I didn’t found how to do that. Also if could help me to add the sound I will be very grateful, I think that this can be done using an actuator and I have they linked to the “object_1” etc but I don’t now how can I get the actuator with python, I think that to load the actuator is with this:


actuator = co.getActuator("actuatorname")
# Activate an actuator
GameLogic.addActiveActuator(actuator, True)

but I’m not sure how can I get the controller for an object using the code that I have, maybe with something like:


object_4.getCotroller("controllername")

???.

Thank you.
Regards.

Have you tried using a timer property? you could write an if statement. E.g


if own["timer"] > 60:
      do-stuff()
      own["timer"] = 0

thank you for the response, I did a test doing this:


import bge
import random
import time

from bge import logic
from bge import constraints

objects = logic.getCurrentScene().objects

global tiradas
global turno
turno = 0
tiradas = []

nivel = 4
i = 0
object_1 = objects["blue"]
object_1["timer"] = 120
object_2 = objects["red"]
object_2["timer"] = 120
object_3 = objects["yellow"]
object_3["timer"] = 120
object_4 = objects["green"]
object_4["timer"] = 120

for i in range(nivel):
    val = random.randint(1, 4)
    tiradas.append(val);
    
    if val == 1:
        object_1.color = (0,0,1,1)
        while object_1["timer"] < 60:
            i  = 0
            while i < 1000:
                print(object_1["timer"])
                i = i + 1
                
            object_1["timer"] = 0
        object_1.color = (0,0,0.75,1)
    if val == 2:
        object_2.color = (1,0,0,1)
        while object_2["timer"] < 60:
            i  = 0
            while i < 1000:
                i = i + 1
                print(object_1["timer"])
                
            object_2["timer"] = 0
        object_2.color = (0.75,0,0,1)
    if val == 3:
        object_3.color = (1,1,0,1)
        while object_3["timer"] < 60:
            i  = 0
            while i < 1000:
                i = i + 1
                print(object_1["timer"])
                
            object_3["timer"] = 0
        object_3.color = (0.75,0.75,0,1)
    if val == 4:
        object_4.color = (0,1,0,1)
        while object_4["timer"] < 60:
            i  = 0
            while i < 1000:
                i = i + 1
                print(object_1["timer"])
                
            object_4["timer"] = 0
        object_4.color = (0,0.75,0,1)

but that does not work :frowning:

Wait if I understand your code there you are using a while loop for a timer This is not a good idea on many levels. What I meant was give your object a property or maybey you need several timers (green-timer, yellow-timer, etc) and set it/them to the “timer” type.(the drop-down menu beside the property) Have an if statement check if they are over a certain value(60?) then do stuff.

If the above wasn’t helpful enough you might want to post a blend file.

Hello

Actually I’m an absolute beginner in Blender and I’m very lost in that, I attached the .blend file I hope found how to solve that because I really like blender and I want learn to make games… The script with the problem is on the main script, it is linked to the camera (I know that this does not have sense but I did not found how to set it as “global”

Thanks you for all the support.

Attachments

untitled.blend (594 KB)

I have attached a blend file with changes to do the things that you wanted to do in your first post. I’m not so sure if sounds pack so if there are no sounds just put one of your own in the sound actuator.

Attachments

delayedcolour.blend (984 KB)

Hello

Thank you so so much for the file, now I have a better understand of how to do that. Just a little question more, how can I make that generating random numbers make the same action that the object makes when the user make a click? Is that possible? I think that I can do something like the one that I was trying to do but I don’t know if it’s possible get the controller from an object or if it’s necessary set it to can get the sensors etc.

And if I understand the file properly, to get that is: add the Timer as a game property (that is linked to each object) and using the controllers I can set the timer, change the colour and play the sound and when the Timer expires return to the original state, that is right?

again, thank you very much.
regards.

What you had set up before with the randint function would work. Yes you can do all this stuff without python :P( but it’s good to learn and some things would require a bit more work in logic bricks.)

ok, thank you very much for all your help, I will try and play a but around that.

Regards.