Page 1 of 1

[CS:S/CSGO] How to get list of primaries and secondaries and use able in menu?

Posted: Sun Jan 31, 2021 4:18 pm
by cssbestrpg
Hi, how i can get list of weapons primaries and secondaries and price?
So i can use them at pagedmenu

Re: [CS:S/CSGO] How to get list of primaries and secondaries and use able in menu?

Posted: Sun Jan 31, 2021 4:34 pm
by Kami
Hey, I'm not sure if this is the best way to do it but you can give it a try:

Syntax: Select all

from weapons.manager import weapon_manager

weapon_list = weapon_manager.ini['weapons']

for weapon in weapon_list:
if 'cost' in weapon_list[weapon]:
print(f"Weapon name: {weapon} | Cost: {weapon_list[weapon]['cost']}")



There are some weapons without a cost (bomb, knife) so I filter those out here or it will give a key error.

There are some more keys you can check for. You can see the Ini files for different games in addons\source-python\data\source-python\weapons

Re: [CS:S/CSGO] How to get list of primaries and secondaries and use able in menu?

Posted: Sun Jan 31, 2021 4:53 pm
by Ayuto
The other keys are already accessible using the WeaponClass instance directly. But I noticed we didn't do that for the cost and item_definition_index, so I just added those attributes as well:
https://github.com/Source-Python-Dev-Te ... a60465c4df

After that update you can access them as easy like this:

Syntax: Select all

from weapons.manager import weapon_manager

for weapon in weapon_manager.values():
print(f'{weapon.name}: {weapon.cost}')

Code: Select all

weapon_awp: 4750
weapon_g3sg1: 5000
weapon_scout: 2750
weapon_sg550: 4200
weapon_ak47: 2500
weapon_aug: 3500
weapon_famas: 2250
weapon_galil: 2000
weapon_m4a1: 3100
weapon_sg552: 3500
weapon_mac10: 1400
weapon_mp5navy: 1500
weapon_p90: 2350
weapon_tmp: 1250
weapon_ump45: 1700
weapon_m3: 1700
weapon_xm1014: 3000
weapon_m249: 5750
weapon_deagle: 650
weapon_elite: 800
weapon_fiveseven: 750
weapon_glock: 400
weapon_p228: 600
weapon_usp: 500
weapon_knife: None
weapon_hegrenade: 300
weapon_flashbang: 200
weapon_smokegrenade: 300
weapon_c4: None


See also:
http://wiki.sourcepython.com/developing ... eaponClass

Edit: Here is a quick shop example

Syntax: Select all

from weapons.manager import weapon_manager
from menus import PagedMenu, PagedOption
from players.entity import Player
from messages import SayText2
from commands.typed import TypedSayCommand

shop_menu = PagedMenu(title='Weapon Shop')

for weapon in weapon_manager.values():
if weapon.cost is None:
continue

shop_menu.append(PagedOption(
f'{weapon.basename.upper()} [{weapon.cost}$]',
weapon))


@shop_menu.register_build_callback
def build_callback(menu, index):
player = Player(index)
available_cash = player.cash
for option in menu:
option.highlight = option.selectable = available_cash >= option.value.cost

@shop_menu.register_select_callback
def select_callback(menu, index, option):
player = Player(index)
available_cash = player.cash

if available_cash >= option.value.cost:
player.cash -= option.value.cost
player.give_named_item(option.value.name)
else:
SayText2('You don\'t have enough cash.').send(index)

return menu

@TypedSayCommand('!shop')
def on_shop(info):
shop_menu.send(info.index)

Re: [CS:S/CSGO] How to get list of primaries and secondaries and use able in menu?

Posted: Sun Jan 31, 2021 5:08 pm
by satoon101
If you want to iterate over specific types of weapons (ie primary or secondary), you could also use WeaponClassIter.

Re: [CS:S/CSGO] How to get list of primaries and secondaries and use able in menu?

