04.12.2011, 06:06
(
Last edited by Lorenc_; 21/01/2012 at 08:46 AM.
)
A controllable NPC script using the default NPC natives.
1st. I was inspired by erorcun's controllable NPC script though had a lot of issues with that so I wanted to create my own using some of his ideas in the creation of this include. This include is MUCH easier to use, hardly anything will error if you do all the requested inside the thread correctly. This is stated down.
2nd. Erorcun's probably has more functions though soon, this include will reach more functions by each version which I'm willing to update and support. The major thing about this version is that it uses up-to-date methods. It's faster than his I assume!
3rd. I'm a fan of NPC's and love playing with them while no-ones around
What this version uses:
- y_ini - The fastest reader/writer
- Mapandreas - RyDeR`'s one, I can make a version for users using the plugin if needed!
- y_hooks - One single include contains all the features and some callbacks required hooking.
- No filterscripts or so many files! Just 1 npcmode file and a scriptfile folder to be created!
- Up-to-date and organised!
- Easy as pie to use!
- Has a fix towards duplicates of a NPC name!
pawn Code:
/*
native CreateCNPC(name[24], skin, Float: X, Float: Y, Float: Z)
native SetNpcMoveTo(nID, Float: X, Float: Y, Float: Z, bool: run = false)
native GetNPCPlayerID(nID)
native GetDistanceBetweenPlayerAndNPC(playerid, nID)
native GetClosestNPC(playerid)
native GetNpcIDFromPlayerID(playerid)
native StopCNPC(npcid)
native GetCNPCTotalCount()
native ReturnNpcName(npcid)
native SetNPCPos(npcid, Float: X, Float: Y, Float: Z)
native ApplyAnimationToNPC(npcid, animlib[], animname[], Float: fDelta, loop, lockx, locky, freeze, time, forcesync = 0)
native SetNPCFacingAngle(npcid, Float: Angle)
native GiveNPCWeapon(npcid, weaponid, ammo)
native ResetNPCWeapons(npcid)
native SetPlayerFacePoint(playerid, Float: fX, Float: fY, Float: offset = 0.0)
native strmatch(const str1[], const str2[])
native strreplacechar(string[], oldchar, newchar)
*/
Example Gamemode
pawn Code:
/*
*
* CNPC - Gamemode Example.
*
*
*
*
*
*
*/
#include <a_samp>
#include <a_cnpc>
#include <zcmd>
/* ** NPC DATA ** */
new
Npc: PornhubUser,
szTmpstring [128] // I'm being a bit careless about strings now. Haha.
;
main(){}
public OnGameModeInit()
{
SetGameModeText("CNPC 0.1a");
AddPlayerClass(0, 0.0, 0.0, 5.0, 270.0, 0, 0, 0, 0, 0, 0);
CreateCNPC("Jackie", 119, 5, 0, 5);
CreateCNPC("Troll", 289, 0, 5, 5);
PornhubUser = CreateCNPC("Porn Hub User", 5, 5, 5, 5);
CreateCNPC("Porn Hub User", 5, 5, 5, 5);
CreateCNPC("Porn Hub User", 5, 5, 5, 5);
return 1;
}
public OnGameModeExit() return 1;
public OnPlayerRequestClass(playerid, classid)
{
SetPlayerPos(playerid, 1958.3783, 1343.1572, 15.3746);
SetPlayerCameraPos(playerid, 1958.3783, 1343.1572, 15.3746);
SetPlayerCameraLookAt(playerid, 1958.3783, 1343.1572, 15.3746);
return 1;
}
public OnNpcCreated(npcid)
{
printf("%s(%d) created!", ReturnNpcName(npcid), npcid);
return 1;
}
public OnNpcConnected(npcid)
{
printf("%s(%d) connected!", ReturnNpcName(npcid), npcid);
return 1;
}
public OnNpcFinishedMoving(npcid)
{
printf("%s(%d) finished moving!", ReturnNpcName(npcid), npcid);
return 1;
}
public OnNpcDestroyed(npcid)
{
printf("%s(%d) destroyed!", ReturnNpcName(npcid), npcid);
return 1;
}
CMD:closest(playerid, params[])
{
new Npc: nTmp = GetClosestNPC(playerid);
format(szTmpstring, 128, "Closest: %d", _: nTmp);
SendClientMessage(playerid, -1, szTmpstring);
return 1;
}
CMD:distance(playerid, params[])
{
if(!IsNPCConnected(strval(params))) return 0;
new Float: fTmp = GetDistanceBetweenPlayerAndNPC(playerid, Npc:strval(params));
format(szTmpstring, 128, "Distance: %f", fTmp);
SendClientMessage(playerid, -1, szTmpstring);
return 1;
}
CMD:walk(playerid, params[])
{
new Float: X, Float: Y, Float: Z;
GetPlayerPos(playerid, X, Y,Z);
SetNpcMoveTo(PornhubUser, X, Y, Z, false);
return 1;
}
CMD:run(playerid, params[])
{
new Float: X, Float: Y, Float: Z;
GetPlayerPos(playerid, X, Y,Z);
SetNpcMoveTo(PornhubUser, X, Y, Z, true);
return 1;
}
CMD:runall(playerid, params[])
{
new Float: X, Float: Y, Float: Z;
GetPlayerPos(playerid, X, Y,Z);
foreachcnpc(i)
{
SetNpcMoveTo(i, X, Y, Z, true);
}
return 1;
}
If anyone could make a video, PM me the link of the video and I'll clip it on the the thread. I CBF doing so.
Disadvantages
- Sloppy walking/running, takes approx 1 second to update movement.
- Compile-time might take about 0.750 milliseconds due to compilation of YSI. (y_hooks especially, use v0.2a to have this situation cured!
- Doesn't stream NPC's (might soon)
- SetPlayerVelocity fails, don't msg me about it.
- Fast and up-to-date.
- Works on Linux (Pretty sure, since it's using the natives)
- No plugins required!
- Very easy to use
- ETC. (Look at "Why? What? Why'd you make this?" to see some more stuff)
[0.2.2a] Latest (v)
[0.2.2a] Recommended build via pastebin!
[0.1a] Download (Not recommended, stick with the latest.)
Includes the scriptfiles/npcmodes/includes/examples. ( All Versions )
Notifications
Code:
[0.2a] This version hooks all callbacks itself! No y_hooks meaning no long compilation time :) [0.2a] Cool functions added. [0.2.2a] Major bug fix with NPC's not spawning to their location. [0.2.2a] New call back "OnNpcSpawn"..
How can I make something like Zombies?
Look here. That link describes what it takes to create such a thing though indeed, you need a bit of experience.
Where do I place the .inc file?
Inside your server directory, click on "pawno" then click again on "include"
Why is the walking/running so sloppy?
You need to read through this again. DERP.
Where do I create a NPC?
Look at the example.
On some functions I get "Tag mismatch", how can I prevent this?
I'm not sure where you'll get that but if you do, you can use "Convert.ToNPC(param)" to prevent it.
Credits
Lorenc_ - Created this.
RyDeR` - Mapandreas include.
erorcun - Ideas/solutions.
y_less - y_ini.