Frictional Games Forum (read-only)
Conditional function activation? - 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: Conditional function activation? (/thread-13978.html)



Conditional function activation? - Damascus - 03-14-2012

I'm having a lot of trouble keeping functions from activating until a certain condition is fulfilled. My first example is trying to activate a grunt after the player goes down a hallway, picks up a hammer, and comes back down the same hallway. I've tried to use an "if" statement, but every time I try, the function activates the first time you go down the hallway, and fails to work properly since you're not holding the right item. Then when you come back out, it doesn't reactivate a second time.

Here are the two scripts I've tried:
Spoiler below!
void CollideActivateLiveGrunt(string &in asParent, string &in asChild, int alState)
{
if(HasItem("stone_hammer_1"))
{
SetEntityActive("servant_grunt_1", true);
}
}

void OnStart()
{
AddEntityCollideCallback("Player", "PlayerFake", "CollideActivateLiveGrunt", true, 1);
}

Spoiler below!
void CollideActivateLiveGrunt(string &in asParent, string &in asChild, int alState)
{
SetEntityActive("servant_grunt_1", true);
}

void OnStart()
{
if(HasItem("stone_hammer_1"))
{
AddEntityCollideCallback("Player", "PlayerFake", "CollideActivateLiveGrunt", true, 1);
}
}

Another problem is having a lever display two different messages depending on whether you've used an item on it yet or not. But, as soon as you touch it, it wastes both functions before you use the item on it and won't display the second message after you use the item.



RE: Conditional function activation? - palistov - 03-14-2012

First off, you'll want to change true to false in your AddEntityCollideCallback line. The true signifies that the callback will be removed once triggered, meaning your function will run only once. Changing it to false lets the callback run each time the collision happens, so that once the player returns to the hallway with the hammer, the function will run again.

Secondly, this semantics bug is likely stemming from a map cache issue. I see nothing wrong syntactically nor semantically in your code. Try adding some debug messages to see (visually) the value of HasItem("stone_hammer_1"). Do something like AddDebugMessage("does player have hammer: "+HasItem("stone_hammer_1"), false);

Thirdly, if you want to do this a different way, you can leave true as is, and simply add the collision callback after the player picks up the hammer. This is a simple way to bypass the conditional and at the same time take advantage of the auto-remove feature of collision callbacks, so you don't have to remove it manually.

To do this, create a function called HammerPick or something similar. Next, select your hammer item in the level editor. Go to the second tab, and in the PlayerInteractCallback field write HammerPick. The function takes one parameter, a string. Then just stick the AddEntityCollideCallback line in that function, and you can totally disregard your if statement in CollideActivateLiveGrunt function.

PHP Code:
void HammerPick(string &in hammerRef)
{
//just an example to show you the parameter


Off to MW3~!


RE: Conditional function activation? - Damascus - 03-14-2012

Ah, man I feel stupid for not getting this. Both things work now! Thank you!



RE: Conditional function activation? - palistov - 03-14-2012

No problem. Don't feel stupid, it's all a learning process Smile