Scripts / Using an NPC to open a Door

Author
Message
hughesygaming
5
Years of Service
User Offline
Joined: 6th Feb 2019
Location:
Posted: 13th Feb 2019 19:46
Hi guys, so I am attempting to use an NPC to open a door, not entirely sure if this is possible, but if so, I would love to know how. Below is the script I have used to speak with an NPC and currently have only set the option of 'E'.


g_KeyPressE = 0
g_KeyPressQ = 0

function questnpc_init(e)
CharacterControlUnarmed(e)
end

function questnpc_main(e)
PlayerDist = GetPlayerDistance(e)
if PlayerDist < 200 then
LookAtPlayer(e)
if g_KeyPressE == 0 then
Prompt ( "Mr Detective, you must select one of the following paths, 'E' will send you down a path of failure and 'Q' will send you on a path of fortune" )
else
if g_KeyPressE == 1 then
Prompt ( "Congrats Mr Detective" )
end
end
end
--if string.find(string.lower(g_Entity[e]['limbhit']), "head" ~= nil then
-- SetEntityHealth(e,0)
-- ResetLimbHit(e)
-- end
end


Here is the script for the door, however, I can press 'E' at spawn and this door will just open without having the prompt from the NPC, I am stuck and am getting frustrated.


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

g_KeyPressE = 0
door_pressed = 0

function dooropen_init(e)
end
function dooropen_main(e)
PlayerDist = GetPlayerDistance(e)
if g_KeyPressE == 1 and g_Entity[e]['animating'] == 0 and door_pressed == 0 then
--GetEntityPlayerVisibility(e)
--if 1 then
if g_Entity[e]['activated'] == 0 then
SetAnimation(0)
PlayAnimation(e)
g_Entity[e]['animating'] = 1
CollisionOff(e)
ActivateIfUsed(e)
PlaySound(e,0)
StartTimer(e)
door_pressed = 1
end
--end
if g_Entity[e]['activated'] == 1 then
if GetTimer(e)>1000 then
CollisionOff(e)
door_pressed = 1
end
end
else
if g_Entity[e]['activated'] == 2 then
SetAnimation(1)
PlayAnimation(e)
g_Entity[e]['animating'] = 1
PlaySound(e,1)
CollisionOn(e)
door_pressed = 1
end
end
end


Any help would be much appreciated, not expecting anyone to write the script for me, just guidance will be appreciated.
PM
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 13th Feb 2019 21:54
You will need a flag that is set in one script and read in the other.

You can't set g_KeyPressE btw, you can read it to find out if the key is pressed but you will need to 'debounce' the key press yourself.

Been there, done that, got all the T-Shirts!
PM
hughesygaming
5
Years of Service
User Offline
Joined: 6th Feb 2019
Location:
Posted: 14th Feb 2019 11:22
Sorry if I come across stupid, but I'm trying to learn LUA, not entirely sure on how to setup a flag and how do I go about debouncing? Guess I will have to play about with the scripts. Thank you for your response btw.
PM
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 14th Feb 2019 12:36
A flag is simply a variable that contains two states, g_KeyPressE is a flag where 0 indicates the E key is not pressed and 1 indicates it is.

To make code easier to read it is more normal to use true or false values for flags.

So in your NPC script you declare a flag, for example g_npc_has_spoken = false, then at the point where the player is given his choice set the flag to true. In the other script add a check for the flag to the logic that allows the door to open.

By 'debounce' I mean that your script is called every frame, assuming vsync is set then that will be 60 times per second, in the time it takes for the player to press a key and let go of it your script may have executed ten times or more so in the script you need to make sure that you don't repeatedly process the key press every frame, you only want to process it once per press event.

This is done by having a flag to indicate that you have already processed the key press which you can check next time the script is executed.

Been there, done that, got all the T-Shirts!
PM
Teabone
Forum Support
17
Years of Service
User Offline
Joined: 8th Jun 2006
Location: Earth
Posted: 14th Feb 2019 23:01 Edited at: 14th Feb 2019 23:01
This was so simple to do back in FPSC - 1 simple command.
Twitter - Teabone3 | Youtube - Teabone3 | Twitch - Teabone3 | TGC Assets - Store Link | Patreon - Teabone3

i7 -2600 CPU @ 3.40GHz - Windows 7 - 8GB RAM - Nivida GeForce GTX 960

Login to post a reply

Server time is: 2024-04-25 02:07:34
Your offset time is: 2024-04-25 02:07:34