Frictional Games Forum (read-only)

Full Version: Problem with door closing
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a script where if the player goes through an area where a door is, and has certain items in their inventory, the door will close and lock itself behind them. When they go through the area, I'm using HasItem(); to check that they have the items, but the door doesn't close or lock.

My code:
Code:
void OnStart()
{
    AddEntityCollideCallback("Player", "enabledoorscript", "DoorScript", false, 1); //area to check if the door should close or not depending on if the player has items
}

void DoorScript(string &in asParent, string &in asChild, int alState)
{
    if(HasItem("ITEM1") && HasItem("ITEM2") && HasItem("ITEM3")) //checking for items
    {
        AddEntityCollideCallback("Player", "closedoor", "CloseDoor", true, 1); //they do, so add the collision callback for the area
    }
}

void CloseDoor(string &in asParent, string &in asChild, int alState)
{
    SetSwingDoorClosed("door1", true, true); //close the door
    SetSwingDoorLocked("door1", true, true); //lock the door
}
You've got the idea, but you forgot to check if it's true or false that you have it...

if you go here:
http://wiki.frictionalgames.com/hpl2/amn..._functions
You'll notice that "HasItem" requires a bool. (It says 'bool' to the left of it).

A bool is either "true" or "false".

So, to make your script correct, it should look like this.

PHP Code:
void OnStart()
{
    
AddEntityCollideCallback("Player""enabledoorscript""DoorScript"false1); //area to check if the door should close or not depending on if the player has items
}

void DoorScript(string &in asParentstring &in asChildint alState)
{
    if(
HasItem("ITEM1") == true && HasItem("ITEM2") == true && HasItem("ITEM3") == true//checking for items
    
{
        
SetSwingDoorLocked("door1"truetrue); //lock the door when true
    
}


This should work.

I also added some adjustments
(10-13-2014, 05:39 PM)FlawlessHappiness Wrote: [ -> ]You've got the idea, but you forgot to check if it's true or false that you have it...

if you go here:
http://wiki.frictionalgames.com/hpl2/amn..._functions
You'll notice that "HasItem" requires a bool. (It says 'bool' to the left of it).

A bool is either "true" or "false".

So, to make your script correct, it should look like this.

PHP Code:
void OnStart()
{
    
AddEntityCollideCallback("Player""enabledoorscript""DoorScript"false1); //area to check if the door should close or not depending on if the player has items
}

void DoorScript(string &in asParentstring &in asChildint alState)
{
    if(
HasItem("ITEM1") == true && HasItem("ITEM2") == true && HasItem("ITEM3") == true//checking for items
    
{
        
SetSwingDoorLocked("door1"truetrue); //lock the door when true
    
}


This should work.

I also added some adjustments

Thanks for the reply. I corrected the code, but it still isn't working. The door also behaves strangely after I enter the area that should lock it. It only lets me grab the door in certain places at the top of the door while (I think) I'm inside the script area. :S

EDIT: I'm so stupid. I made a typo on one of the entity's names. Sorry for the inconvenience XD

Fixed it.
So everything works?
Just as a side note, the original boolean statements were correct. You don't explicitly need to define a true or false to the boolean in question. If undefined, it assumes true. Another way of doing false would be do to
PHP Code:
if(!HasItem("Name")) 
(10-13-2014, 09:33 PM)Mudbill Wrote: [ -> ]Just as a side note, the original boolean statements were correct. You don't explicitly need to define a true or false to the boolean in question. If undefined, it assumes true. Another way of doing false would be do to
PHP Code:
if(!HasItem("Name")) 

This.

Actually, the reason this works is because the "if" statement is checking whether the whole condition (the entire contents of the brackets) works out as "true", eg:

Code:
if(true)
if(!false)
if(1 + 1 == 2)
if(2 == 2)
if(x == x)
if(false == false)
if(false != true)
if(false == !true)
if((false == true) == (true == false))
if((false == !true) == (true == !false))
if(StringToBool("true"))

...are all different ways of saying "if(true)", and the condition would pass.
^ Good points.
So eh... That means it was all correct from the beginning?

How come it worked when he put " == true" on it then...?



Maybe because of the "Adjustments" I made... Not sure.
(10-13-2014, 06:03 PM)NickD Wrote: [ -> ]EDIT: I'm so stupid. I made a typo on one of the entity's names. Sorry for the inconvenience XD

Fixed it.

I guess that was the problem all along.
(10-13-2014, 11:48 PM)MrBehemoth Wrote: [ -> ]
(10-13-2014, 06:03 PM)NickD Wrote: [ -> ]EDIT: I'm so stupid. I made a typo on one of the entity's names. Sorry for the inconvenience XD

Fixed it.

I guess that was the problem all along.

Haha!
^-^

Oh well, I learned stuff! Thank you!