Jump to content
Sign in to follow this  
laxemann

Smoother way to check if a player owns an item? (For addon creation)

Recommended Posts

Heya,

we're currently working on an addon which adds a new feature to the game.

The main functions of the addon or better the addon itself can be opened via the action menu (addAction, rather simple).

Now what we want to do is rather simple:

Only make the functions avaiable in case the corresponding item is in the player's inventory.

Our current solution looks like this:

[] spawn
{
       blabla code
while {alive player} do {
	[b][color="#FF0000"]waituntil {sleep 1; "ITEM" in items player};[/color][/b]

	Addaction1 = player addActionblablabla;

               [...more code of course...]

	[color="#FF0000"][b]waituntil {sleep 1; !("ITEM" in items player)};[/b][/color]

	player removeAction Addaction1;
};
};

So basically it really is just a loop that checks every second if the item is in the player's inventory and then adds the action if this is the case,

then proceeds and waits untill the item is removed again, removes the action, repeats... you get the idea.

However I asked myself if there's a better, more elegant and mainly more reliable way to check if the item

is in the player's inventory?

Thanks guys,

Laxe

Share this post


Link to post
Share on other sites

Use this to check for the item, best placed directly before the addAction command.

_itemOk = false;
_itemToLookFor = "putItemclassHere";
if (_itemToLookFor in (uniformItems player)) then {
_itemOk = true;
}else{
if (_itemToLookFor in (vestItems player)) then {
	_itemOk = true;
}else{
	if (_itemToLookFor in (backpackItems player)) then {
		_itemOk = true;
	};
};
};

and as condition param for the addAction use

"_itemOk"

greetings Na_Palm

Edited by Na_Palm

Share this post


Link to post
Share on other sites

Hey Na_Palm,

I get your point but I'm afraid that's not what we're looking for, my text probably was missleading.

Your example will a) only check if the item is in the player's inventory once and b) implementing it results in pretty much the same as I did... but it'd actually be just more complex as in items player is sufficient,

we don't need a differentiation between vest, uniform and backpackitems... or did you do this in order to save performance since ArmA will stop checking if one condition is fulfilled?

Thanks for your effort anyway!

Greetings!

Share this post


Link to post
Share on other sites

oh, i read your code false it seems....

didn't realised that this was "real" code of you and interpreted "in items" as text. maybe a bit late here on my end :)

In that light i would use "'ITEM' in items player" as condition (parameter #8) and separate the addAction in a new file. Then call it once on player init and if needed once on respawn.

The condition will be checked automatically on every frame, no need to further micromanage it.

Share this post


Link to post
Share on other sites

Haha no worries. :)

Hm, isn't that what we're doing by using the waitUntil command?

We're actually using the sleep function intentional since there's no need to check if the player owns the item that often and it saves resources...

Share this post


Link to post
Share on other sites

Is this "sleep" delay nesessary for the waitUntil command? I know that it checks condition every frame but I read that it doesn't affect perfomance much.

Share this post


Link to post
Share on other sites

To, solely my expirience: if you have to worry about performance and therefore use the waitUntil{sleep 1;...} plus micromanaging a simple addAction including an "light" condition... you may have then more problems on other ends than this command.

Please don't take these words harder as they are meant. In the end you can always throw the code to/in BIS_fnc_codePerformance and check what takes less resources.

Share this post


Link to post
Share on other sites

Hey buddy,

nono, I understand your point. :)

We're not facing any performance problems and it works like a charm in the current form, however I thought that there's an even easier/more "official" way to check if an

item is inside a player's inventory.

If this isn't really the case, I'd be more than happy since, as mentioned, everything works.

I honestly don't know how necessary the sleep command is, I might actually check

the code performance with and without.

Anyway, thanks for your suggestions,

and I'll let you know once the mod

is out. :)

Share this post


Link to post
Share on other sites

You could use a "Take" EventHandler that checks if the given Item is the one you need.

Something like that:

config.cpp

class EventHandlers {
   take = "_this call myTag_fnc_checkMyItemFunction";
};

this EH class is part of your unit class.

fn_checkMyItemFunction.sqf

checkMyItemFunction =
{
   private["_unit","_item"];

   _unit = _this select 0;
   _item = _this select 2;

   if(_item == "myClassname") then {
       [_unit, _item] call WhateverFunctionYouWant;
   };
};

How you can add functions to your functionlibrary via addon is described here.

I think this will be the cleanest method for doing this.

Edited by Lappihuan

Share this post


Link to post
Share on other sites

Addaction takes a condition, just enter

"itemName" in items player

There. No need to loop

Share this post


Link to post
Share on other sites

Hey guys, I have kinda unrelated question.

Why this doesn't work:

_tcb_ais_agony = player getVariable "tcb_ais_agony"; // Boolean value
waitUntil {_tcb_ais_agony};
//code

But this does:

_tcb_ais_agony = player getVariable "tcb_ais_agony"; // Boolean value
if (_tcb_ais_agony) then {
//code
};

Any ideas?

edit: I "spawn" this code

Edited by Champ-1

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×