Python String Problem

I’m new at Python, so please let me keep up :wink:
I’m trying to swap the camera via python so I have a single key press to swap between several cameras. My problem is when it comes to calling a string set to the object.

import GameLogic
cont = GameLogic.getCurrentController()
space = cont.getSensor("camkey")
actuator1 = cont.getActuator("act1")
actuator0 = cont.getActuator("act3")
setact001 = cont.getActuator("act2")
setact = cont.getActuator("act")
cam001 = cont.getOwner()

if camswap.getValue(fix):
    setCamera(cam001)

if space.isPositive():
    actuator1.setValue(fix)

So when space is pressed the “camswap” string receives the “fix” value it changes to cam001.

What am I doing wrong?

Looks like a lot of things to me:

Where exactly is “camswap” declared? Also what about fix, you have to declare that too before you use it.

Actually, I couldn’t find any python methods for the camera actuator. Where are you getting your code?

Anyway I wrote some python before for another user that does something similar. Instead of setting camera it just switches between actuators set to different targets, but that should make no difference to you, because it would just be a matter of setting an anchor object where you want the camera to switch to.

Here, this is the example I wrote for stu, I think it could help you out too:

Also, you may want to check out my python tutorial:

http://blenderartists.org/forum/showthread.php?t=80826

I’m told it gets people started, so maybe you can use it as well.

Sorry, I should have explained more thoroughly. Camswap’s declared as a object property string in the logic block section. Part of my problem is I can’t find the code for declaring it in the script. The ‘fix’ (And ‘ipo’ for the second camera) are then values set to Camswap, then I have Act and Act2 set the camera using Scene>Set camera (Depending on the value of the string). setCamera’s from the Scene module (http://www.blender.org/documentation/pydoc_gameengine/PyDoc-Gameengine-2.34/KX_SceneActuator.KX_SceneActuator-class.html, I actually got the link by going through your Python tutorial (Helped me alot by the way, thanks :slight_smile: ). It looks like your correction could be what I’m after, I’m trying to integrate it into my program now.

Nope, your script’s set for changing Camera tracking, my aim’s to change the active Camera. Although I thought the value sets would help, I’m not getting any errors, but nor is it having the effect I need, it just changes from the ‘ipo’ camera to the ‘fix’ camera without changing back upon request.

import GameLogic
cont = GameLogic.getCurrentController()
space = cont.getSensor("camkey")
actuator1 = cont.getActuator("act1")
actuator0 = cont.getActuator("act3")
own = cont.getOwner()

if space.isPositive():
    own.camswap += 1
    
if own.camswap > 0:
    own.camswap = 1
    
if own.camswap == 1:
    GameLogic.addActiveActuator(actuator1, True)
else:
    GameLogic.addActiveActuator(actuator0, False)


I’ve changed the string to an int and assigned the correct values in my sensors and actuators. So far I can see this script should be working, but for the fact it isn’t.
Can anybody play spot the mistakes? ^^

See Nigholith, when you force me to guess, it’s a little difficult to get you what you want.

What I meant by using the camera tracking switch script is to just place the objects to be tracked where you want the camera to be. So when someone switches the tracking, the camera goes to that location.

Well anyway, now that I know whats what:


# space-------->script----------->  cam
#                                |_ cam1
#                                |_ cam2
#
# Have a prop named "counter"

cont = GameLogic.getCurrentController() # No need to import GameLogic
own = cont.getOwner()

# Sensor
space = cont.getSensor("space")

# Actuator
cam = cont.getActuator("cam")
cam1 = cont.getActuator("cam1")
cam2 = cont.getActuator("cam2")

# Process:

start_act = GameLogic.addActiveActuator

if space.isPositive():
    own.counter += 1  

if own.counter > 3:
    own.counter = 1
    
if own.counter == 1:
    start_act(cam, 1)
if own.counter == 2:
    start_act(cam1, 1)
if own.counter == 3:
    start_act(cam2, 1)

Strangely enough there seem to be some bugs with “.setCamera()”, that’s why I used a different actuator for each camera.

If anyone more experienced can give me some info about that I would appreciate it.

Couldn’t you just do it with logic bricks? with a keyboard sensor set to ‘space’, an ‘and’ controller, and a scene ‘set camera’ actuator?

Well don’t forget a few prop bricks for the counter, because he wants to do all of this with one key.

Using python for me is still easier, less bricks to worry about. Actually, if “setCamera()” code worked right, you would only need 3 bricks for the whole thing, but as I said it doesnt, so you have to use an actuator for each.

I made my request clear at the start, although granted not in much detail :stuck_out_tongue: Yup, that script works with it, slapping myself for making such simple mistakes. Thanks muchly for your help Social :slight_smile:

1 Like

You’re a pretty cool dude, it’s an honor to help you out.

PS: I get the feeling you’re doing this for the Bullet competition.

Hehe, sanks you :slight_smile: It’s a privilege to have been helped out by the very same guy who introduced me to BGE Python :smiley:

Not at all, to be honest I don’t think I have the experience to be in with a shot, this is for a freeware release project I’ve been working on the past few months. I was quite surprised you turned it down, though I guess you have your reasons.

My primary reason is simply that it’s not a game competition anymore. It wasn’t designed to benefit the BGE (what this whole forum is about) as much as it should.

I liked the previous bullet competition (to an extent), where the goal was to actually make games, and not just to pimp out a physics engine. The only thing I had a problem with in that particular competition was it’s name. I think that by all rights it should have been named the “BGE competition”, because that’s what it actually was.

Anyway, that competition created some of the best BGE content to date. Also, the .blends provided great resources about important things like game logic, level design and overall game development with the BGE (of which the bullet engine is a part of, respectivelly).

I don’t see how this current “bullet competition” (well at least now the name fits) can make back even half of the contributions that sprouted from the previous one. I mean this one is clearly just a pimp push for bullet, and imo that’s basically marginalizing what the BGE is really capable of. (which is making actual games, and not just physics simulations).

I can certainly understand it from that point of view, though it’s up to Erwin if he wants to span out from BGE, ultimately his work on Bullet’s earned him that.

Though I do agree with you, there needs to be a major BGE specific competition to innervate and generate BGE Games. Maybe one of the central BGE figures needs to get in contact with Sony/Nintendo/Microsoft (Or ideally just Sony/Nintendo^^) and see bout getting a sponsored Prize for BGE’s own contest.