Re: I need help, again!

This has to deal with name spacing, variables made in a function live and die in that function unless passed to an outside source, so global or return functions are usually required. With return functions its more about deciding under what conditions you want to return and to perform the operations you want before hand, it might be a bit tricky in some cases, but you can always get results.

There is another way to go about this though, and thats with classes. In a class, a variable can be treated as a global internally to that class, and it can have its own internal functions for manipulating that data, which can make them very useful. For example:

class example(object):
 def __init__(self):
  self.index = 0
 def addtoindex(self):
  self.index += 1
 def addto(self,data):
  data += 1

box = example()
box.addtoindex()
box.addto(box.index)

This class treats index as a global within itself and has two functions, addtoindex() and addto(). Addtoindex() does a simple operation on self.index and those changes are retained because self.index exists outside that function, but with addto() you'll notice we don't use the "self" prefix anywhere, so the variable data is created and dies within that function since its not being returned anywhere. You can also access the index variable externally with the name of the class and the name of the variable, like the variable we pass to addto() on the last line of the example. So, going with your resource list idea, here's an example of what that might look like:

class example(object):
 def __init__(self):
  self.index = 0
  self.resource = ['wood','rock','iron','grain']
 def addtoindex(self):
  self.index += 1
 def inventory(self):
  print(self.resource[self.index])

box = example()
box.addtoindex()
box.inventory()
-- 
Audiogames-reflector mailing list
Audiogames-reflector@sabahattin-gucukoglu.com
https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
  • ... AudioGames . net Forum — Developers room : magurp244 via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : stewie via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : magurp244 via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : magurp244 via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : magurp244 via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : magurp244 via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector

Reply via email to