Posted: Sun Jan 31, 2021 5:14 pm
by cssbestrpg
Thank you for so quick answers. Kami's code works perfectly. Satoon101 i tried earlier to use WeaponClassIter, but i didn't get it work.

Re: [CS:S/CSGO] How to get list of primaries and secondaries and use able in menu?

Posted: Sun Jan 31, 2021 5:22 pm
by Kami
satoons way might actually be easier for making a buy menu (supposed you are trying that) as you can loop through the categories instead of checking for them afterwards or adding them manually. Here are some examples:

Syntax: Select all

from filters.weapons import WeaponClassIter

for weapon in WeaponClassIter('primary'):
print(f'{weapon.name}:{weapon.cost}')

for weapon in WeaponClassIter('sniper'):
print(f'{weapon.name}:{weapon.cost}')

for weapon in WeaponClassIter('pistol'):
print(f'{weapon.name}:{weapon.cost}')


The filer for WeaponClassIter should be the same as the tags you can find in the weapon ini files.

The only problem would be to choose the right tag if a weapon has both (like weapon_awp has rifle and sniper)

Re: [CS:S/CSGO] How to get list of primaries and secondaries and use able in menu?

Posted: Sun Jan 31, 2021 5:23 pm
by Ayuto
Here is a quick example for WeaponClassIter:

Syntax: Select all

from filters.weapons import WeaponClassIter

print('--- All weapons: ---')
for weapon in WeaponClassIter():
print(f'{weapon.name}: {weapon.cost}')

print('--- Primary weapons: ---')
for weapon in WeaponClassIter(is_filters='primary'):
print(f'{weapon.name}: {weapon.cost}')

print('--- Everything except primaries: ---')
for weapon in WeaponClassIter(not_filters='primary'):
print(f'{weapon.name}: {weapon.cost}')

Code: Select all

--- All weapons: ---
weapon_awp: 4750
weapon_g3sg1: 5000
weapon_scout: 2750
weapon_sg550: 4200
weapon_ak47: 2500
weapon_aug: 3500
weapon_famas: 2250
weapon_galil: 2000
weapon_m4a1: 3100
weapon_sg552: 3500
weapon_mac10: 1400
weapon_mp5navy: 1500
weapon_p90: 2350
weapon_tmp: 1250
weapon_ump45: 1700
weapon_m3: 1700
weapon_xm1014: 3000
weapon_m249: 5750
weapon_deagle: 650
weapon_elite: 800
weapon_fiveseven: 750
weapon_glock: 400
weapon_p228: 600
weapon_usp: 500
weapon_knife: None
weapon_hegrenade: 300
weapon_flashbang: 200
weapon_smokegrenade: 300
weapon_c4: None
--- Primary weapons: ---
weapon_awp: 4750
weapon_g3sg1: 5000
weapon_scout: 2750
weapon_sg550: 4200
weapon_ak47: 2500
weapon_aug: 3500
weapon_famas: 2250
weapon_galil: 2000
weapon_m4a1: 3100
weapon_sg552: 3500
weapon_mac10: 1400
weapon_mp5navy: 1500
weapon_p90: 2350
weapon_tmp: 1250
weapon_ump45: 1700
weapon_m3: 1700
weapon_xm1014: 3000
weapon_m249: 5750
--- Everything except primaries: ---
weapon_deagle: 650
weapon_elite: 800
weapon_fiveseven: 750
weapon_glock: 400
weapon_p228: 600
weapon_usp: 500
weapon_knife: None
weapon_hegrenade: 300
weapon_flashbang: 200
weapon_smokegrenade: 300
weapon_c4: None

Basically, it allows you to filter the weapon_manager based on the tags defined in our ini files.

Edit:
Damn Kami, you were faster! :D

Re: [CS:S/CSGO] How to get list of primaries and secondaries and use able in menu?

Posted: Sun Jan 31, 2021 6:20 pm
by cssbestrpg
Thank you for showing good examples of WeaponClassIter.
I managed to do my buy menu that works perfectly