Hi all,
I have a model of a police in the scene. I want the model to follow the
avatar, if the avatar is in some vicinity, as soon as some event happens
(or after a particular time). I want the script to run on the server. I
have written the following script for this:
var entityNameToFind = "PoliceWalk.";
var animationNameToStart = "Walk";
var environmentEntity = "Environment";
var availableAnims = 0;
var delta = -0.05;
var enObject = scene.GetEntityByName(environmentEntity);
var objectToAnimate = scene.GetEntityByName(entityNameToFind);
var avatarEntity;
var myPos = me.placeable.transform;
var terrainEntity =
scene.GetEntity(scene.GetEntityIdsWithComponent('EC_Terrain')[0]);
var terrain;
if(terrainEntity)
terrain = terrainEntity.terrain;
else {
print('Terrain not found');
terrain = null;
}
function StartAnimation()
{
avatarEntity = scene.GetEntityByName("Avatar1");
if (objectToAnimate != null && objectToAnimate.animationcontroller !=
null)
{
if(!avatarEntity)
frame.DelayedExecute(0.05).Triggered.connect(StartAnimation);
var avatarPos = avatarEntity.placeable.transform;
if(availableAnims<=0)
frame.DelayedExecute(1.0).Triggered.connect(StartAnimation);
availableAnims =
objectToAnimate.animationcontroller.GetAvailableAnimations();
var animFound = false;
for(var i=0; i<availableAnims.length; i++)
{
if (availableAnims[i] == animationNameToStart)
{
animFound = true;
break;
}
}
avatarPos = avatarEntity.placeable.transform;
myPos.pos.x = 12;
myPos.pos.y = 1.5;
myPos.pos.z = -15;
myPos.rot.x = 0;
myPos.rot.y = 0;
myPos.rot.z = 0;
me.placeable.transform = myPos;
if (mod(avatarPos.pos.z,myPos.pos.z) < 2 &&
mod(avatarPos.pos.x,myPos.pos.x) < 2 && enObject.skyx.time > 15.1)
{
objectToAnimate.animationcontroller.EnableAnimation(animationNameToStart,
true);
objectToAnimate.animationcontroller.SetAnimSpeed("Walk","1.0");
frame.DelayedExecute(0.1).Triggered.connect(updatePosition);
}
else
{
frame.DelayedExecute(0.5).Triggered.connect(StartAnimation);
}
}
else
{
frame.DelayedExecute(0.5).Triggered.connect(StartAnimation);
}
}
startScript();
function startScript()
{
avatarEntity = scene.GetEntityByName("Avatar1");
if(!avatarEntity)
frame.DelayedExecute(0.5).Triggered.connect(startScript);
else
StartAnimation();
}
function updatePosition(dt) {
var tm = me.placeable.transform;
var p = terrain.GetPointOnMap(tm.pos);
var avatarPos = avatarEntity.placeable.transform;
avatarPos = avatarEntity.placeable.transform;
if(mod(avatarPos.pos.z,tm.pos.z) < 8 && mod(avatarPos.pos.x,tm.pos.x) <
8 && enObject.skyx.time > 15.1)
{
objectToAnimate.animationcontroller.SetAnimSpeed("Walk","1.0");
objectToAnimate.animationcontroller.EnableAnimation(animationNameToStart,
true);
tm.pos.z = avatarPos.pos.z + 2;
tm.pos.y = p.y + 0.02;
tm.pos.x = avatarPos.pos.x + 2;
tm.rot.x = avatarPos.rot.x;
tm.rot.y = avatarPos.rot.y;
tm.rot.z = avatarPos.rot.z;
}
if(avatarEntity.animationcontroller.animationState != "Walk")
objectToAnimate.animationcontroller.DisableAllAnimations();
me.placeable.transform = tm;
tm.pos = p;
frame.DelayedExecute(0.0001).Triggered.connect(updatePosition);
}
function mod(a,b)
{
var c;
if(a>b)
c = a - b;
else
c = b - a;
if(c>0)
return c;
else
return (-1*c);
}
I have used AvatarApp provided with Tundra. It creates the avatar
dynamically that's why I need to check whether avatarEntity exists or it is
Null.
After the client is connected to the server, I see a QT error saying:
Failed to create timer (The current process has used all of its system
allowances of handles for Window Manager Object)
If I run the same script on client mode it runs well. I want to run it on
the server so as to synchronize it with other clients.
I haven't worked on QT before so I don't understand what does this error
mean?? Is there a better way of doing this?
Regards,
Vaibhav Vaidya
--
http://groups.google.com/group/realxtend
http://www.realxtend.org