Frictional Games Forum (read-only)
Only call a function when 2 condictions are fulfilled - 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: Only call a function when 2 condictions are fulfilled (/thread-20297.html)



Only call a function when 2 condictions are fulfilled - tonitoni1998 - 02-12-2013

i want to show a messge when the player is standing inside of an are and is looking at an entity.


RE: Only call a function when 2 condictions are fulfilled - MulleDK19 - 02-12-2013

Add a collide callback for the area, and a look callback for the entity.

When the player enters the area, set a variable to true. When he leaves, set the same variable to false.
When the player looks at the entity, set another variable to true. When he stops looking, set the same variable to false.

In both functions, call a third function like: void DoStuffIfPlayerIsInAreaAndLookingAtEntity()
where you check the 2 variables.


RE: Only call a function when 2 condictions are fulfilled - tonitoni1998 - 02-12-2013

im sorry, im a newbie to coding. i dont know wich variable you mean. i only know how to set a collide callback to enter, leave and both. and im not sure how to change that look at and look away. can you please give me a kickstart?


RE: Only call a function when 2 condictions are fulfilled - palistov - 02-12-2013

The most common condition checking mechanism is the 'if' statement.

An example would be

if(yourCondition1 && yourCondition2) {
// Do some cool code here!
} else {
// This code runs only if the conjuctive expression
// (yourCondition1 && yourCondition2) evaluates to false!
}

The && symbol is a boolean operator 'and'. It basically means for the condition to be met, both yourCondition1 and yourCondition2 must be met.

You can describe your conditions many ways and you can do the condition checking in multiple places. You can do it before calling the function, or you can do it inside the function, and simply don't run any code unless the condition is met.

Anyways you need to describe your conditions as boolean values, true or false. To expand on MulleDK's example:

Goal:
If the player is in the area 'MyArea' and is looking at area 'DoorArea', give him a lantern.

(We'll assume that the global variable 'IsInMyArea' is a boolean variable that changes when the player enters or exits 'MyArea', and the global variable 'IsLookingAtDoorArea' represents whether the player is looking at the door or not.)

void LanternGift() {
if(IsInMyArea && IsLookingAtDoor) {
GivePlayerItemFromFile("LanternGift", "lantern.ent");
} else {
// No gift for you!
}
}

This is a primitive example and requires you manually change the global variables to keep them up to date, so that whenever you evaluate them they are accurate. You would change the IsInMyArea variable in a callback function to area callbacks, which you said you know how to do. The same goes for the IsLookingAtDoor variable, except with a SetEntityPlayerLookAt callback. Check out the wiki page for more info on that function. Good luck.