How to return multiple values and apply conditionals for all possible combinations?
My plugin needs to check a survivor's primary weapon, if they have one, for any upgrades (laser, incendiary ammo or explosive ammo) and then decide if an upgrade should be given or not.
Peace-Maker posted some code that I modified to find the current upgrades, if any, and return a value that corresponds to the appropriate cvar values for the same upgrades. There are two challenges at this point: 1. The GetUpgradeType function successfully determines the client's primary weapon upgrades, but it needs to return multiple values and I'm not sure how best to handle it. 2. Because there are five unique non-repeating combinations of possible upgrades (incendiary and explosive ammo are mutually exclusive) the logic to check them for regular and vip players gets a little complicated. The code below works for one upgrade, but not more. I understand why but am not sure where to go from here. Seems like an array is needed but my array experience is limited: Note: Survivors are handled individually based on player_spawn event, so this codes runs for one client at a time. PHP Code:
PG |
Re: How to return multiple values and apply conditionals for all possible combination
The only problem i could see:
PHP Code:
PHP Code:
|
Re: How to return multiple values and apply conditionals for all possible combination
A bitvec / bitflag is basically an array of boolean values, so you might as well get the contents of m_upgradeBitVec and check your requirements against it; something like the following (untested):
Code:
// contains bitflag of upgrades (m_upgradeBitVec) |
Re: How to return multiple values and apply conditionals for all possible combination
I don’t have enough time on me these days to poke around, but you can hook these functions straight out of binary. Check out backpack plugin and the forwards I posted there.
|
Re: How to return multiple values and apply conditionals for all possible combination
Return an array if you need to return multiple integers. Return a data pack handle if what you return is a combo of chars ( strings ) and other stuff like float or ints. Check how cloning handles works on Google.
|
Re: How to return multiple values and apply conditionals for all possible combination
Thanks so much for all of the help and code examples. Once I recognized it was unnecessary to return multiple values from the GetUpgrdType function, I adapted nosoop's example code. Here's the updated version:
PHP Code:
PHP Code:
PHP Code:
Please let me know if you see any mistakes or opportunities for improvement. Thanks again for all of the help! PG |
Re: How to return multiple values and apply conditionals for all possible combination
You can save an indentation level by stripping the surrounding else { /* ... */ } nesting since you're returning from the function early anyways.
There's also the possibility of or-ing the UpgdChoiceBitVec and !bHasAmmoUpgrd result together, and storing the result of UpgdChoiceBitVec may help with readability so it's less characters to read. Since all the decision making just determines if SelectUpgrd(client, bIsVip); is called or not, you could reduce it down to a couple of boolean variables to check against (even down to a single one if you want to sacrifice readability). I wouldn't worry about it too much as long as the code works; you'll pick up these things as you learn. |
Re: How to return multiple values and apply conditionals for all possible combination
Quote:
Thank you for those suggestions. Now that you've pointed them out they seem obvious :oops: After making those changes in this section, I went through the rest of the item selection code and made similar improvements in several other sections. It really simplified the nested conditionals and was a good exercise in hunting redundancy hunting (lol). I think further consolidation could be done, but for the sake of readability and debugging, I'll stop here. Some of the if statements are getting rather long. Here's the updated code: PHP Code:
PG |
Re: How to return multiple values and apply conditionals for all possible combination
Don't forget to remember the upgrade values for your weapons.
I saw a plugin that let you drop your weapon, but didn't remember the upgrades, and that's really frustrating when you're trying to drop it for a teammate. |
All times are GMT -4. The time now is 17:34. |
Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.