Scripts / Two scripts into one to follow waypoints and other

Author
Message
Enzo1400
6
Years of Service
User Offline
Joined: 26th Jul 2018
Location:
Posted: 13th Jul 2022 22:37
Hello,

I'm trying to use together two scripts. One is by Smallg (civilian.lua) and another that showed GubbyBlips https://forum.game-guru.com/thread/223595#msg2648268

The scripts are the following:

1)

-- LUA Script - precede every function and global member with lowercase name of script

-- globals
local aniplaying = 0
ai_newdest_time = {}
ai_cover_on = {}
-- init when level first runs
function civilian_init(e)
ai_soldier_state[e] = "patrol";
ai_soldier_pathindex[e] = -1;
ai_start_x[e] = nil
ai_starting_heath[e] = nil
ai_ran_to_cover[e] = 0
ModulateSpeed(e,1.0)
ai_old_health[e] = 0
--CharacterControlStand(e)
ai_returning_home[e] = 0
ai_newdest_time[e] = -1
ai_cover_on[e] = 0
ai_alerted_spoken[e] = 0

end

function civilian_main(e)
EntObjNo = g_Entity[e]['obj'];
MoveUp(e,1)
-- Detect player distance (note: possible optimization here)
PlayerDX = g_Entity[e]['x'] - g_PlayerPosX;
PlayerDY = g_Entity[e]['y'] - g_PlayerPosY;
PlayerDZ = g_Entity[e]['z'] - g_PlayerPosZ;
PlayerDist = math.sqrt(math.abs(PlayerDX*PlayerDX)+math.abs(PlayerDY*PlayerDY)+math.abs(PlayerDZ*PlayerDZ));

--if ai_ran_to_cover[e] ~= 1 then
-- Always looking at player
--if PlayerDist < 200 then
--LookAtPlayer(e)
--AIEntityGoToPosition(EntObjNo,g_Entity[e]['x'],g_Entity[e]['z'])
--else
-- Patrol Mode
if ai_soldier_state[e] == "patrol" then

-- Try and find a close path to patrol, just check once for it
if ai_soldier_pathindex[e] == -1 then

ai_soldier_pathindex[e] = -2

CharacterControlArmed(e)

-- find initial waypoint path to follow
PathIndex = -1;
pClosest = 99999;
for pa = 1, AIGetTotalPaths(), 1 do
for po = 1 , AIGetPathCountPoints(pa), 1 do
pDX = g_Entity[e]['x'] - AIPathGetPointX(pa,po);
pDZ = g_Entity[e]['z'] - AIPathGetPointZ(pa,po);
pDist = math.sqrt(math.abs(pDX*pDX)+math.abs(pDZ*pDZ));
if pDist < pClosest and pDist < 200 then
pClosest = pDist;
PathIndex = pa;
end
end -- po
end -- pa

-- follow found path
if PathIndex > -1 then
ai_soldier_pathindex[e] = PathIndex;
ai_path_point_index[e] = 2
ModulateSpeed(e,1.0)
SetCharacterToWalk(e)
ai_path_point_direction[e] = 1
ai_path_point_max[e] = AIGetPathCountPoints(ai_soldier_pathindex[e])
end

end
-- If we have a path then lets patrol it
if ai_soldier_pathindex[e] > -1 then

ai_patrol_x[e] = AIPathGetPointX(ai_soldier_pathindex[e],ai_path_point_index[e])
ai_patrol_z[e] = AIPathGetPointZ(ai_soldier_pathindex[e],ai_path_point_index[e])

AIEntityGoToPosition(EntObjNo,ai_patrol_x[e],ai_patrol_z[e])

tDistX = g_Entity[e]['x'] - ai_patrol_x[e]
tDistZ = g_Entity[e]['z'] - ai_patrol_z[e]

DistFromPath = math.sqrt(math.abs(tDistX*tDistX)+math.abs(tDistZ*tDistZ))

if DistFromPath < 50 then
if ai_path_point_direction[e] == 1 then
ai_path_point_index[e] = ai_path_point_index[e] + 1
if ( ai_path_point_index[e] > ai_path_point_max[e] ) then
ai_path_point_index[e] = ai_path_point_max[e] -1
ai_path_point_direction[e] = 0
end
else
ai_path_point_index[e] = ai_path_point_index[e] - 1
if ( ai_path_point_index[e] < 1 ) then
ai_path_point_index[e] = 2
ai_path_point_direction[e] = 1
end
end
end
else
CharacterControlFidget(e)
end
end

-- end
--if g_Entity[e]['health'] < 100 then
-- SetCharacterToRun(e)
-- ModulateSpeed(e,1.3)
-- ai_ran_to_cover[e] = 1
-- AISetEntityControl(EntObjNo,AI_MANUAL);
-- AIEntityMoveToCover(EntObjNo,g_PlayerPosX,g_PlayerPosZ);
-- end
end

_______________________________

2)

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

_______________________________

In practice, the npc character should follow the waypoints in a neutral way until the player is at some distance away from him, then the second script should activate (walk towards the player and say something). I can't get them to work together because there is a conflict where the npc character is still trying to follow waypoints.

