Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

If-else code help

I need help with the if-else code. I’m trying to make the trap direction change when the character moves to a location


this is level 17 in game dev 1

In this case, you are testing a literal string…it’s value will never change. Instead, you need to test the hero’s position using a variable or method. There are several ways you can do this.

targetPos = hero.pos will return the vector indicating the hero’s current position. It will look something like {x: 80.18, y: 70.82, z: 1.00}.

Or, you can individually obtain the hero’s x and y positions using:
xPos = hero.pos.x
yPos = hero.pos.y

Using a variant of the second method, the if statement might be something like:

if Math.round(hero.pos.x) == 35 and Math.round(hero.pos.y) == 18:
    # Code to change the trap direction
    # other code

i tested the code and it worked, but only when the hero spawned at the position


Instead of using the spawn position, maybe using a box test might be what you have in mind. This is where you test to see if the hero has passed a certain x pos and/or y pos, where x and y define the upper right corner of the box…

if hero.pos.x >= 35 or hero.pos.y >= 36:
    # trap code
    # other code 

In this example, Math.round is not needed, as you are not testing for an exact integer.

Not being sure of what you are attempting…why not just turn the trap on at the start? Or, if you are intending to turn the trap on as soon as the hero moves, you can do this with:

xPos = hero.pos.x
yPos = hero.pos.y # this would be placed before any other code that impacts the hero's position, but after the hero has spawned

if xPos != hero.pos.x or yPos != hero.pos.y:
    # trap
    # other
1 Like

im trying to make the trap change direction when my hero gets to a certain location or collects somthing

find it helpful. your last reply dedrous gave me the answer to my question about my project, going to get some drinks now, lol. just kidding. thanks a lot.
Jamison hopefully you will find what you need.

@Aidd, glad it helped!

@Jamison_Brown, I still have your code from the previous topic, so I adapted it for this scenario. Assuming you are using the X as your trigger, here’s what I have so far:

trap.disabled = False
if player.pos.x >= 35 and player.pos.y <= 18:
    trap.direction = "horizontal"

However, like you are seeing, it does not change the direction. I’ll need to keep playing with it. I seem to recall being able to test for the condition of a goal (game.addMoveGoal, for example), but I think that was in Dev 2. I’ll let you know if I find anything.

Hopefully, someone might chime in too, with other ideas.

1 Like

ok. my game is due today so ill just use something else. however I still want to make a game with the if-else code

EDIT…I’ve refined and improved the code; also added a 3rd gem, so you get a 2nd change in the trap’s direction…well, I figured out a different method…add this to the bottom of your code:

def onCollect(event):
    unit =
    item = event.other
    if trap.direction == "vertical":
        trap.direction = "horizontal"
        trap.direction = "vertical"

player.on("collect", onCollect)

i added the other one and it works