Help with my Python Code

This forum is currently in read-only mode.
From the Asset Store
5 levels with simple coding Source-code (.c3p) + HTML5 Exported
  • Hi, I was making an event sheet that makes randomly generated rooms. It worked when it was event sheet but I thought It'd be a lot faster to work on using Python. I'm pretty new to using Python in Construct and I just can't see what's wrong with the code.

    Here's the code:

    ---

    import random
    
    if Sprite4.Value('StartCell') == 1:
         Temp = random.randrange(1, 4)
         if Sprite4.Value('Temp') == 1:
              Cell1.SpawnObject(WallUp, 6, 0)
              Cell1.SpawnObject(WallDown, 6, 0)
              Cell1.SpawnObject(WallLeft, 6, 0)
              Cell1.SpawnObject(DoorRight, 6, 0)
              Player.SetPositionToObject(Cell1, 0)
              Sprite4.Cell2 = 1
         if Sprite4.Value('Temp') == 2:
              Cell1.SpawnObject(WallUp, 6, 0)
              Cell1.SpawnObject(WallDown, 6, 0)
              Cell1.SpawnObject(WallLeft, 6, 0)
              Cell1.SpawnObject(DoorRight, 6, 0)
              Player.SetPositionToObject(Cell1, 0)
              Sprite4.Cell2 = 1
         if Sprite4.Value('Temp') == 3:
              Cell1.SpawnObject(WallUp, 6, 0)
              Cell1.SpawnObject(WallDown, 6, 0)
              Cell1.SpawnObject(WallLeft, 6, 0)
              Cell1.SpawnObject(WallRight, 6, 0)
              Player.SetPositionToObject(Cell1, 0)
              Sprite4.Cell5 = 1
         if Sprite4.Value('Temp') == 4:
              Cell1.SpawnObject(WallUp, 6, 0)
              Cell1.SpawnObject(WallDown, 6, 0)
              Cell1.SpawnObject(WallLeft, 6, 0)
              Cell1.SpawnObject(WallRight, 6, 0)
              Player.SetPositionToObject(Cell1, 0)
              Sprite4.Cell5 = 1
    

    ---

    I've set it so that StartCell is always 1.

    Here's a .cap in case the code doesn't make any sense:

    mediafire.com/download/lmxbczb7pyt01ym/VaniaLike.cap

    Also here's a .cap when it was still an event sheet, this is how it's supposed to work:

    mediafire.com/download/6zhelgghg6eqawm/SomeVaniaNew.cap

    Thanks in advance!

  • I think I remember events actually being faster than Python in most cases (as Python is interpreted, while the core of events in CC are compiled from C++), not sure about that though.

  • Events are faster than python. Mainly because of the interpreter warmup and overhead to call actions and expressions from python. The difference only matters if it's done every frame. One time stuff like this is fine and you get the benefit of easy code reuse for other caps.

    Here's a reason why your code isn't working.

    Create and spawn actions take a string for the object name. So instead of:

    Cell1.SpawnObject(WallUp, 6, 0)

    use

    Cell1.SpawnObject('WallUp', 6, 0)

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I didn't know events were faster than python, Thanks! Also thanks for the fix, can't believe I didn't see that.

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