python script only runs once/ other issues

For questions about using Classic.

Post » Tue Nov 09, 2010 11:40 pm

I'm writing a script using python for finding a path for the object to move to (I know the RTS movement works for this). It's not complete, and it probably isn't the best written. One problem I'm running into is that it seems to run through the script only once. It's not under any construct events. Here's what I have so far.
[code:2buu5dmb]
#Pathfinding
from math import sqrt
d = sqrt(((unit.X-goal.X)**2)+((unit.Y-goal.Y)**2))
findpath = 'false'
if System.globalvar('start') == "yes":
findpath = 'true'
if findpath == 'true':
findpath == 'false'
distance.SetText(d)
pX = unit.X
pY = unit.Y
listX = []
listY = []
listX.append(pX)
listY.append(pY)
whileloop = 'yes'
while whileloop == 'yes':
p1 = sqrt((((pX+1)- goal.X)**2)+ (((pY+1)-goal.Y)**2))
p2 = sqrt((((pX+1)- goal.X)**2)+ (((pY)-goal.Y)**2))
p3 = sqrt((((pX+1)- goal.X)**2)+ (((pY-1)-goal.Y)**2))
p4 = sqrt((((pX)- goal.X)**2)+ (((pY-1)-goal.Y)**2))
p5 = sqrt((((pX-1)- goal.X)**2)+ (((pY-1)-goal.Y)**2))
p6 = sqrt((((pX-1)- goal.X)**2)+ (((pY)-goal.Y)**2))
p7 = sqrt((((pX-1)- goal.X)**2)+ (((pY+1)-goal.Y)**2))
p8 = sqrt((((pX)- goal.X)**2)+ (((pY+1)-goal.Y)**2))
choice = min(p1,p2,p3,p4,p5,p6,p7,p8)
n = 0
if n == 0:
if p1 == choice:
choice = [pX+1,pY+1]
n = 1
elif p2 == choice:
choice = [pX+1,pY]
n = 1
elif p3 == choice:
choice = [pX+1,pY-1]
n = 1
elif p4 == choice:
choice = [pX,pY-1]
n = 1
elif p5 == choice:
choice = [pX-1,pY-1]
n = 1
elif p6 == choice:
choice = [pX-1,pY]
n = 1
elif p7 == choice:
choice = [pX-1,pY+1]
n = 1
elif p8 == choice:
choice = [pX,pY+1]
n = 1
else:
choice = [pX,pY]
n = 1
if n == 1:
listX.append(choice[0])
listY.append(choice[1])
pX = choice[0]
pY = choice[1]
n = 0
if pY == goal.Y and pX == goal.X:
whileloop = 'no'[/code:2buu5dmb]
Thanks for any help.
B
2
G
2
Posts: 48
Reputation: 746

Post » Wed Nov 10, 2010 6:10 am

[quote="RomanCenturion":2wyumo0u]
[code:2wyumo0u]
...
if findpath == 'true':
findpath == 'false'
...[/code:2wyumo0u]
[/quote:2wyumo0u]
That's the only part I could find. You don't assign "false". Correct it to [code:2wyumo0u]findpath = 'false'[/code:2wyumo0u]
Image
B
23
S
8
G
10
Posts: 1,820
Reputation: 8,242

Post » Wed Nov 10, 2010 9:36 pm

Thanks, I was trying to make it so that the script wouldn't be running multiple times at once(I learned this in a different not well known programming language). Hopefully I'll complete it soon.
B
2
G
2
Posts: 48
Reputation: 746

Post » Wed Nov 10, 2010 10:12 pm

Use boolean values: True and False. Not strings disguised as booleans: 'true' and 'false' or 'yes' and 'no'. When you use strings you are doing a string comparison with requires more CPU power than a boolean comparison. Also, if you need to modify your conditions in the future to include logic operators the behaviour will not be what you expect.
B
17
S
6
G
6
Posts: 113
Reputation: 4,161

Post » Wed Nov 10, 2010 10:24 pm

As Trevor mentioned, it would be wise to use boolean operators such as:

[code:1siuge8w]if findpath == True:[/code:1siuge8w]

or

[code:1siuge8w]if findpath == 1:[/code:1siuge8w]
B
3
G
3
Posts: 23
Reputation: 946

Post » Wed Nov 10, 2010 11:15 pm

Thanks for the tips. Also, I am having timing issues. I import time and use time.sleep(1). This causes it to freeze the whole script before it executes and the script never starts. I have time.sleep under a while statement that happens after part of the script is run through. Any reason for this?
B
2
G
2
Posts: 48
Reputation: 746

Post » Thu Nov 11, 2010 2:41 am

When you run a script everything freezes until the script is done. I'd recommend avoiding sleep() because "time.sleep(1)" will cause a one second freeze every time it's used.
B
79
S
24
G
52
Posts: 4,725
Reputation: 39,713

Post » Thu Nov 11, 2010 4:29 am

Okay, thanks about the sleep info R0J0hound.
On another issue, I've been trying to implement various ways to make the sprite move, but none of them have been very successful. Either they don't work at all or they move too quickly(a second to get to their destination). I was wondering what you guys would think I should use. I'm using lists to store the info, but another way would be fine.
B
2
G
2
Posts: 48
Reputation: 746

Post » Thu Nov 11, 2010 5:56 am

I used something similar to the following with the PathMovement behavior. It uses Sprite2 to define the points but lists can be used instead without much trouble.
[url:l6flkhrz]http://dl.dropbox.com/u/5426011/examples2/pyMotionThroughPoints.cap[/url:l6flkhrz]
made in 0.99.96
B
79
S
24
G
52
Posts: 4,725
Reputation: 39,713

Post » Thu Nov 11, 2010 11:55 pm

Thanks for the example, I modified it a little so that the square wouldn't rotate. I'm having a few issues making the motion rotate more than 90 degrees, but I should be able to figure that out. Thanks for your help.
Edit: fixed the issue
Edit2: While it worked in your .cap, I can't replicate it in my .cap. Here's mine: pathfinding.cap.
B
2
G
2
Posts: 48
Reputation: 746

Next

Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 7 guests