I need to do a game for my computer class in school entirely in python, only using Logic Bricks where absolutely necessary.
I’m doing the standard FPS, and there have been several times where I need a script to communcate across layers, between scenes, and have more than two users.
One example is the simple bullet - I’ve got it on the second layer, the game itself on the first layer. I’ve got the simple script to tell the bullet to end when it collides with anything. However, none of the scripts for the bullet work, and I assume it’s because they’re being added from a different layer.
Another example is the health gauge. I’ve got the health system itself on the character one scene, and I’m trying to link the same health script to the HUD on an overlay scene. But it doesn’t seem to want to communicate with the other scene, probably because a script can only have one user and can’t communicate via scenes?
Anyway, my point is, is there something I’m doing wrong, or is there some script which will allow me to make multi-user and inter-scene etc. scripts? Thanks for reading… and for any help.
To communicate to other active scenes, you have to use messages. You can’t use the basic copy property actuator (though I swear you used to be able to) so you have to send the property you want through the message and have your other scene receive it and get the message body via python.
You can take a look at my seamless scene change script to see how to send messages if you’d like.
Or you could try to assign a global variable
import GameLogic as gl #This way we won't have to type as much
cont = gl.getCurrentController() #If you need this explained, you're in trouble =/
own = cont.getOwner()
gl.prop = own.prop #Now the GameLogic module has a "prop" attribute with the value of the owners "prop" property
Then to retrieve:
import GameLogic as gl
cont = gl.getCurrentController()
own = cont.getOwner()
own.prop = gl.prop #Now the owner's "prop" property is equal to the GameLogic's "prop" attribute
At least, I think this should work. I haven’t tested it though. :o
As Moguri demonstrated above, attributes that you assign to GameLogic are global. This means, regardless of scene, layer, or anything, “GameLogic.blah” will always be what you set it to be.
So, for the health for example, in your “game” scene, set “GameLogic.HP” to be the current health. The healthbar on the HUD scene, can easily retrieve it by accessing it the very same way: “GameLogic.HP”.
Hope that helps,