Jump to content
Rosso777

Multiple Item Replacements (Help)

Recommended Posts

I am trying to create a script that will automatically replace certain backpacks (_eng, _exp, _medic, etc.) with the same empty versions. I was going to try something like this:

 

if (unitBackpack Player isKindof "B_Carryall_mcamo_AAA") then {
                      _unit removeItem "B_Carryall_mcamo_AAA";
                      _unit addItem "B_Carryall_oli";
                    };

 

But I would need to do the same thing for every single backpack that has these suffixes. I am wondering if there is an eaiser way to accomplish this? I considered the idea of:

 

if (unitBackpack Player isKindof "B_Carryall_mcamo_AAA" || "B_Carryall_mcamo_AAT" || "B_Carryall_ocamo_Eng") then {...

 

But when it comes to the removeItem, I am not sure how to accomplish that without doing each individually. 

 

Am I in over my head with this one, or on the right track?

 

Share this post


Link to post
Share on other sites
if (["Repair","Medic","Exp","Eng","Ammo","AA","Mine"] findIf {_x in backpack _unit}  >-1) then {
  private _array = backpack _unit splitString "_";
  _array deleteAt (count _array -1);
  private _bpk = _array joinString "_";
  removeBackpack _unit;
  _unit addBackPack _bpk;
};

Works with vanilla backpacks

You can add "Diver"... that works for Diver explosive specialist but not for Assault diver (missing suffix _black in its backpack, so the new backpack doesn't exist with this method).

 

 

  • Like 1

Share this post


Link to post
Share on other sites

Thanks for your help, Pierre! I’m curious: 

is “>-1” correct or should it be “>=1”?  Not sure if that was a typo since I haven’t seen that before. 

Share this post


Link to post
Share on other sites

@Rosso777 findIf returns -1 if none of those elements are found, so it'd read:

> -1

As per the returned value from the wiki:

Quote

Return Value: Number - Zero-based position of the first array element for which the code evaluate to true, -1 if not found

 

Share this post


Link to post
Share on other sites
19 minutes ago, beno_83au said:

@Rosso777 findIf returns -1 if none of those elements are found, so it'd read:


> -1

As per the returned value from the wiki:

 


very helpful, I hadn’t thought about that. Thank you!

 

once I implement and test all this, I will report back. Thanks all!

  • Like 1

Share this post


Link to post
Share on other sites
23 minutes ago, Rosso777 said:

I hadn’t thought about that.

Mostly its not just thinking bout but reading bout it. Thats what the biki is for...

Share this post


Link to post
Share on other sites
5 minutes ago, sarogahtyp said:

Mostly its not just thinking bout but reading bout it. Thats what the biki is for...


you’re absolutely right. My apologies. There’s a lot to keep up with.

Share this post


Link to post
Share on other sites
1 hour ago, Rosso777 said:


you’re absolutely right. My apologies. There’s a lot to keep up with.

 

To be fair, initially I thought he was going for >= but just the experience of using the command helped. But I usually go to the wiki an embarrassing amount 😁

  • Like 2

Share this post


Link to post
Share on other sites
47 minutes ago, beno_83au said:

embarrassing amount

Not embarrassing, thats just the smarter way instead of getting much more bugs/errors which force you to read biki after

Share this post


Link to post
Share on other sites
On 8/12/2021 at 11:37 PM, pierremgi said:

if (["Repair","Medic","Exp","Eng","Ammo","AA","Mine"] findIf {_x in backpack _unit}  >-1) then {
  private _array = backpack _unit splitString "_";
  _array deleteAt (count _array -1);
  private _bpk = _array joinString "_";
  removeBackpack _unit;
  _unit addBackPack _bpk;
};

 

 


Unfortunately, I didn’t have any luck with this. I tried it in both init and initPlayerLocal. Would either of those locations not be the proper place?

Share this post


Link to post
Share on other sites

Try it in onPlayerRespawn.sqf

https://community.bistudio.com/wiki/Event_Scripts#onPlayerRespawn.sqf

 

Then you should use _newUnit instead of _unit in @pierremgi's script...

 

Also:

1 hour ago, Rosso777 said:

I didn’t have any luck with this

is not a detailed description of your problem. Hard to help therefore...

Share this post


Link to post
Share on other sites
3 hours ago, Rosso777 said:


Unfortunately, I didn’t have any luck with this. I tried it in both init and initPlayerLocal. Would either of those locations not be the proper place?

 

Just think about what you are doing and what you want! In your code, you are scripting for player and _unit...  which seems weird. We don't know (as most of cases on forum) if you script for single player or multiplayer. We don't know if this script should work at start (probably) or during game...

 

I scripted for a generic unit with a local variable _unit. It was an example for someone able to script with local variable as you already tested something with it. I thought you were not newbie on forum and your experience allowed you to understand what a local variable is.

onPlayerRespawn.sqf  is not the best solution, as miracle for embedded variables, especially if you are scripting for single player, or if you don't respawn at start in MP. And that doesn't help at all if you are scripting for all player's units.

 

If you are scripting for player(s) only (where player command is working), you can stay on   initPlayerLocal.sqf   and use player or (any declared local variable as 1st parameter)

So, just try player instead of _unit. You can also script in init.sqf but you need to wait for player to be in game:   [] spawn {waitUntil {!isNull player}; <code with player command>};

Share this post


Link to post
Share on other sites
2 minutes ago, pierremgi said:

 

Just think about what you are doing and what you want! In your code, you are scripting for player and _unit...  which seems weird. We don't know (as most of cases on forum) if you script for single player or multiplayer. We don't know if this script should work at start (probably) or during game...

 

I scripted for a generic unit with a local variable _unit. It was an example for someone able to script with local variable as you already tested something with it. I thought you were not newbie on forum and your experience allowed you to understand what a local variable is.

onPlayerRespawn.sqf  is not the best solution, as miracle for embedded variables, especially if you are scripting for single player, or if you don't respawn at start in MP. And that doesn't help at all if you are scripting for all player's units.

 

If you are scripting for player(s) only (where player command is working), you can stay on   initPlayerLocal.sqf   and use player or (any declared local variable as 1st parameter)

So, just try player instead of _unit. You can also script in init.sqf but you need to wait for player to be in game:   [] spawn {waitUntil {!isNull player}; <code with player command>};

 

Looking back I can understand why my post may be frustrating for you guys; after spending hours and hours scouring this and other forums (and yes, the biki) I understand some pieces but not all. Every time I do something new I learn more and more and I enjoy the process.

 

I will keep tinkering with it until I can figure out how to put it all together. 

 

Sorry guys.

Share this post


Link to post
Share on other sites
4 minutes ago, Rosso777 said:

 

Looking back I can understand why my post may be frustrating for you guys; after spending hours and hours scouring this and other forums (and yes, the biki) I understand some pieces but not all. Every time I do something new I learn more and more and I enjoy the process.

 

I will keep tinkering with it until I can figure out how to put it all together. 

 

Sorry guys.

No problem. Just try to explain what you want and read the basics on BIKI, especially before any script for multiplayer.

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

×