[Code] For a NPC to play an animation Created 8 years ago2016-08-27 20:39:36 UTC by Shepard62FR Shepard62FR

Created 8 years ago2016-08-27 20:39:36 UTC by Shepard62FR Shepard62FR

Posted 8 years ago2016-08-27 20:40:52 UTC Post #331435
This problem has been haunting me for hours hence why I'm asking for help.

I have a NPC which is a clone of Barney except that it's hostile and has a shield (also it doesn't speak, meaning it's based on "CSquadMonster" instead of "CTalkMonster").

I have a "shield" animation linked to ACT_EXCITED, the NPC crouch, place it's shield, wait a bit then return to standing and aiming, that animation last for 5,58 seconds.

The shield itself works, the damage is blocked if you shoot it or spam your crowbar at it, the problem is below.

When I "force" the activity to ACT_EXCITED (in other words, I force the NPC to use it's shield), he use it for a second and start shooting at me again (or chase me). I want the NPC to keep shielding himself until he dies or the animation finished, how can I achieve that ?

Here is the code that forces the NPC to use it's shield (in the PrescheduleThink method)

m_IdealActivity = ACT_EXCITED;
m_flShieldTime = gpGlobals->time + 5.58f; // Shield for 5,58 seconds
m_flNextShieldTime = gpGlobals->time + 15.0f; // 15 seconds before using the shield again

In the GetSchedule method:

if ( m_Activity == ACT_EXCITED )
return &slMyNPCShield[ 0 ];
Thank you in advance for your help
Posted 8 years ago2016-08-27 22:12:36 UTC Post #331440
Did you tried to make i with Tasks?

I did so with the infantry of my mod, they shooted one time and start running, I put the shooting action three or four times and they keef firing for a longer period of time (4 x 1.5 secs = 6 secs). Something like this....
[quote]
//=============================================================
//=============================================================
Task_t tlShepardsBarneyShield[] =
{
{ TASK_STOP_MOVING,						(float)0					},
{ TASK_PLAY_SEQUENCE,					(float)ACT_CROUCH			},
{ TASK_PLAY_SEQUENCE,					(float)ACT_EXCITED	},
{ TASK_PLAY_SEQUENCE,					(float)ACT_EXCITED	},
{ TASK_PLAY_SEQUENCE,					(float)ACT_EXCITED	},
{ TASK_PLAY_SEQUENCE,					(float)ACT_EXCITED	},
{ TASK_PLAY_SEQUENCE,					(float)ACT_EXCITED	},
(float)ACT_STAND },
};

Schedule_t slShepardsBarneyShield[] =
{
{ 
	tlShepardsBarneyShield,
	ARRAYSIZE ( tlShepardsBarneyShield ),
	bits_COND_NEW_ENEMY		|
	bits_COND_LIGHT_DAMAGE	|
	bits_COND_HEAVY_DAMAGE,
	0,
	"Shepards Barney Shield"
},
};

//=========================================================
//=========================================================
[/quote]

Please, be merciful with the code, you know I cannot make even the "Hello World" program, but this is what I´ve done and it worked. ;)
Posted 8 years ago2016-08-28 07:31:03 UTC Post #331443
Yes I tried with a schedule and a task, here is my attempt:

[quote]Task_t tlMyNPCShield[] =
{
{ TASK_STOP_MOVING,					(float)0			},
{ TASK_PLAY_SEQUENCE_FACE_ENEMY,	(float)ACT_EXCITED	},
{ TASK_WAIT,						(float)5.58f		}
};

Schedule_t slMyNPCShield[] =
{
{
	tlMyNPCShield,
	ARRAYSIZE( tlMyNPCShield ),
	0,
	0,
	"MyNPC Shield"
},
};[/quote]

I tried with your task and schedule and it doesn't work, he play the animation for 1 second (instead of the 5,58 seconds) and shoot at me again (or chase me).
Posted 8 years ago2016-08-28 16:35:08 UTC Post #331450
You'll probably need to add custom tasks to keep the shield behavior going until it's deployed.

For an example on how to make a time delay task, look at Big Momma's TASK_WAIT_NODE task. It has helpful alerts to help indicate what is happening.

You should call TaskComplete() when it has finished. Make sure to handle failure cases as well in case shield deployment needs to be cancelled.
Posted 8 years ago2016-08-28 19:43:21 UTC Post #331456
Thank you Solokiller, I had some trouble getting to work at first but after a lot of tests I managed to get it working as it should. Here is the code for people who are interested:

[quote]Task_t tlMyNPCShield[] =
{
{ TASK_STOP_MOVING,					(float)0			},
{ TASK_PLAY_SEQUENCE_FACE_ENEMY,	(float)ACT_EXCITED	},
{ TASK_EAT,							(float)0			}
};

Schedule_t slMyNPCShield[] =
{
{
	tlMyNPCShield,
	ARRAYSIZE( tlMyNPCShield ),
	0,
	0,
	"MyNPC Shield"
},
};

Schedule_t *CMyNPC::GetSchedule( void )
{
switch ( m_MonsterState )
{
case MONSTERSTATE_COMBAT:
	if ( m_hEnemy != NULL && pev->health <= (gSkillData.zombieHealth / 2.0f) && gpGlobals->time > m_flNextShieldTime )
		return GetScheduleOfType( SCHED_COWER );
}
return CSquadMonster::GetSchedule();
}

Schedule_t *CMyNPC::GetScheduleOfType( int Type )
{
switch ( Type )
{
case SCHED_COWER:
	return slMyNPCShield;
	break;
}
return CSquadMonster::GetScheduleOfType( Type );
}

void CMyNPC::RunTask( Task_t *pTask )
{
switch ( pTask->iTask )
{
case TASK_EAT:
	TaskComplete();
	break;
default:
	CSquadMonster::RunTask( pTask );
	break;
}
}

void CMyNPC::StartTask( Task_t *pTask )
{
switch ( pTask->iTask )
{
case TASK_EAT:
	m_flNextShieldTime = gpGlobals->time + 15.0f;
	break;
default:
	CSquadMonster::StartTask( pTask );
	break;
}
}[/quote]
Posted 8 years ago2016-08-29 13:01:41 UTC Post #331464
m_flNextShieldTime = gpGlobals->time + 15.0f;
So that part is the key, right?
Posted 8 years ago2016-08-29 15:52:29 UTC Post #331466
Nope, it's the schedule, task, StartTask and RunTask
Posted 8 years ago2016-08-29 19:59:54 UTC Post #331473
Of course, I was talking about the time while your npc is holding the shield. I did not read well, the "next" part says it all ;).
Posted 8 years ago2016-09-04 11:29:32 UTC Post #331554
Shepard, I have the opposite problem, I want my npc to perform the RANGE_ATTACK once every 30 seconds, but it, once starts, keeps doing the ACT_RANGE_ATTACK continuously... please, help!! It kills me in a glance!!
Posted 8 years ago2016-09-13 14:26:29 UTC Post #331640
I have "fixed" a bit how my npc works, but it still performs the attack animation continuously even when it's flying, what can I do?, I have tried the few things I know (mflNextAttack, and all that stuff) with no luck, probably because I misplaced the pieces of code I tried. Please, help! :(
Posted 8 years ago2016-09-13 16:12:13 UTC Post #331657
Could you show us the code you're using to handle the attack?
Posted 8 years ago2016-09-13 20:34:06 UTC Post #331664
You´ve got PM ;).
You must be logged in to post a response.