# Crowd - Refresh Offset_Location of an Agent

Hi All,

First of all, please forgive my english… -__-

So, I’m trying to code a sample of a crowd simulation.
I want my agents follow one leader. Thats works in the scene but the problem start in the BGE.

I explain you with some screenshots :
Here it is the logic view of my leader. Then you see there is an Always sensor where i run a script “moveAgents”
In this scripts I do some things but the most important (and where is the problem) is that I change the offset_location of each agent.

``````
#Move all agents
def move_all_agents():
'''
Function    :
__init__()
Summary     :
Arguments   :
'''

# bpy.ops.object.select_name(name=own.name)
# cube = bpy.context.selected_objects
GetAgents()
for a in agents:
velocity = [0,0,0]
position = [0,0,0]
v1 = rule1(a)
v2 = rule2(a)
v3 = [0, 0, 0]	#rule3(a)
v4 = [0, 0, 0]	#v4 = self.bound_position(a)

velocity = v1 + v2 + v3 + v4 + v5
velocity = v1 + v2 + v3 + v4 + v5
velocity = 0 #a.getVelZ() + v1 + v2 + v3 + v4 + v5

a.location = a.location + velocity
a.location = a.location + velocity
a.location = a.location + velocity
move = a.game.actuators["move"]
<b>move.offset_location.x = velocity
move.offset_location.y = velocity
move.offset_location.z = velocity</b>
#end for
#end move_all_agents

``````

Here I show you the logic view of an agent : So finally, the offset_location (of each agents) is changed first time I run BGE (on press “P”) but their are not refresh, only if i stop BGE and run another time.

I hope you will understand and I thank you for the time you will spend on it.

triguN.

Well, thank you for your answer Monster.
I also correct the little things like 2 same sensors = 1.

But just to go back to the problem, there is no way to refresh the offset_location during the game ?

There is no offset_location at the ObjectActuator (which is labeled Motion Actuator in Blender). See http://www.blender.org/documentation/blender_python_api_2_63_5/bge.types.html#bge.types.KX_ObjectActuator.

Thanks again Monster.

I understand better. So now I need to get each controllers of my agents to change their KX_ObjectActuator.dLoc propertie. Then I hope thats will work.

To do that I search in the forum how to register controllers of all my objects in a list.
I found that code :

``````
import GameLogic

scene = GameLogic.getCurrentScene()
objList = scene.getObjectList()

try:
GameLogic.DictObjects
init=1
except:
init=0
if init:
cont = GameLogic.getCurrentController()
own = cont.getOwner()
name = own.getName()
if not GameLogic.DictObjects.has_key(name):
GameLogic.DictObjects[name]=cont
own.init=0

``````

First thing, I changed “scene.getObjectList()” by “scene.objects”. The function getObjectList() seems not exists anymore.

But GameLogic.DictObjects also don’t exists. Any Ideas ?
If I succeed to create my list like that, I would be able to change my “move_all_agents()” code like that :

SEE EDIT…

Do you think that will works ?

EDIT : I found another way to do this. I use bge.logic.globalDict

Then this script add to this globalList each agent I want to work with :

``````
import GameLogic
import bge

try:
init=1
except AttributError:
init=0
if init:
cont = GameLogic.getCurrentController()
own = cont.owner
name = own.name
bge.logic.globalDict[name] = cont
init=0

``````

Then in my script “moveAgents” I do this :

``````
#Move all agents
def move_all_agents():
GetAgents()
for a in agents:
velocity = [0,0,0]
position = [0,0,0]
v1 = rule1(a)
v2 = rule2(a)
v3 = [0, 0, 0]	#rule3(a)
v4 = [0, 0, 0]	#v4 = self.bound_position(a)

velocity = v1 + v2 + v3 + v4 + v5
velocity = v1 + v2 + v3 + v4 + v5
velocity = 0 #a.getVelZ() + v1 + v2 + v3 + v4 + v5

if len(bge.logic.globalDict) != 0:
# get the controller
agentController =  bge.logic.globalDict[a.name]
# get the object actuator attached to the controller named act
contrActuatorMotion = agentController.actuators["move"]
# set amount to be moved
contrActuatorMotion.dLoc = [ velocity, velocity, velocity]
#end for
#end move_all_agents

``````

But that don’t works… each agent take the first velocity I give, and then they go far far away… dLoc propertie isn’t refreshed

Hi Monster,

Big thanks for your contribution !

Juste before change my code and adapt it with your script I would like to show you what I’ve done until here.

So I attach a blend file where you will find 4 script Init, RegisterObject, moveAgents, cubeMove. Well you juste have to run Init.py script and then on the TOOLS panel you will find Crow Config section. Then click on Generate and press P to start the game.

With arrow’s keys move the leader. But you will see that agents go far in their own direction.

While that I modify script with yours.

Thank you.

triguN

### Attachments

crowdSimulationFinalBGE.blend (425 KB)