Spawning Active Cameras

I have a camera parented to my enemy player. The camera is accessed when a number key on the keyboard is pressed, which sends a message to an empty that contains various game data. The empty contains the logic to switch to that camera.

My problem is that when I spawn a new enemy player, the empty no longer points to the same camera, so the number key on the keyboard no longer works.

What is the simplest way to reassign the empty to point to the new camera? Do I have to use a property to tell the empty to use the new camera, or is there a simpler way?

I just tried using a property as described in the last post, but it doesn’t want to work.

Here’s the logic I have on the empty:

it can only be done with python. logic bricks arent capable of keeping track of specific objects

I agree with your post, but to clarify on that specific claim:
They can link to specific objects, but the issues occur when you’re spawning things in from other layers.

An inefficient solution to this problem would be to have all of your enemies as separate objects that aren’t spawned in. Using Python is a MUCH better way of fixing it.

Creating every object beforehand isn’t a good option, since that would require a limited number of them.

What if I parented the camera to the spawned objects as soon as they’re spawned? That can be done, right? It would solve the problem. The camera can remain on the active layer and would just need to be positioned properly on the new object, which is always spawned at the same location. What do you think? …Maybe I should just try it! :roll_eyes:

Agreed. I was simply saying it for completeness and for helping understand why referencing objects with logic bricks works sometimes but not others.

You absolutely can… with Python. The challenge you’re trying to solve is referencing the right camera. Once you’re using Python to keep track of your cameras, then it won’t matter how it was spawned in.

That python is sounding more and more tempting to get into…

So I’m looking into python now but getting absolutely no help in trying to figure out a simple thing.

I figured out how to create a new camera and place it where I want it.

bpy.ops.object.camera_add(view_align=True, enter_editmode=False, location=(-321.496, 358.864, 7.53326), rotation=(1.5708, -0, -1.5708))

I even have the line of code to parent an object to another one.

bpy.ops.object.parent_set(type=‘OBJECT’, keep_transform=True)

What I don’t know is how to select the object to parent it to. It is named “Enemy.000”.

Can you help?

your code(bpy) only works in uppbge 3.0

here an example:


#always ->python module mode ->scriptname.move_camera
#script in text editor needs to end with .py
#in this case run the script from any object you like

def move_camera(cont):
    
    own =  cont.owner
    
    # grab one enemy by name
    enemy = own.scene.objects['enemy_name_here']
    #grab the active camera or use the same line as enemy, but with the camera name
    camera = own.scene.active_camera
    #set camera worldposition
    camera.worldPosition = enemy.worldPosition.copy()
    #add an offset so camera is not inside the enemy
    camera.worldPosition.z += 1.5
    camera.worldPosition.y -= 1.5
    #parent the camera to enemy
    camera.setParent(enemy)

next time you can use google, don’t say it can’t because it can, i learned it that way to years ago.

blender game engine python… then add what y you want to learn

blender game engine python…set parent
blender game engine python…move objects
blender game engine python…grab property
etc

just ask google the right question and tons of tutorials will show up.

That doesn’t mean they are all what I’m looking for. Most are anything but. It takes longer to wade through 100s of pages, reading/watching videos hoping to find something that fits, than to just ask here or somewhere else where such topics are supposed to be supported.

Google is notorious these days for playing an information controller. Fortunately it won’t be that way much longer.

Thank you for the code, btw.

Knowing the steps involved is very helpful. It means I don’t have to come back and ask again and again. Nobody seems to want to assist much around here. Just lecture.

Attention:

I have no desire to have other people do my project. Insinuating that I’m a cheater or thief is very insulting to me. I suggest people don’t answer questions seeking help at all if they have that attitude.

If I were doing it, I’d keep your original setup with the enemy and the parented camera. I’d put a script on the enemy’s camera object that assigns cont.owner to a bge.logic variable.

Then, when you push the button to view the enemy, you set that bge.logic variable as the target of a ‘set camera’ actuator, then activate the actutator.

I was meaning to grab you an example last weekend but totally forgot about it. I shall try to remember this week!

