python script only runs once/ other issues

This forum is currently in read-only mode.
From the Asset Store
Full game Construct 2 and Construct 3 to post on Google Play
  • 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.

    #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.
  • > ...
    if findpath == 'true':
    	findpath == 'false'
    	...[/code:2wyumo0u]
    
    

    That's the only part I could find. You don't assign "false". Correct it to

    findpath = 'false'[/code:2wyumo0u]
  • 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.

  • 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.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • As Trevor mentioned, it would be wise to use boolean operators such as:

    if findpath == True:[/code:1siuge8w]
    
    or
    
    [code:1siuge8w]if findpath == 1:[/code:1siuge8w]
  • 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?

  • 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.

  • 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.

  • 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.

    http://dl.dropbox.com/u/5426011/examples2/pyMotionThroughPoints.cap

    made in 0.99.96

  • 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.

  • Move lines 3 and 71 into the start of layout block and it should work.

  • Thank you! After a few tweaks I got it to work perfectly. Now all I have to do is set up a way to get obstacles and optimize it.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)