copy logic to selected state

Did i ask this question before?Is it possible to copy logic bricks to the selected state.By selected state i mean like state1,state2,state3.

I don’t think so but it sure sounds like a good topic for a new add on I could make.

Change the state number of the according controllers. - This is not a copy but a move.

There is no direct copy. Controllers do not need many parameters, so it is pretty simple to just add new controllers.

I think monster you are not getting what i am posting about and nicholas_A does.So i will let him try to explain it to you.

be nice to monster, now. from what i can tell, he answered your question. there is no copy, a small price to pay to use logic bricks.

the tricky thing about logic bricks is the center column is exclusive to the state. the left and right (sensors and actuators) can be shared. take a look at my aircraft physics logic brick setup for a good example. REMEMBER TO NAME YOUR BRICKS!

under the “Object” menu in the 3d view, there is a “Game” menu, in which you can copy brick setups from one object to another.

Lostscience, add one sensor connected a controller and actuator on state one. Add +1 on the name of sensor and actuator. Run this code.This is what your wanting? It works for one line ,but you start adding anything more and it does weird things or crashes blender. Be a good start for a addon though.

Code:

import bpy

#Copy game logic to different state
act_obj = bpy.context.active_object
states_visible = act_obj.game.states_visible

for d in act_obj.game.controllers:
    con_name = d.name
    con_type = d.type
    con_states = d.states
    con_copy_name = con_name + "_copy"
                
    for i in act_obj.game.sensors:
        sens_name = i.name
        sens_type = i.type
        sens_copy_name = sens_name + "_copy"

        for k in act_obj.game.actuators:
            act_name = k.name
            act_type = k.type
            act_copy_name = act_name + "_copy"
        
            if states_visible[0] == True and con_states == 1:
                plus_state = "+" + "1"    
                            
                if sens_name.find(plus_state) != -1:
                   bpy.ops.logic.sensor_add(type = sens_type, name= sens_copy_name)
                   sensor_copy = act_obj.game.sensors[sens_copy_name]

                if act_name.find(plus_state) != -1:
                   bpy.ops.logic.actuator_add(type = act_type, name= act_copy_name)
                   actuator_copy = act_obj.game.actuators[act_copy_name]       
        
                   bpy.ops.logic.controller_add(type = con_type, name= con_copy_name)
                   controller_copy = act_obj.game.controllers[con_copy_name]
                   sensor_copy.link(controller_copy)
                   actuator_copy.link(controller_copy)
                   controller_copy.states = 2
                                               
                

I would not copy the sensors and actuators as they can be shared within states. Copying the controllers should be sufficient enough. If that is really an option, an configurable parameter might be a good way to let the user tell it’s wishes.

I do not understand the cubic iteration. It should be sufficient enough to iterate over all sensors once and over all actuators once too.

I suggest to deal with sensors/actuators that are connected to any of the controllers of the “input state”.

(using meaningful variable names would help to understand the code quite a lot ;)).

As monster said, no there is no way to do that.

But there is a work around for it, you can copy bricks from 1 object to another.
so setup a cube with all bricks you need/want to copy.

now select the cube, shift select the object that needs the bricks then in 3d window down left corner hit ‘Object’ then go to game -> copy bricks.

now select the object with states, and put the controller to an other state, all bricks go with it. rinse repeat.

is this effective? no, do you want to copy bricks? no, you should as monster said reuse the bricks that you are already using.

#edit
if you get bored to connect wires, you dont have to put a controller first, connect the wire fron the sensor to the actuator and it places an and controller connected with wires, to it.

Here is a slightly modified Blender script. It copies all controllers and you need to manually set the target state at the script. (I know it would be better to add a little GUI to enter that parameter).


import bpy


# Copies all controllers of the current state to target state
targetState = 2 
object = bpy.context.active_object


sensorsByController = {}
for sensor in object.game.sensors:
    for controller in sensor.controllers:
        sensors = sensorsByController.get(controller, [])
        sensorsByController[controller] = sensors
        sensors.append(sensor)


for controller in list(object.game.controllers):    
    bpy.ops.logic.controller_add(type = controller.type)
    newController = object.game.controllers[-1]
    
    for sensor in sensorsByController[controller]:
        newController.link(sensor=sensor)
    
    for actuator in controller.actuators:
        newController.link(actuator=actuator)
        
    newController.states = targetState  
        

But how would i run the script?

