Help with my Python Code

For questions about using Classic.

Post » Tue Sep 17, 2013 4:51 am

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:
---
[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
[/CODE]
---

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

Here's a .cap in case the code doesn't make any sense:
http://www.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:
http://www.mediafire.com/download/6zhelgghg6eqawm/SomeVaniaNew.cap

Thanks in advance!ParanoidDev2013-09-17 04:56:56
B
3
Posts: 2
Reputation: 179

Post » Tue Sep 17, 2013 6:01 am

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.
"Construct 4 lets YOU make advanced games! (but not play them)" Construct Classic - Examples Kit
B
113
S
39
G
17
Posts: 2,183
Reputation: 19,215

Post » Tue Sep 17, 2013 6:40 am

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)
B
91
S
31
G
102
Posts: 5,232
Reputation: 67,250

Post » Tue Sep 17, 2013 9:05 am

I didn't know events were faster than python, Thanks! Also thanks for the fix, can't believe I didn't see that.
B
3
Posts: 2
Reputation: 179


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 2 guests