Time.schedule() does not let me call a function

I tried to make a function be scheduled after a period of time, e.g.

time.schedule(function, 10)

def function():

  • pass*

I do not know how to fix this issue, am I doing something wrong?

3 Likes

@DamonPython Like this or did you mean something else?

2 Likes

Yes, like that, but in Python.

2 Likes

You need to define the function before you call it, ie:

def function():

time.schedule(function, 10)

otherwise it produces an error.

3 Likes

But I see techleapz already explained, usefull too because I just got it to work by sheer trial and error and would not know why mine worked :joy:

2 Likes

Yes, it works now, thanks. Also, how do I use time.schedule_repeating, I tried this but all I got was code that did nothing. There didn’t seem to be any compilation errors I was alerted of.

2 Likes

@DamonPython I’ve updated my space to work using time.schedule_repeating; Why it works I still wouldn’t know as this is my first experience with python, seems like its kind of like a for loop?, where the function must have a parameter, and the time delay is passed in automatically as its parameter? No idea, hopefully @techleapnz can shed some light on this.

1 Like

With time.schedule_repeating, you’ll see there are 2 options:

  1. Set the period parameter to the number of seconds between each repeated function call, e.g time.schedule_repeating(function, 10) will repeat the function every 10 seconds.

  2. Leave out the period parameter, which will mean it will run the function every single frame (which varies, but is often 60 frames per second), e.g. time.schedule_repeating(function)

@Luc is right - this is a timed loop.

3 Likes

@techleapnz How would you pass a parameter to the function thats being called in this case?
Because if I do:
time.schedule_repeating(moveblock(1), 1) it works once, but then stops.

(as opposed to the working version:)
time.schedule_repeating(moveblock, 1)

2 Likes

Hi @Luc, this parameter takes a callback function, and only accepts the name of a function to run. If you use parentheses, you are essentially running the function then and there.

In Typescript you get around this by passing in an anonymous function, e.g. ()=>{moveblock(1)}.

I’m no python guru, but I would simply create a new function called moveblock1 which contains your function with a parameter, and call that in the loop. Otherwise, you could try Python anonymous functions and see if it works.

Does that help?

3 Likes

Ah thank you! I asked mainly because I did not understand and not so much because I needed it for something. But learning about those callback functions is really usefull, definitely heard them mentioned before but did not know what it ment, nor think too much of it. I’m gonna go look further into them, thank you once again for the help!

3 Likes

Thanks Luc and Techleapnz for your help, appreciate it :slight_smile:

3 Likes

Hi, I’m back.
Just want to know what is wrong with mine, I tried and it is not running the function.
This is what I’ve written…

time.schedule(chest_open, 10)

def chest_open():
pass

1 Like

See my original reply:

2 Likes

My bad,
Also, I tried what you said Techleapnz, but it still doesn’t work.

1 Like

Can you make your CoSpace remixable and share it here? That would make it easier to debug.

1 Like

Sorry I can’t, it is in a school profile which doesn’t allow sharing.

1 Like

Can you get your teacher to share it as unlisted and give you a share code? Otherwise, I can’t see a way to easily debug this.

1 Like

Try calling the function before the time.schedule as well. Also include a mysterious (dt) argument. This is what worked for me:

truck.transition.move_on_path(path=testPath, time = 3.0)
def moveTruck(dt):
    truck.transition.move_on_path(path=testPath, time = 3.0)

time.schedule_repeating(moveTruck, 3.0)

Credit to Medard for the dt code: CoSpaces Edu :: Solar system - Python code

1 Like