Game Development Community

Adding NPCs

by Dreamer · in RTS Starter Kit · 11/08/2005 (12:10 am) · 1 replies

Hello everyone, I have found a way to add NPCs to the game, which would be the first step in making an RPG engine out of RTS. In reality it was much simpler than I thought, there is one problem however. As soon as you attempt to attack him the engine segfaults on me. This could be due to a number of causes however I cannot seem to locate the problem at the moment, since I have to use the precompiled binary (RTS Doesn't compile for me), and cannot do a DDD to find out, likewise the console.log is equally unhelpful.
Anyways, it's a start and here is my code.

In server/scripts/core/game.cs add the following at the bottom of your startGame() function.
$AIManager = new RTSConnection() {};
   $AIManager.units = new SimGroup();
   %clientIndex = $AIManager.getClientIndex();
   $AIManager.setTeam(%clientIndex);
   MissionCleanup.add($AIManager);
   $AIManager.createPlayer("0 0 400",3);

Note, he may be invisible until you locate him, so wander north east eventually you will find the guy.

#1
11/08/2005 (2:55 am)
Woohoo! I solved the segfaulting issue, or at least found the cause.
Turns out if you attack a function called RTSUnit::OnDamage is called.
Currently all this function does is message the client to inform them that the unit has been damaged.
This causes a segfault, if there is no client for the message to go to.
My solution at least temporarily is to do this.
In server/scripts/avatars/player.cs
Find the function RTSUnit::OnDamage and comment out the following line.
messageClient(%obj.client, 'MsgUnitAttacked', "", getWords(%obj.getPosition(), 0, 2));

That'll do the trick, until I can get some sort of way setup to tell if it's AI controlled or not.
In the meantime, we now have a way to place one enemy NPC in the game, and have them be attackable!