Beginner Python Question?

Hi All,

I’ve decided to bite the bullet and learn Python. I’m doing okay so far though I’ve run into a little snag.

I’m currently just trying to set variables for a double jump:

jumped = 0

if space.positive:
	if jumped == 0:
		jumped = 1
	elif jumped == 1:
		jumped = 2
	elif jumped == 2:
		if floorTouch.positive:
print jumped

I’m still learning so some of this code may not be great.

When space is pressed it’s setting the ‘jumped’ variable to 1.

My problem is that it’s setting it back to 0 straight away (presumably because it’s looping back through to the ‘jumped = 0’ part?

Any help would be great :slight_smile:

im pretty new to python too but wouldnt you need to use a switch or something?
if space positive would only evaluate true while your pressing space i think, might be other ways to play with triggering modes. you could prob do this using logic bricks but would be good to code i suppose. hope this helps :slight_smile:

Hi, thanks for the reply.

Yeah, I think you’re right. Is there any way I can set the variable globally from inside an If Statement?


p.s. I might not be understanding the usage of variables correctly? Are properties better for this sort of thing?

Okay I got this working using properties. Here’s my code if anyone’s interested:

def jump():
	movespeed[2] = 1000
	owner["jumpDetect"] = owner["jumpDetect"] + 1
#Reset the jump if you touch the ground
if floorTouch.positive:
	owner["jumpDetect"] = 0
#Detect whether jump has been pressed and if it's enabled

if space.positive and owner["jumpDetect"] == 0:
elif space.positive and owner["jumpDetect"] == 1:

You can use the has_key method to create a property and give it a default value. For example:

# check if the property exsists
if owner.has_key("jumped") == False:
# If it doesn't exsist create it and give it a value
owner["jumped"] = 0

The first time the script runs the property jumped won’t exsist so it’ll get created and assigned a default value, in this case 0. Next time the script loops through its self has_key will return True so the propertie’s value won’t be reassigned back to 0.

Hope this is of some use to you!

Hope you got it working, as i know enough to spot errors but im still terrible with python syntax, trying to work on my C++ skills :slight_smile: i would have thought a property would be the way to go though, unless theres a specific command for assigning/switching variables. I might have to use that some time, will be a while though as im working on AI and art for my game.
Just curious (ive already posted a thread about this) but do you know if animated material settings and real time uv properties are working in GE as i think theres a bug.
Happy blending :slight_smile:

Thanks, this is really very useful.

No worries, happy to help.