JeremyR
I have a question regarding the subject header. Im making a simple 2D scroller game in pygame and find myself confronted with this issue often, to make a variable global or to pass it into a function. I've heard arguments for and against using globals and the best one I like so far is that globals have a purpose so to use them when you need to but dont overuse them. The problem is when to and when not to - that wasnt explained very well at the time.

So say I have a local variable called "lives" inside the main() function, now I need to pass "lives" into a "Jet" object because "Jet" has a update function that decrements "lives" when it dies (so does main() on collisions as collisions is handled by main). 

Now I can achieve what I need if I make "lives" global in which case I dont actually need to pass "lives" to "Jet" at all except declare it global inside Jet.update().
The worry is now though that I have made about 10 other variables global like "score", "kills" etc because of the convenience.

Heres my question, if I therefore pass "lives" to Jet instead of making it global it appears I need to make it self.lives. If I then update self.lives will it update lives inside main() too or do I need to return "lives" to main()? Also what if I need to again pass self.lives to another object that is created inside Jet like Explosion(self.lives), am I just making copies of lives instead of actually pointing to lives inside main()?

What is the best way to do this type of thing in python?
Globals seem to be the easiest, but Im interested in code safety too.
Elizeusz
Hi this is no python but visual scripting in playmaker but maybe this will help You http://www.youtube.com/watch?feature=player_embedded&v=bHxgjTu5crk#! 
Reply 0 0
JeremyR
Ill take  a look thanks.
Reply 0 0
pshipkov
I am not familiar with this authoring environment, but usually i do something like this:

1. I create base class that defines interface between objects (ships, characters, weapons, etc) in the scene (game level, etc). This class will have methods like takeLife, addLife, spawn, die, etc, etc, TRS, etc, etc. Basically all actions and properties common to all objects in the scene.
Here we have base set of properties like life, shield, armor, etc. TRS, etc.

2. Then i create new classes based on Common for each type of elements in the game - ships, props, weapons, effects, environments, etc. Each one of them will now have unique features. For example - ships will have shield, generator(s), engines, slots for different devices/weapons/etc.
Here i implement the controller methods (how this thing will move, path finding, dynamics, etc) and many other features.

Then each object in the scene is basically a class instance of #2. I can add even more unique features (methods) to these guis and define life, shield, armor, types of weapons, per object by setting some property values.

Now depends on how the weapons system is set up this can get really hairy with dynamically instantiating and then deleting "weapon" class instances that "travel" and hit things. But regardless of the implementation each object should be able to access any other object's control properties. So if you hit an enemy ship this simply means to do enemyShipClassInstance.life -= 5 (or whatever amount of damage your weapon inflicts per evaluation step).
That class instance will check its life property and once it goes down to 0 it will call its ::die() method.

Reply 0 0
JeremyR
@Peter, I think this is the way I need to do things though Im not 100% sure on how to implement your workflow. Things are already starting to get complicated because Im starting to think about how I can create 3 jet objects and fly them in formation for example. Do you think Id implement the position update method inside each object or pass the jet objects into another class or method and move them that way? Currently their positions are updated inside themselves but we are talking really basic AI as in pos.x += 1 for example or pos.y = sin(time), now I am looking at more complicated motion.

Reply 0 0
pshipkov
well hard to say how exactly you should approach this without knowing the environment, but the basics apply everywhere. You have to think through and map out everything and then figure out how translate this into proper object model that fits the bill without overcomplicating or oversimplifying things.
Reply 0 0
Reply