Frictional Games Forum (read-only)

Full Version: need help: if HasItem, spawn monster
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey guys.

I've got a bit of a scripting problem (or so it seems). What I want to have happen is the player picks up a key, walks into an area, and (since they have a key) the monster starts beaking down a door behind the player.

When I tested it, it didn't seem to work. Here's the whole .HPS file (it's not very big):

Spoiler below!

void OnStart()
{
SetEntityCallbackFunc("keytonextstage", "OnPickup");

AddUseItemCallback("", "keytostorageroomonedoor", "storageroomonedoor", "KeyOnDoorStorageRoomOneDoor", true);

AddEntityCollideCallback("Player", "furiousenemyspawn", "collidefuriousenemyspawn", true, 1);
}

void collidefuriousenemyspawn(string &in asParent, string &in asChild, int alState)
{
if(HasItem("keytostorageroomonedoor") == true)
{
SetEntityActive("furious_enemy", true);
ShowEnemyPlayerPosition("furious_enemy");
}
}


//void OnPickup(string &in asEntity, string &in type)
//{
// SetEntityActive("furious_enemy", true);
// ShowEnemyPlayerPosition("furious_enemy");
//}

void KeyOnDoorStorageRoomOneDoor(string &in asItem, string &in asEntity)
{
SetSwingDoorLocked("storageroomonedoor", false, true);
PlaySoundAtEntity("", "unlock_door", "storageroomonedoor", 0, false);
RemoveItem("keytostorageroomonedoor");
}

void OnEnter()
{

}

void OnLeave()
{

}


The bolded parts are the key bits.

Is there any reason why this might not be working?
1) Delete the map cache file (Close Amnesia before doing so)
2) Check the naming of your areas and monsters. It's likely a spelling error if not the map cache.
3) Remove == true from your if statement. Your expression is basically saying if(true == true). I don't think it causes any problems but there's no harm in slimming down code -- just say if(HasItem("itemhere")) //code here
Don't know if I agree with 3).

Isn't it saying: If you have the key the enemy gets activated. Because if you don't have the key the "if statement" isn't true which means the enemy won't spawn.

Right?

EDIT: Nvm I see what you meant now
1) Alright, so how do I delete the map cache? I assume I do it in code, but I'm not sure what I type.

2) The names are right, so no problem there.

3) I remember looking at a piece of code that had the "== true" bit in it, so I assumed that's what it needed to function properly.
All you do is locate "/custom_stories/(Your storyname here)/maps" and then IF you have a file called "something".cache then delete it.

If that's not the problem then I think there's something wrong with either the names or your level editor, because I have a very similar event on my own map and it works perfectly (with almost the exact script you use).

The only other thing I can think of is, since you have "true" so that the area gets removed after you enter it the first time, I guess you maybe walk through it first WITHOUT key, and THAT'S where the function uses the "if statement" and can't find the key in your inventory (because you haven't picked it up yet) and when you enter the area a SECOND time, WITH the key, the area is gone because you already entered it once before.

Does that sound likely?
It that's the problem then I suggest you do what I did.

Just use SetEntityCallbackFunc(string& asName, string& asCallback); to your key and when you pick it up the Area will spawn SetEntityActive("Area_Name"). That way you won't activate the area when you walk through it when you don't have the key. And when you find the key and pick it up, they area spawns and the "if statement" will work.
Or you could make it really easy.
Code:
void OnStart()
{
    AddEntityCollideCallback("Player", "CollideArea", "MonsterScare", false, 1); // This will activate every time the player enters the area.
}
void MonsterScare(string &in asParent, string &in asChild,  int alState)
{
    if(HasItem("Key")) // Checks if the player has the key
    {
    // Monsterstuff goes here. SetEntityActive and whatnot.
    SetEntityActive("CollideArea", false);// This gets rid of the area, so the script will only run once.
    }

// If the player doesn't have the key, nothing happens.
}
Yeah doesn't really matter how you do it, as long as the area doesn't get removed when used once.
Sorry for the late reply >.<

I tried out Obliviator27's code and it worked like a charm. Thanks a lot :]