Scripts / NPC that approaches and speaks to the player

Author
Message
Enzo1400
5
Years of Service
User Offline
Joined: 26th Jul 2018
Location:
Posted: 30th Apr 2022 21:05
Hello,

I found two scripts and I merged them into one with some changes. When the player approaches an NPC, it begins to walk and talk to him (audio and text). When the player moves away, the NPC stop. There is a flaw I can't get rid of: when NPC approaches the player, it keeps walking against the player, instead of stopping. Can anyone please help? Thank you in advance.

The code is:

function npc_approaching_init(e)
CharacterControlUnarmed(e)
end

local playingsound = 0
function npc_approaching_main(e)
PlayerDist = GetPlayerDistance(e)
if GetPlayerDistance(e) < 600 then
LookAtPlayer(e)
RotateToPlayer(e)
if GetPlayerDistance(e) < 200 then
SetCharacterToWalk(e)
Prompt("Hello, how may I help you?")
end
if playingsound == 0 then
PlaySound(e,1);
playingsound = 1
end
AIEntityGoToPosition(g_Entity[e]['obj'],g_PlayerPosX,g_PlayerPosZ)
else
AIEntityStop(g_Entity[e]['obj'])
StopAnimation(e)
end
end

function npc_approaching_exit(e)
end
PM
GubbyBlips
5
Years of Service
User Offline
Joined: 14th Jan 2019
Location:
Posted: 1st May 2022 03:03
You set a sound flag to activate sound one time, and then turn it off like this;
"playingsound = 1"
Now, you just needed a similar approach to modulate the character when approaching,
"approachplr[e] = 1"
and shuffle a few lines around to suit

So, in the end, it should look like this;
..........................................................................................

local approachplr = {}
local playingsound = {}

function npc_approaching2_init(e)
CharacterControlUnarmed(e)
approachplr[e] = 0
playingsound[e] = 0
end -- init


function npc_approaching2_main(e)
PlayerDist = GetPlayerDistance(e)
if GetPlayerDistance(e) < 600 and GetPlayerDistance(e) > 200 and approachplr[e] == 0 then
LookAtPlayer(e)
RotateToPlayer(e)
SetCharacterToWalk(e) -- new edit
AIEntityGoToPosition(g_Entity[e]['obj'],g_PlayerPosX,g_PlayerPosZ) -- new eidt
approachplr[e] = 1
end

if approachplr[e] == 1 then
if GetPlayerDistance(e) < 200 then
Prompt("Hello, how may I help you?", 5000)
AIEntityStop(g_Entity[e]['obj'])
StopAnimation(e)
approachplr[e] = 0
if playingsound[e] == 0 then
PlaySound(e,1);
playingsound[e] = 1
end
end
end
end -- main
PM
GubbyBlips
5
Years of Service
User Offline
Joined: 14th Jan 2019
Location:
Posted: 1st May 2022 03:07
..........
not exactly perfect yet.
perhaps you would want a
RotateToPlayer(e)
when under 200 units from player?

It's a start to work with.
PM
GubbyBlips
5
Years of Service
User Offline
Joined: 14th Jan 2019
Location:
Posted: 1st May 2022 03:45
So a little better version-- obviously no obstacle detection going on here with these simple scripts...

................................................................................................

local approachplr = {}
local playingsound = {}
local idletalk = {}

function npc_approaching3_init(e)
CharacterControlUnarmed(e)
approachplr[e] = 0
playingsound[e] = 0
idletalk[e] = 0
end -- init


function npc_approaching3_main(e)
PlayerDist = GetPlayerDistance(e)

if GetPlayerDistance(e) < 600 then
if approachplr[e] == 0 and GetPlayerDistance(e) >= 200 then
approachplr[e] = 1
LookAtPlayer(e)
end

if GetPlayerDistance(e) < 200 then
approachplr[e] = 0
idletalk[e] = 1
RotateToPlayer(e)
AIEntityStop(g_Entity[e]['obj'])
StopAnimation(e)
else -- keep walking
RotateToPlayer(e)
SetCharacterToWalk(e) -- new edit
end
else
approachplr[e] = 0
AIEntityStop(g_Entity[e]['obj']) -- deactivate this line and AI walks to last player position!
end

if approachplr[e] == 1 then
LookAtPlayer(e)
RotateToPlayer(e)
SetCharacterToWalk(e) -- new edit
AIEntityGoToPosition(g_Entity[e]['obj'],g_PlayerPosX,g_PlayerPosZ) -- new edit
end

if idletalk[e] == 1 then
idletalk[e] = 0
Prompt("Hello, how may I help you?", 5000)
if playingsound[e] == 0 then
PlaySound(e,1);
playingsound[e] = 1
end
end -- idletalk

end -- main
PM
Enzo1400
5
Years of Service
User Offline
Joined: 26th Jul 2018
Location:
Posted: 1st May 2022 10:16
It works! Thank you very much. It works mostly with characters made with Character Creator.
PM

Login to post a reply

Server time is: 2024-03-29 12:09:04
Your offset time is: 2024-03-29 12:09:04