0 Favourites

Function created with Python needs check

This forum is currently in read-only mode.
  • I had some problems with spawning objects in the edges of the layer (at random positions). I thought of making an array table that is given an appropriate random variable to allow the object to spawn in the correct position.

    I drew up a flow chart but was way to convoluted for my taste so I just learned some python for about 10 minutes and wrote this instead to server my purpose.

    def edgeplacement(o,l):
         i = random.randint(1,4)
         xx = 0
         yy = 0
         switch i:
              case i==1:
                   xx = 0
                   yy = random.randint(0,768)
              case i==2:
                   xx = 1024
                   yy = random.randint(0,768)
              case i==3:
                   xx = random.randint(0,1024)
                   yy = 0
              case i==4:
                   xx = random.randint(0,1024)
                   yy = 768
         else:
              xx = 0
              xy = 0
         newobject = System.Create(o,l,xx,yy)
         newobject.angle = random.random(360)
         return true
    

    All it does is take in the type of object to spawn and the layout it spawns. There are only 4 possible sides the object can spawn in, since the whole point is to spawn the object from the sides. Each 'case' represents a side. The parameter 'o' is the object. The parameter 'l' is the layer.

    I'm doing this without actually testing it because I'm at a public computer and it doesn't have dx9... well it says it doesn't when I run the exe but I'm pretty sure its just the computer blocking my access.

    So, is there anything wrong with it? Is there a much much much simpler way? I'm a complete novice here at python scripting so please tell me if I got something wrong. I need to learn from others not just from books.

  • * To use random you need to import it prior to using it. It is the best way to import at the beginning of the script:

    import random
    
    def edgeplacement
    ...
    

    * No need for the else-part. You're switching between one of the numbers, that will be generated with randint(1, 4). 'i' will never be set to any other number than 1, 2, 3 or 4.

    * No need to explicitly 'return true'. You may safely delete that line.

    * As far as I know (but I might be wrong), random.random() isn't used as you would like to. It is used without a number in brackets and returns the next random number everytime it is called, which is a number between 0.0 and 1.0

    If I'm right, you better use randint(0, 360) here, or, if you explicitly want decimals, random() * 360

    * You don't reference an object by creating it. You need to access it using SOL (selected objects list). I can't work with CC at the moment, but I think it is something like

    System.Create(o, l, xx, yy)

    newobject = SOL.o

    newobject.angle = random.random() * 360

    If SOL doesn't accept a reference, then you need to create the object by name.

  • There's no switch statement in python, and "Create" works with a the text name of the object.

    This works:

    from random import randint,random
    
    def edgeplacement(o,l):
       # get the name of the object
       objectName= o.__class__.__name__
    
       if randint(0,1) == 1:
          x = randint(0,1) * 640
          y = random() * 480
       else:
          x = random() * 640
          y = randint(0,1) * 480
       System.Create(objectName,l,x,y)
       getattr(SOL, objectName).angle = random()*360
  • There's no switch statement in python

    <img src="smileys/smiley9.gif" border="0" align="middle" />

    and "Create" works with a the text name of the object.

    That's what I was looking for.

    from random import randint,random
    
    def edgeplacement(o,l):
       # get the name of the object
       objectName= o.__class__.__name__
    
       if randint(0,1) == 1:
          x = randint(0,1) * 640
          y = random() * 480
       else:
          x = random() * 640
          y = randint(0,1) * 480
       System.Create(objectName,l,x,y)
       getattr(SOL, objectName).angle = random()*360

    Clean and efficient as always! And again something new to learn. Accessing with __class__ is new as is 'getattr' to me. Thank you for sharing it <img src="smileys/smiley1.gif" border="0" align="middle" />

  • Thanks heaps R0J0hound. Just knowing how to retrieve names and attributes helped a lot. If only there was some more in depth tutorial of the integration of python and CC or at least a descriptive reference table. I might make one once I master the basics.

    I'll digress. I have been wondering how I could make a sprite object appear near this object called 'player'. I wanted to make a proper range limit to it, as in randomly spawn in a circular area around 'player'.

    So here's what I've come up with. Using some basic Pythagoras Theorem to randomly spawn objects in a circular area relative to 'player'.

    from random import random,randomint,choice
    from math import sqrt,pow
    
    def SpawnNearPlayer(o,l,rangeMin,rangeMax):
         objectN = o.__class__.__name__
         playerN = player.__class__.__name__
         
         rads = randomint(rangeMin,rangeMax)     #distance from 'player' object
         xside = randomint(1,rads)*randomint(-1,1)     #randomising x-axis
         yside = sqrt(pow(rads,2) - pow(xside,2))*choice(-1,1)     #randomising y-axis to fit x-axis and distance
         
         #create the object relative to 'player'
         System.Create(objectN,l,
              int(xside)+getattr(SOL,playerN).x,
              int(yside)+getattr(SOL,playerN).y)

    I know there must be a more clean or better way of doing this but with only couple of weeks total experience in programming I'm still a newb.

    Also there is another problem. The function I just wrote will definitely create objects out side the screen too (outside the game boundaries). This may cause some issues and I was wondering is there a way to cut out the area that is outside the screen?

    I mean to say is there some kind of method that takes into consideration the boundaries of the game so my function won't choose a vector there?

    I must learn more but the official python manual is a pain to read through. All I need to know is things that are slightly advanced than beginner's (since I attempted to learn C#).

  • Here's good python reference that I use all the time that is easier use than the official docs.

    http://www.tutorialspoint.com/python/

    Here's my working of the problem.

    from random import random,randint
    from math import sin,cos
    
    def SpawnNearPlayer(objectName, layer, rangeMin, rangeMax):
       # calculate random polar coordinate.
       random_angle = random()*360 
       random_distance = randint(rangeMin,rangeMax)
       
       # calulate x,y from angle and distance from Player.
       x = random_distance * cos(random_angle) + Player.X
       y = random_distance * sin(random_angle) + Player.Y
       
       #check to see if x,y will be on the layout
       if 0 <= x <= System.LayoutWidth and 0 <= y <= System.LayoutHeight:
          #Yes. create the object
          System.Create(objectName, layer, x, y)
       else:
          #No.  Try a different point.
          SpawnNearPlayer(objectName, layer, rangeMin, rangeMax)

    I eliminated the __class__.__name__ bit as it looks too cluttered. You now pass the object's name to begin with instead of the object type.

    So instead of calling the function like this:

    SpawnNearPlayer(Sprite,1, 50,100)

    Do this (notice the quotes):

    SpawnNearPlayer('Sprite',1, 50,100)
  • Yet again I thank you. You're about the best help I got here. If only there was a 'like' button... lol

    And, ah, I see how you get x and y. It's from something in the lines of (x-a)^2+(y-b)^2=r^2 isn't it? Didn't know that 't' in cos or sin * 't' was the angle between the two points relative to the x axis. Thanks again. ^^

  • Are you using python for any particular reason, or just trying to learn?

  • Right now I'm both using python to create various functions for my game and learning at the same time.

    But then I did use GML for almost everything in GM8. So in the end I might use more python scripts than I'll click and drag for almost everything.

  • I wouldn't do so. Construct's event system is very powerful. There are rare occasions where you need to use python or develop your own plug. But almost everything can be done just with events - really :)

    (And events will be executed a lot quicker than python scripts)

  • Oh. Then it's learning purpose :P

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • I just realised another reason. In python and many other programming languages you don't have to predefine variables like you would in CC. In python I can just have variable 'i' within a method without defining the variable 'i' before hand. In construct though you can't do that, now can you?

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