creating NPCs in scene
by Roger D Vargas · in Torque 3D Beginner · 06/19/2014 (11:23 am) · 8 replies
I need to create some NPCs in the scene, I first tried to place a spawn sphere, but besides the player spawn sphere, no other created any character. I asked in the chat and I was told that it has to be done via scripts, like in RTS tutorial. Where should I place the code to create the NPCs when scene is loaded?
About the author
Game developer and writer
#2
In scripts/server/Aiplayer.cs create a new custom spawn function.
And the in the gameCore.cs function of startgame, at the bottom add:
And simply repeat for as many Ai as you want.
06/20/2014 (6:13 am)
If you're wanting all your AI to be around at the start of the game, I would suggest creating them in "StartGame" in the file is scripts/server/gameCore.cs.In scripts/server/Aiplayer.cs create a new custom spawn function.
function AIPlayer::spawnBot(%name, %data, %spawnPoint, %weapon)
{
%player = new AiPlayer(%name)
{
dataBlock = %data;
path = "";
};
MissionCleanup.add(%player);
%player.setShapeName(%name);
%player.setTransform(%spawnPoint.getTransform());
echo(%name @ " has spawned at " @ %spawnpoint);
%player.equipBot(%weapon);
return %player;
}And the in the gameCore.cs function of startgame, at the bottom add:
%bot1 = AIPlayer::spawnBot(%name, %datablock, %spawnPoint, %weaponImage); //name is the name of the actual object so that you can call script on it in game //datablock is the custom Ai datablock the stock one is "DemoPlayer" //spawnpoint needs to be the name of the spawnpoint in the game where the Ai will appear //weaponimage is the name of the weapon they're holding eg: LurkerWeaponImage
And simply repeat for as many Ai as you want.
%bot1 = AIPlayer::spawnBot(Ai1, DemoPlayer, spawn1, LurkerWeaponImage); %bot2 = AIPlayer::spawnBot(Ai2, DemoPlayer, spawn2, RyderWeaponImage); %bot3 = AIPlayer::spawnBot(Ai3, DemoPlayer, spawn3, LurkerWeaponImage);
#4
06/20/2014 (8:27 am)
S
#5
06/20/2014 (8:29 am)
Steve, how does this specify the map where the NPC will spawn? The game is an RPG and I need some specific characters in each map.
#6
06/20/2014 (1:14 pm)
Rogdev in that case you'd be better off using triggers in the map and having them call the spawnBot function. Of course you'll have to check what object is entering the trigger and turn them off after spawning. Check my tactics game resource, I think that has the info you'd want.
#7
06/20/2014 (6:27 pm)
Additional note: Spawning a player in a trigger does not fire the OnEnterTrigger() callback.
#8
06/22/2014 (7:22 pm)
Hmm. You could just use spawn points. When exiting a level (in mission cleanup) walk your NPCs, move the associated spawn point to each NPC's current location and save the level, then clean up and load the next level. And you can add a way to change the NPC AI "think frequency" based on zones, triggers or distance from the camera etc to keep NPCs on the other end of the map from wasting processor time - see my AI Tutorial scripts, specifically scripts/server/aiManager.cs for a possible "think throttling" method.
Jason Campbell
Part Eight of the Simple FPS Tutorial covers what you need. You'll just have to change the custom weapons.
tdn.garagegames.com/wiki/T3D/Tutorials/SimpleFPSTutorial/Part8