I don’t know about using python any more. I can’t get any code to work and I can’t get adequate help. I just want to move forward with my project, not stop to learn a new language. I’ve wasted three days getting drawn further and further away from my intended goal following the advice I’ve been getting.

So I’m ignoring all that and going back to square one with the logic bricks.

Here’s what I’ve got:

My enemy set-up is an object (“Enemy”) with camera (“Camera”) parented.

There is another object (“Enemy.001”) on another layer with it’s own parented camera (“Camera.001”) used for spawning new enemies through an empty (“Enemy Spawner”).

I have keyboard keys set up through another empty (“Game Data”) to switch camera views, with one of the keys set to the camera on “Enemy”. I want to use this same key on “Camera.001” after it’s spawned.

Using logic bricks, I currently have this set-up:

  • Player hits enemy with a cannonball.
  • Enemy keeps count of cannonball hits through a collision sensor.
  • After 3 hits, enemy dies, sending a message to “Game Data” to spawn a new enemy (“Enemy.001”).
  • “Game Data” sends a message to “Enemy Spawner” to spawn the new enemy.

This is where I start having trouble. Because “Enemy.001” has “Camera.001” parented to it and not “Camera”, the logic breaks down and the key for the enemy camera no longer works.

I don’t know enough about logic bricks to know how to get some order to them so that I can get the keyboard key for the enemy camera to Set Camera for “Camera.001” instead of “Camera” immediately after “Enemy.001” is spawned.

Now that I have all that python ‘advice’ shoved out of the way, I can think a little more clearly again.

I know that a boolean property could be used to keep track of whether the original enemy is alive or dead, and use that property to control whether the keyboard key Sets Camera for “Camera” or for “Camera.001”.

I put a property called “new enemy” on “Game Data” and pass it a message from “Enemy” when “Enemy” dies, and I’ve changed “Game Data” to check the property when the keyboard key is hit to switch to the enemy camera. The keyboard sensor points to “Camera” if it’s FALSE and “Camera.001” if it’s TRUE.

By my reasoning, this should work, but it doesn’t. “Camera.001” is no longer attached to “Enemy.001” when it spawns.

So I’m back to where I originally was.

When you spawn in an enemy, it’s a new object. The logic brick is referencing the original camera, which is on the enemy in the hidden layer. It ‘feels’ like it’s not attached, because your hidden enemy isn’t moving.

You’re trying to change to a camera that didn’t exist prior to the game starting… which you can’t do via the interface. You need to change the ‘set camera’ actuator to point at the new camera while the game is running.

I’m sorry, but I don’t see a way to solve this properly without using Python. The only other solution that I’m aware of is to not spawn new enemies in, so your camera actuator can always point to the camera that is actually in use.

I’ll make a basic example with Python that you can learn from and copy-paste into your game if desired.

Not true. I can still access the new camera via the keyboard key after it’s spawned on the current layer, but it remains where it was generated while the enemy moves away from it.

Here’s a demo video to show what I mean. I switch between the player camera and the enemy camera before and after a new enemy is spawned.

demo

can you give us the .blend to examine? you can send me a PM if you want.

Maybe there is a way to do it with logic bricks only. its just that after python we tend to forget about visual scripting.

I don’t mind some python if it does the job. I just don’t know how to do it.

Here’s the file: Tank Game (2.79) 3.blend

yes it is. you are accessing the original camera on the hidden layer(Camera.001) not the spawned one.
because blender actually never adds the original object into the current layer, it adds a copy that will have another name on the run and you can not reference to that new added obj with logic bricks.

1 Like

ppqojj.blend (2.7 MB)

Done.

I added a script onto the enemy cameras to set the camera object as a global variable.
I also added a script to the ‘4’ key which assigns the enemy camera to the ‘set camera’ actuator, then activates it.

I don’t understand why you’ve got two copies of the same tank, so I just put the script on both of them. Also, your enemy tank is trying to add new overlay scenes every time it gets added, which is failing because they already exist. It’s not doing any harm right now, but you should probably move that logic to your game control empty.

1 Like