I’ve got a scene currently set up for the main menu, including a Player Select screen which is separate from the main game scene. For the Player Select screen I am trying to have character previews display using the models from the game scene. I have a python code attached to an always sensor on an empty, which should be where the model loads. This is the code I have attached currently.
import bge
from bge import logic
cont = logic.getCurrentController()
sel = cont.owner
scene = logic.getCurrentScene()
objects = scene.objects
player1 = objects['player1select']
p1sel = player1['player1']
sel['p1'] = p1sel
if sel['p1'] == "vilhelm":
scene.addObject("VilleCont", "p1", 0)
The conditional statement is currently set up so that it will grab a specific model based on the player selection (defined by the property ‘p1sel’).
But this is not working for me at all. I cannot seem to figure out how to add an object using python. I also tried just using the logic brick actuator for ‘add object’ and that didn’t seem to work as well. I’ve spent about an hour tinkering and reading around but haven’t really made any progress.
hi Plingativator,
just make sure that the object to be added is in a diferent layer otherwise it wont work,just move it to layer 2 lets say then it will work…
Romy
I think that the addObject function / logic brick only works on inactive objects in the same scene as the calling object. For example, you can’t create a bullet from the game scene in the GUI scene.
the functions is:
ob_in = G.getCurrentScene().objectsInactive (i hope i remembered it right…)
however, “getCurrentScene” pooints to the current scene, wich can cause errors or even Crashs, if there is no scene, or scene is closed etc.
I found a handy funcition to define scenes others than the current one.
example:
GUI= [scene for scene in G.getSceneList() if scene.name == ‘GUI’][0]
Try to add from the defined scene .
GUI.addObject(obj, other, time)
Thanks for your help! I’ve managed to get a bit further now but I’m having an issue trying to end the added object when changing the selecting character, ie. on the player select screen I want the currently selected player to have the model appear, and then be removed again when toggling the selected character.
I’ve been reading about the endObject function but it doesn’t seem to work when I throw it into a conditional statement. Shouldn’t something like this work?
if player1['selection'] == 1:
ville = scene.addObject("VilleCont", "p1", 0)
gabe.endObject()
if player1['selection'] == 2:
gabe = scene.addObject("GabeCont", "p1", 0)
ville.endObject()
No, that won’t work, as in either case, either object never existed if the selection is only one value. For example, if player1[‘selection’] == 1, then gabe is never created. Make sure to trigger that only once. It would be better to create both models once, make them invisible, and then make one visible when the selection is true.
Yes, I’ve got two separate scripts now, one that fires once from an always sensor which adds the objects to the scene. The other always sensor is set to True Pulse mode and will change the visibility of each model depending on the selected character.
A new issue has arisen. I have two players to select and a separate model display for each, so all the models are being added twice, each to a different empty. I can affect the visibility of the first ones that I add, ( this seems to work by using the original names of the objects). However, I cannot seem to adjust the visibility of the second set of models being added for player 2 because I assume they are being given a new name which I am not accessing properly. Is there some kind of default naming convention for objects that have been added multiple times to a scene?
I’ve tinkered with my approach a little bit and found a work-around. I add only one instance of each character, and then set their position based on the selection. I used the two empties for the selected characters and any characters that aren’t selected get thrown way up out of the way, so only two models are ever visible at once. Maybe this isn’t the best way to do it but I think this covers everything I need. Here’s my code.
scene = logic.getCurrentScene()
objects = scene.objects
player1 = objects['player1select']
player2 = objects['player2select']
villebones = objects['VilleBones']
gabebones = objects['GabeBones']
empty1 = objects['p1']
empty2 = objects['p2']
if player1['selection'] == 1:
villebones.position = empty1.worldPosition
if player1['selection'] == 2:
gabebones.position = empty1.worldPosition
if player2['selection'] == 1:
villebones.position = empty2.worldPosition
if player2['selection'] == 2:
gabebones.position = empty2.worldPosition
if player1['selection'] != 1 and player2['selection'] != 1:
villebones.worldPosition = (0, 200, 0)
if player1['selection'] != 2 and player2['selection'] != 2:
gabebones.worldPosition = (0, 200, 0)
Thanks all for your help. These forums are in invaluable tool for Blender.