Anyone know a way to merge the two scripts? I don't know how to stop and then resume walking along the waypoints.

In another way, is it possible to write a script of this type, that executes the others:

if GetPlayerDistance(e) > 800 then

(switchscript or do file ...)

if GetPlayerDistance(e) < 800 then

(switchscript or do file ...)


Thank you in advance
PM
smallg
Community Leader
18
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 14th Jul 2022 08:29
Quote: "In another way, is it possible to write a script of this type, that executes the others:

if GetPlayerDistance(e) > 800 then

(switchscript or do file ...)

if GetPlayerDistance(e) < 800 then

(switchscript or do file ...)
"

that should work but you would need to assign it to another entity and pass the desired entity number cos it will swap from that script to the other script if assigned to the actual character
not really used switchscript myself but i think you have to 'require' the intended script(s) and then you can switch to it as needed
lua guide for GG
https://steamcommunity.com/sharedfiles/filedetails/?id=398177770
windows 10
i5 @4ghz, 8gb ram, AMD R9 200 series , directx 11
Enzo1400
6
Years of Service
User Offline
Joined: 26th Jul 2018
Location:
Posted: 14th Jul 2022 16:21
Thank you Smallg for your reply, but I'm very newbye to lua scripting. I've seen these threads: https://forum.game-guru.com/thread/217896#msg2577455 and https://forum.game-guru.com/thread/219096#msg2592310 but I don't know how to use them.
May you show me how to attach to an entity a script that controls the state of a NPC based on the distance of a player? So the NPC changes state (script) without conflicts?
PM
smallg
Community Leader
18
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 14th Jul 2022 18:31 Edited at: 14th Jul 2022 18:31
it should be something like this but i'm not sure how to require a concatenated string - seems to return an error
(also not sure switch script even works in max)
lua guide for GG
https://steamcommunity.com/sharedfiles/filedetails/?id=398177770
windows 10
i5 @4ghz, 8gb ram, AMD R9 200 series , directx 11

Attachments

Login to view attachments
3com
10
Years of Service
User Offline
Joined: 18th May 2014
Location: Catalonia
Posted: 14th Jul 2022 21:59
@ smallg
Quote: "it should be something like this but i'm not sure how to require a concatenated string - seems to return an error"


Try this one below rather than "Require" statement.
Quote: "Include(dbsc.behaviour1)
Include(dbsc.behaviour2)"
Laptop: Lenovo - Intel(R) Celeron(R) CPU 1005M @ 1.90GHz

OS: Windows 10 (64) - Ram: 4 gb - Hd: 283 gb - Video card: Intel(R) HD Graphics
cpu mark: 10396.6
2d graphics mark: 947.9
3d graphics mark: 8310.9
memory mark 2584.8
Disk mark: 1146.3
Passmark rating: 3662.4

PM
Enzo1400
6
Years of Service
User Offline
Joined: 26th Jul 2018
Location:
Posted: 15th Jul 2022 00:41
Thank you very much Smallg and 3com. How to use Include(dbsc.behaviour2) and where, instead of:

require "scriptbank\\"..(dbsc.behaviour1)
require "scriptbank\\"..(dbsc.behaviour2)
dbsc.state = "init"

About you, is it possible to run the scripts separately from two different trigger zone that point to the NPC, and how? These types of scripts might be used in different situations.
PM
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 15th Jul 2022 19:42
Quote: "(also not sure switch script even works in max)"


Yep does but you have to 'require' whichever scripts you may be likely to switch to and you can't do the require in the middle of a function like that.
Been there, done that, got all the T-Shirts!
PM
smallg
Community Leader
18
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 15th Jul 2022 20:29 Edited at: 15th Jul 2022 20:30
yh Include instead of require works, thanks
place this on an entity and make sure you have at least one entity in your map with the name you set in the behaviour
lua guide for GG
https://steamcommunity.com/sharedfiles/filedetails/?id=398177770
windows 10
i5 @4ghz, 8gb ram, AMD R9 200 series , directx 11

Attachments

Login to view attachments
3com
10
Years of Service
User Offline
Joined: 18th May 2014
Location: Catalonia
Posted: 15th Jul 2022 20:54
@ smallg
Nope, thanks to you for writing the script.
Laptop: Lenovo - Intel(R) Celeron(R) CPU 1005M @ 1.90GHz

OS: Windows 10 (64) - Ram: 4 gb - Hd: 283 gb - Video card: Intel(R) HD Graphics
cpu mark: 10396.6
2d graphics mark: 947.9
3d graphics mark: 8310.9
memory mark 2584.8
Disk mark: 1146.3
Passmark rating: 3662.4

PM
Enzo1400
6
Years of Service
User Offline
Joined: 26th Jul 2018
Location:
Posted: 17th Jul 2022 16:18
Thank you all! I will try to use the script.
PM

Login to post a reply

Server time is: 2024-11-23 07:57:37
Your offset time is: 2024-11-23 07:57:37