Jump to content
  • Advertisement
Sign in to follow this  
Sheldar

Help with Modules ;[

This topic is 2703 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So among some other minor snags in the game I'm at the point to where I want to start to split my game up into different files. So when I did this I thought that I could return a value from one of my modules back to my main module and change the value of a variable there. Here's the code:

Main:

...>
import new_game
...>
#These are the menu items.
#EDIT: Thought I'd let you know that 'mpos' is the mouse position. mpos = pygame.mouse.get_pos()
if 260 < mpos[0] > 32 and 402 > mpos[1] < 55:
new_game.new_game(playing)
print ("The game state 'playing' has been set to, ", playing)
#Yeah, my area of "clickability" is also a little glitchy here and if you can help my code here I'd appreciate it.


new_game:

def new_game(playing):
print ("Imported new game...")
playing = True
print ("The game state 'playing' has been set to, ", playing)
return playing


This is what it prints out:
Imported new game...
<"The game state 'playing' has been set to, " True>

It pops out of new_game and back to main then gives this line to me:

<"The game state 'playing' has been set to, "False">

So I know that it isn't returning my function right from the module. I have the same problem when I code in C++, I guess I just wasn't taught right?

Guidance please! Thanks.

-Sheldar

Share this post


Link to post
Share on other sites
Advertisement
This is a common confusion about local variables that happens with many languages. There is also some confusion about how assignment works in Python which is different from the way it works in C++.

You pass in a bool value which is then bound to a local name 'playing'. Local means that identifier only exists within the body of that function. That name comes solely from the argument in the function definition and not from the variable you pass as an argument when you call that function (<--you may want to read that sentence several times if it doesn't sink in right away). The 'playing' inside the function body is not the 'playing' from outside. It only has the same name because you used the same name, and the same value because that's what you passed in.

Next you rebind that reference because that's what assignment does in Python. So basically you discard the value that was passed in without using it and create an entirely new bool.

Finally you return the new value, which is True. Back in main where you called the function, you don't use that return value. But I do understand what you expected and why, I'm just trying to explain what really happens.

This is going to seem trivial but, long story short, you could accomplish what you wanted like this:

[source lang="python"]
...
import new_game
...
if 260 < mpos[0] > 32 and 402 > mpos[1] < 55:
playing = new_game.new_game() # notice how I made use of the return value
print ("The game state 'playing' has been set to, ", playing)


[/source]
[source lang="python"]
def new_game():
print ("Imported new game...")
return True
[/source]

Notice the argument for new_game wasn't needed since it didn't have any impact on the return value.

There is other stuff you can do like declaring globals or modifying mutable arguments, but I think I've written enough for now.

Share this post


Link to post
Share on other sites
I like what you wrote a lot man. I was actually thinking about doing something like this, but I guess it never sunk in. Thanks so much for your advice!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!