I don’t see why one would ever need to copy entire sets of logic bricks between states; it’s horrible practice.
I understand reusing sensors and actuators, but if two different states have the same set of logic bricks connected in the same way, then you’re clearly doing something wrong.

If you need a specific set of logic to be ran on two different states (say A and B), then put that set of logic in a separate third state (say C). Whenever you switch to state A or B, make sure to add in C, so they both run together. Whenever you switch out of A or B, make sure to remove C. No need to copy entire sets of logic bricks.

What if you wanted to track another npc that the ray sensor detected.And it contained the same logic brick setup but a different target.Do you see now that it would be usefull?

put it into a textblock.

Select the object
Show the state you want to copy
In the script set the state you want to set (e.g. 3)
press run script

–> show the target state to see the new controllers

While this is a use case it is “code replication” as well. The logic of the state differs in a single thing … the target. You can encapsulate the shared logic/configuration and differ it by a single parameter.

Unfortunately there is no easy way to implement such an abstraction. So I suggest to have one state, but you configure the target object (before or latest when you switch to this state). Due to its dynamic nature you need to do that with Python.

e.g.:


actuator = ...
actuator.target = <whatever object you want as target>

This turns the “go to A” state and the “go to B” state into a single “go to …” state. The … can be set earlier.

There is an alternative implementation: Use always the same target to go to -> just one configuration. To go to different targets (A, B) place the target at the desired location before or while switching to the “go to” state. A separate (and maybe invisible) object can be very helpful.

OK, this could almost be a addon, I think I got it pretty close to working.

code:

import bpy

#Copy game logic to different state
act_obj = bpy.context.active_object
states_visible = act_obj.game.states_visible


#The state to move the copied controller to
state_to_go = 5

#The state your own
active_state = 2

#Search for + in sensor and actuator names to link to copied controller
plus_state = "+"


#Logic Controllers
for k in act_obj.game.controllers:
    con_name = k.name
    con_type = k.type
    con_states = k.states
    con_copy_name = con_name + "+" + "%s" % state_to_go


#If states visible == to the controller state, copy controller            
if states_visible[active_state -1] == True and con_states == active_state:
      
    #Copy Controller                                                            
    bpy.ops.logic.controller_add(type = con_type, name= con_copy_name)
    controller_copy = act_obj.game.controllers[con_copy_name]

    #Move copied controller to selected state
    controller_copy.states = state_to_go


#Logic Sensors
for i in act_obj.game.sensors:  
    sens_name = i.name
    sens_type = i.type
    sensors = act_obj.game.sensors[sens_name]

    #Sensors that contain a + , link to the copied controller
    if plus_state in sens_name:
        sensors.link(controller_copy)


#Logic Actuators
for i in act_obj.game.actuators:  
    act_name = i.name
    act_type = i.type
    actuators = act_obj.game.actuators[act_name]

    #Actuators that contain a + , link to the copied controller
    if plus_state in act_name:
        actuators.link(controller_copy)            

Looks like I’m a little slow,LOL. Looks like Monster got something to work before me. The only problem with yours Monster is it copies the entire setup from previous state.

That is true the only problem there is with that.It remembers the logic bricks that i previously copied and then adds those.

How do you use your script AFWS?Then i will test it.

No, it still isn’t useful. A “different target” is a different logic brick setup that is functionally different. The logic bricks do something completely different; tracking to a different npc. In this case, if you’re going pure logic bricks, you would need to create an entirely new controller and an entirely new tracking actuator regardless.

Also, with your given example and using Blender’s current vanilla logic bricks, you will never be able to have the flexibility and maintainability as you would if you wrote that same logic with like 10 lines of code.


ray = # get sensor
track_to = # get actuator

if ray.positive:
     object = ray.object
     track_to.object = object
     track_to.activate()
else:
     track_to.deactivate()

I know that’s not the exact python syntax for Blender, but it’s something pretty close to that.

If you have actuators dependent on specific sensor input, I’d highly recommend learning python because as of now, there is no good way of doing that type of logic with logic bricks.

Mine might need a little more work,LOL. I did a few test runs and it seemed to work. Did a few test later and it gave a error. Tried it again and it seemed to work. Anyways It’s similar to Monsters. Mine you have to define at the top what state your on and what state you want to go to. If you have bricks on state 1 and state 2 ,but you want to copy state 2 to state 3. You have to add a + at the end of the sensors and actuators on state 2.