Like I got these collectibles of same kind.
Them disappear when touched, so that Ok.
Now if I set property called count
and want to do
count++;
or
count = count + 1;
each time I get collectible in the game, but how ?
I tried and seems it was like private variable and
not public variable as in programming terms.
Have a global object with a “count” property.
Whenever a pickup object “disappears” send a message to increase the count.
When the global object gets the message, increment the pickup count.
Or he could checkout the asset I spent time on, that uses 1 system to handle all pickups
Thanks BluePrintRandom, that’s nice.
Lucrecious thanks too,
I kinda wish I knew how in BGE declare
something as (global or local) or
(public and private.)
own[‘property’] = an object property persistent until set again
There is also the global. Dictionary that some people use, but it has less meaning when you know how to use bge.logic.getSceneList()
The only way to run a python script is through an object. However, you could use an external python script to define global variables and import it when you need it.
For example,
# globals.py
class Globals:
COUNT = 0
# item.py
from globals import Globals
def die():
Globals.COUNT += 1
endObject() # some function to kill the item object
The thing about this is that the COUNT should transcends scene change, which is neat.
Still, I’d recommend using the provided global dictionary, it’s a lot safer.
Lastly, you should look up the scope and encapsulation documentation for Python, if you want to learn how global/local or private/public work (or if those concepts exist in Python).
a class just for variables? wow. better to use:
global COUNT
COUNT = 0
print(COUNT)
## OR ##
bge.logic.POWERUPS = {"ITEM1":{"COUNT":0}, "ITEM2":{"COUNT":0}}
print(bge.logic.POWERUPS["ITEM1"]["COUNT"])
## OR ## Preferred
bge.logic.globalDict["POWERUPS"] = {"ITEM1":{"COUNT":0}, "ITEM2":{"COUNT":0}}
print(bge.logic.globalDict["POWERUPS"]["ITEM1"]["COUNT"])
The class was just an example, and it could help a lot with organization.
For example,
# globals.py
class World:
LevelCount = 10
CurrentLevel = "Level 1"
class Physics:
Gravity = 9.8
TerminalFallSpeed = 10
def impulse(speed):
return force
then in other files:
# player.py
from globals import Physics
if player.inAir():
player.applyForce((0, 0, -Physics.Gravity))
Again, I still would suggest to use the global dictionary bge.logic provides to do this type of stuff.
Just be careful because the global dictionary provided is persistent and can grow indefinitely. If you’re constantly adding stuff to it, you might hit a memory error.
Edit: Also, in languages like C# or Java, it’s common practice to put globals or constants in a class (since there are no other ways to define “global” variables). In C++, the only reason you use globals is for speed (less assembly instructions), which is negligible in most cases.