Python 'threading' in CC

For questions about using Classic.

Post » Sun Jul 31, 2011 11:47 am

Does Construct Classic supports python threading module?
I need it to use 'time.sleep()' without freezing of application and it works great in my python IDE, but doesn't work in Construct =\

http://dl.dropbox.com/u/24325446/pyThreading.cap
B
2
S
2
G
2
Posts: 158
Reputation: 1,366

Post » Sun Jul 31, 2011 1:14 pm

try the Timer() instead of sleep():

Timer(3.0, yourfunction).start()

I'm not sure, but I think sleep() is allways will freez the whole process - but as I said, I may wrong.
B
10
S
6
G
3
Posts: 127
Reputation: 2,481

Post » Sun Jul 31, 2011 3:10 pm

I tried threading.Timer, but I can't figure out how to make it work inside of a loop and with function's args

sleep() freezes only thread, this is why I'm trying to use it in separate threads, and as I said, it works in IDE and promt, but doesn't in Construct
B
2
S
2
G
2
Posts: 158
Reputation: 1,366

Post » Sun Jul 31, 2011 3:27 pm

OK, I just found how to deal with Timer ()
[code]
from threading import Timer


def run (input, delay):
    for i, ch in enumerate(input):
        t = Timer(delay*i, prnt, (ch))
        t.start ()

def prnt (txt):
    print txt



a = "abcdefg"
run(a, 0.2)

[/code]
>>> <typing letters with delay of 0.2>

But, again, it works only in Python BUT doesn't work in Construct!
In Construct (with "Text.text+= txt" instead of "print txt") it adds only first char of string.

So, Construct doesn't support threading module? If so, how can I implement timer with Python?
B
2
S
2
G
2
Posts: 158
Reputation: 1,366

Post » Sun Jul 31, 2011 7:59 pm

The reason it doesn't work is because Python is not run continuously, it's only run for the duration of the script and then control is given back to construct. You need to add a python script to be run every frame. It can be something as simple as "#".
B
79
S
24
G
53
Posts: 4,732
Reputation: 40,227

Post » Sun Jul 31, 2011 8:46 pm

Oh thanks, it works now!
You are Construct\Python guru, I had no chances to solve this on my own

B
2
S
2
G
2
Posts: 158
Reputation: 1,366

Post » Tue Aug 16, 2011 8:08 am

I have wrote a timeline in python before.
https://sites.google.com/a/binhua.twbbs.org/construct-project/FileStorage/timeLine.7z

Here is a sample code to
1. flash a sprite object immediately
2. then wait 2 second
3. rotate the sprite each 0.01 second

def test_iter(timer, obj):
    obj.Flash(0.1, 1)
    timer.delay_time = 2
    yield QTimer.CONTINUE

    timer.delay_time = 0.01
    while(1):
        obj.RotateClockwise(1)
        yield QTimer.CONTINUE
rexrainbow2011-08-18 05:21:10
B
97
S
22
G
177
Posts: 4,111
Reputation: 103,525

Post » Tue Aug 16, 2011 3:06 pm

Awesome.
What module[s] are you using, it's your own or parts of some library?
B
2
S
2
G
2
Posts: 158
Reputation: 1,366

Post » Wed Aug 17, 2011 3:15 am

Yes, it's my own. All of these source files are put in zip file.
The concept of timeline is simple.
At each tick:
1. get current time
2. pick timer which is time-out.
3. execute callback function of timer picked in step2


A performance issue is, it need call python function each tick.
rexrainbow2011-08-17 03:44:34
B
97
S
22
G
177
Posts: 4,111
Reputation: 103,525


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 9 guests