Frictional Games Forum (read-only)
need help: if HasItem, spawn monster - Printable Version

+- Frictional Games Forum (read-only) (https://www.frictionalgames.com/forum)
+-- Forum: Amnesia: The Dark Descent (https://www.frictionalgames.com/forum/forum-6.html)
+--- Forum: Custom Stories, TCs & Mods - Development (https://www.frictionalgames.com/forum/forum-38.html)
+---- Forum: Development Support (https://www.frictionalgames.com/forum/forum-39.html)
+---- Thread: need help: if HasItem, spawn monster (/thread-13136.html)



need help: if HasItem, spawn monster - Ghostblade - 02-06-2012

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?



RE: need help: if HasItem, spawn monster - palistov - 02-06-2012

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


RE: need help: if HasItem, spawn monster - Ninami - 02-06-2012

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


RE: need help: if HasItem, spawn monster - Ghostblade - 02-06-2012

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.


RE: need help: if HasItem, spawn monster - Ninami - 02-06-2012

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.


RE: need help: if HasItem, spawn monster - Obliviator27 - 02-06-2012

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.
}



RE: need help: if HasItem, spawn monster - Ninami - 02-06-2012

Yeah doesn't really matter how you do it, as long as the area doesn't get removed when used once.


RE: need help: if HasItem, spawn monster - Ghostblade - 02-19-2012

Sorry for the late reply >.<

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