So, I'm not sure if this is possible, but I'm in a situation where I need to do such or something similar. I'm designing a multi-level game, where the game goes on to level 2 once the player comes into contact with the door at the end of level 1. Now...without going into a lot of detail about my code (There's just a lot of it, waaaay too much to post here), I want to modify a string called "currentLevelImage" in a module called "common" from another module called "player"

The string "currentLevelImage" basically represents the filename of the level image i want to load. What i essentially want to do is:

for door in objects.doors:
    if player collidesWith(door):
       s= common.currentLevelImage[:1]
       common.currentLevelImage= str(int(s)+1)+ '.png' #DOES NOT WORK AS INTENDED
       level.levelSpawner.changeImage(common.currentLevelImage)

Dont worry about the actual code, its is written in pseudo-code mostly (to shorten it up), but line 4 is what needs to be fixed. Again, there is a string in the module "common" that I want to modify. My basic code is all correct (indentation, importing all correct modules), I just didn't wanna post it all here. Is this actually not possible? If so, how could I do it a different way? Thanks to anyone who can help!

Recommended Answers

All 4 Replies

I suspect you are dealing with a timing issue, where you have modules levelOne and levelTwo that each import common. After that, levelOne changes the value, but levelTwo has already got its own copy, so it doesn't see the change. If this is the problem, then the simplest solution is to add a level of indirection so that you are manipulating common.currentLeval or the like.

Hope this helps.

Thanks for the quick reply!!

Sorry, that isn't the problem though. I literally have different image files called "level 1.png" , "level 2 .png", and such. The string in common, "currentLevelImage" is simply the filename of the level image that is currently being loaded. I'm just trying to change "currenLevelImage" to a different filename (using some basic string manipulation tactics) when player comes in contact with door, so that a different image can be loaded.

That has to work for the module that is doing the changing. Easy enough to print out the current value after you make the change to double check that it worked. But if you import common from two different modules, and module one changes common.currentLevelImage, module two will not see the change because it already has its own copy of the string. You can fix that by indirection: If common has a list, map, set or other container (even with just one item), then the copy that each module has is the container, not the contents, and changed contents is seen by all the modules.

I just can not understand structure of organizing modules. Usually level two would be created on completion of level 1, either in chain fashion or by returning control to the main loop.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.