AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Help with cs_get_weapon_id (https://forums.alliedmods.net/showthread.php?t=131386)

Jelle 07-04-2010 15:13

Help with cs_get_weapon_id
 
I want to know the weapon a guy is holding, and set a string to a value which is what the gun max can hold in one mag. I did it this way:

PHP Code:

new ca
            
            
//find what weapon type the player has
            
switch(cs_get_weapon_id(player))
            {
            case 
CSW_P228 ca 13;
            case 
CSW_SCOUT ca 10;
            case 
CSW_HEGRENADE ca 0;
            case 
CSW_XM1014 ca 7;
            case 
CSW_C4 ca 0;
            case 
CSW_MAC10 ca 30;
            case 
CSW_AUG ca 30;
            case 
CSW_SMOKEGRENADE ca 0;
            case 
CSW_ELITE ca 15;
            case 
CSW_FIVESEVEN ca 20;
            case 
CSW_UMP45 ca 25;
            case 
CSW_SG550 ca 30;
            case 
CSW_GALI ca 35;
            case 
CSW_FAMAS ca 25;
            case 
CSW_USP ca 12;
            case 
CSW_GLOCK18 ca 20;
            case 
CSW_AWP ca 10;
            case 
CSW_MP5NAVY ca 30;
            case 
CSW_M249 ca 100;
            case 
CSW_M3 ca 8;
            case 
CSW_M4A1 ca 30;
            case 
CSW_TMP ca 30;
            case 
CSW_G3SG1 ca 20;
            case 
CSW_FLASHBANG ca 0;
            case 
CSW_DEAGLEca 7;
            case 
CSW_SG552 ca 30;
            case 
CSW_AK47 ca 30;
            case 
CSW_P90 ca 50;
            
            } 

Then I was looking at the includes on how to actually use the native, as of what I heard, it would just give out the CSW_* type, but I can see now, that it is not, but it gives a number. Now here comes my question. First the amxconst.inc:

Code:

/* Id of weapons in CS */
#define CSW_P228        1
#define CSW_SCOUT        3
#define CSW_HEGRENADE        4
#define CSW_XM1014        5
#define CSW_C4            6
#define CSW_MAC10        7
#define CSW_AUG            8
#define CSW_SMOKEGRENADE    9
#define CSW_ELITE        10
#define CSW_FIVESEVEN        11
#define CSW_UMP45        12
#define CSW_SG550        13
#define CSW_GALI        14
#define CSW_GALIL        14
#define CSW_FAMAS        15
#define CSW_USP            16
#define CSW_GLOCK18        17
#define CSW_AWP            18
#define CSW_MP5NAVY        19
#define CSW_M249        20
#define CSW_M3            21
#define CSW_M4A1        22
#define CSW_TMP            23
#define CSW_G3SG1        24
#define CSW_FLASHBANG        25
#define CSW_DEAGLE        26
#define CSW_SG552        27
#define CSW_AK47        28
#define CSW_KNIFE        29
#define CSW_P90            30
#define CSW_VEST        31
#define CSW_VESTHELM    32

I am just curious here. Is there no #2 weapon, or is it just my include which is outdated?

hleV 07-04-2010 15:27

Re: Help with cs_get_weapon_id
 
Use get_user_weapon() in this case.

Jelle 07-04-2010 16:08

Re: Help with cs_get_weapon_id
 
Awesome, it works! Thank you!

wrecked_ 07-05-2010 01:40

Re: Help with cs_get_weapon_id
 
cs_get_weapon_id() is used with the weapon entity as the first and only parameter. In your case, you were passing the player's id. Just an explanation so you'd learn from this.

DarkGod 07-06-2010 10:53

Re: Help with cs_get_weapon_id
 
Quote:

Originally Posted by Jelle (Post 1228294)
I am just curious here. Is there no #2 weapon, or is it just my include which is outdated?

Shield?

Bugsy 07-06-2010 16:47

Re: Help with cs_get_weapon_id
 
Instead of using a switch you could create an array, having an element for each weapon index.
Just be sure to add a blank/zero value as a place holder if there is no weapon for a given index.
PHP Code:

new const GunMax31 ] =
{
     
0,
     
13,
     
0,
     
10,
     
0
// etc for all weapons


Then access value

GunMax[ get_user_weapon( id ) ]

If you choose to do this, it is a good idea to check the value returned by get_user_weapon prior to passing it into the array.

Jelle 07-06-2010 19:02

Re: Help with cs_get_weapon_id
 
Quote:

Originally Posted by DarkGod (Post 1229843)
Shield?

It does not say so in amxconst.inc, but thank you.

Quote:

Originally Posted by wrecked_ (Post 1228736)
cs_get_weapon_id() is used with the weapon entity as the first and only parameter. In your case, you were passing the player's id. Just an explanation so you'd learn from this.

I see now. Thank you.

Quote:

Originally Posted by Bugsy (Post 1230217)
Instead of using a switch you could create an array, having an element for each weapon index.
Just be sure to add a blank/zero value as a place holder if there is no weapon for a given index.
PHP Code:

new const GunMax31 ] =
{
     
0,
     
13,
     
0,
     
10,
     
0
// etc for all weapons


Then access value

GunMax[ get_user_weapon( id ) ]

If you choose to do this, it is a good idea to check the value returned by get_user_weapon prior to passing it into the array.

I believe get_user_weapon() returns the CSW_* value of the gun the index is holding, so I do not quite understand what you are trying to explain me here.
I think it was cs_get_user_weapon() which gave a number.

But why add a 0 after each weapon? And I do not quite follow you on how to store max clips and such. I rarely use arrays. Is it any faster to do it that way, and not with a switch?

Bugsy 07-06-2010 23:26

Re: Help with cs_get_weapon_id
 
Quote:

Originally Posted by Jelle (Post 1230345)
I believe get_user_weapon() returns the CSW_* value of the gun the index is holding, so I do not quite understand what you are trying to explain me here.
I think it was cs_get_user_weapon() which gave a number.

But why add a 0 after each weapon? And I do not quite follow you on how to store max clips and such. I rarely use arrays. Is it any faster to do it that way, and not with a switch?

Add this to your bookmarks: http://www.amxmodx.org/funcwiki.php

cs_get_weapon_id( weapon_entity_index ) returns the CSW_ constant of the weapon entity index passed to it.

get_user_weapon( player_index ) returns the CSW_ constant of the weapon a player is holding.

Both of these return the same thing (a number / CSW_ constant). The difference is what you pass to the function as a parameter.

What you should be using is get_user_weapon() since you are passing a player-id.

The array method I was showing you will allow you to not use switch statement which will make your code more efficient.

Since there are no weapons with an index (CSW_ value) of 0 or 2, we need to put placeholders in these locations so the remainder of the array will stay in order (this can be any number since it will never be used). Maybe this code will be easier to understand...you can continue for the remaining weapons, there are no other 'empty slots'.
PHP Code:

new const GunMax31 ] =
{
     
0//0 - empty slot
     
13//1 - P228
     
0//2 - empty slot
     
10//3 - SCOUT
     
0//4 - HEGRENADE
     
7//5 - XM1014
     
0//6 - C4
     
30//7 - MAC10
     
30//8 - AUG
     
0//9 - SMOKEGRENADE
     
15//10 - ELITE
// etc for all weapons


Just be sure the weapons are all in order identical to the CSW constants.
Code:

/* Id of weapons in CS */
#define CSW_P228        1
#define CSW_SCOUT        3
#define CSW_HEGRENADE        4
#define CSW_XM1014        5
#define CSW_C4            6
...


ConnorMcLeod 07-07-2010 00:22

Re: Help with cs_get_weapon_id
 
2 doesn't exists AFAIK.

Jelle 07-07-2010 07:11

Re: Help with cs_get_weapon_id
 
Oh I see what you mean bugsy. What I do not get, is after making the array, how do I look thou it? I was looking at the function reference on amxmodx.org for get_user_weapon, and I still have no idea how you want me to use the array.

Quote:

Originally Posted by ConnorMcLeod (Post 1230589)
2 doesn't exists AFAIK.

DarkGod just wrote it is the shield.


All times are GMT -4. The time now is 07:07.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.