Frictional Games Forum (read-only)
[SCRIPT] Checks whether you got an item - 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: [SCRIPT] Checks whether you got an item (/thread-10971.html)



Checks whether you got an item - flamez3 - 10-25-2011

Correct me if I'm wrong, but would it be like this:


HasItem("stone_hammer_1");
if (alState == 0)
{
GiveItemFromFile("stone_hammer_1", "stone_hammer.ent");
return;
}

I thought that would be it but it wasn't... may be im missing something huge here?

And yes I know that i need to put this in the function block


RE: Checks whether you got an item - jens - 10-25-2011

Code:
if(!HasItem("stone_hammer_1"))
{
  GiveItemFromFile("stone_hammer_1", "stone_hammer.ent");
}

should do it if placed in the correct place.



RE: Checks whether you got an item - Apjjm - 10-25-2011

Take a gander at the signature for the HasItem function:
Code:
bool HasItem(string& asName);

You may notice it returns a boolean value; That is to say a "True" or "False". If statements work by evaluating the expression between the brackets to either a "True" or "False", and executing the following code if the statement evaluated to true.

What you are doing, is you are running the HasItem function, but aren't doing anything with the result. Instead you are checking If "alState" is 0 - This variable does not hold the result of any function, it is often used as a parameter in callback functions though.

A final point is you want to determine IF the player doesn't have the item. Not if they DO have it. There are two ways you could approach this:
1) HasItem() == false
2) !HasItem()
The first may initially seem easier to read -"if the player having the item is false". However, the "!" just means "not" - and it turns True into False and vice versa. It is actually just as legible and actually a better way to write the function.
Code:
if (!HasItem("stone_hammer_1"))
{
GiveItemFromFile("stone_hammer_1", "stone_hammer.ent");
return;
}
As the if statement will try to evaluate the function "HasItem" - this in turn will return true or false and execute the following code. "alState" is not a place where functions store their result.


RE: Checks whether you got an item - flamez3 - 10-25-2011

(10-25-2011, 11:33 AM)Apjjm Wrote: Take a gander at the signature for the HasItem function:
Code:
bool HasItem(string& asName);

You may notice it returns a boolean value; That is to say a "True" or "False". If statements work by evaluating the expression between the brackets to either a "True" or "False", and executing the following code if the statement evaluated to true.

What you are doing, is you are running the HasItem function, but aren't doing anything with the result. Instead you are checking If "alState" is 0 - This variable does not hold the result of any function, it is often used as a parameter in callback functions though.

A final point is you want to determine IF the player doesn't have the item. Not if they DO have it. There are two ways you could approach this:
1) HasItem() == false
2) !HasItem()
The first may initially seem easier to read -"if the player having the item is false". However, the "!" just means "not" - and it turns True into False and vice versa. It is actually just as legible and actually a better way to write the function.
Code:
if (!HasItem("stone_hammer_1"))
{
GiveItemFromFile("stone_hammer_1", "stone_hammer.ent");
return;
}
As the if statement will try to evaluate the function "HasItem" - this in turn will return true or false and execute the following code. "alState" is not a place where functions store their result.
Thank you for that, I understand why it didn't work, and now it does Smile

+repped



(10-25-2011, 11:31 AM)jens Wrote:
Code:
if(!HasItem("stone_hammer_1"))
{
  GiveItemFromFile("stone_hammer_1", "stone_hammer.ent");
}

should do it if placed in the correct place.
Also +repped Smile


It's not working for my lantern?


RE: Checks whether you got an item - Your Computer - 10-25-2011

(10-25-2011, 12:29 PM)flamez3 Wrote: It's not working for my lantern?

Maybe the lantern gets treated the same way as tinderboxes. I haven't tested it if that's true, but an alternative method would be to use an interact callback on the lantern and set a global variable in the callback. Then instead of using HasItem to check for a lantern (assuming you're trying to check if user has a lantern in their inventory), you just check if the global variable is set.