AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   1, 2, 4, 8, 16... system (https://forums.alliedmods.net/showthread.php?t=282094)

skz 04-28-2016 15:31

1, 2, 4, 8, 16... system
 
Hello guys,

I'm creating a Knife Skins Shop for my server, and I want to save in only one variable which knifes a user have, and im trying to use the system 1, 2, 4, 8, 16 (if the user buys the first skin and the third, is value will be 5, because 1+4) but im having serious problems, because I think its hardcode to check like this:

PHP Code:

if (szKnife[id] == || szKnife[id] == || szKnife[id] == 7 etc..) 

No one knows a equation that will know which knife skins the user have, with the number in his variable?

OciXCrom 04-28-2016 15:39

Re: 1, 2, 4, 8, 16... system
 
PHP Code:

switch(szKnife[id])
{
    case 
137: { }



klippy 04-28-2016 15:40

Re: 1, 2, 4, 8, 16... system
 
PHP Code:

enum Knives (<<= 1)
{
    
Knife_First 1
    Knife_Second
,
    
Knife_Third
    Knife_Nth
};

new 
Knivesg_PlayerKnives[33]; 

To add a knife to a player:
PHP Code:

g_PlayerKnives[id] |= Knife_First

To remove it:
PHP Code:

g_PlayerKnives[id] &= ~Knife_First

And somewhere later on to check if they have a specific knife
PHP Code:

if(g_PlayerKnives[id] & Knife_First)
{
    
// They have the first knife
}
if(
g_PlayerKnives[id] & (Knife_Second Knife_Third))
{
    
// They have at least the second or third knife
}
if(
g_PlayerKnives[id] & (Knife_Second Knife_Third) == (Knife_Second Knife_Third))
{
    
// They have both the second and third knives


Search and read up on a few tutorials on bitsums and bit arithmetic around AlliedModders (or even somewhere else). There you will most likely find some useful macros to make it easier for you to deal with bitsums.

siriusmd99 04-28-2016 15:46

Re: 1, 2, 4, 8, 16... system
 
it's not correct.
You shall use & symbol, like:

if(szKnife[id] & 1 || szKnife[id] & 2 || szKnife[id] & 3)

But explain what are you trying to do...

skz 04-28-2016 15:47

Re: 1, 2, 4, 8, 16... system
 
Quote:

Originally Posted by OciXCrom (Post 2414999)
PHP Code:

switch(szKnife[id])
{
    case 
137: { }



Its the same thing as I said, but in another way...

Quote:

Originally Posted by KliPPy (Post 2415000)
PHP Code:

enum Knives (<<= 1)
{
    
Knife_First 1
    Knife_Second
,
    
Knife_Third
    Knife_Nth
};

new 
Knivesg_PlayerKnives[33]; 

To add a knife to a player:
PHP Code:

g_PlayerKnives[id] |= Knife_First

To remove it:
PHP Code:

g_PlayerKnives[id] &= ~Knife_First

And somewhere later on to check if they have a specific knife
PHP Code:

if(g_PlayerKnives[id] & Knife_First)
{
    
// They have the first knife
}
if(
g_PlayerKnives[id] & (Knife_Second Knife_Third))
{
    
// They have at least the second or third knife
}
if(
g_PlayerKnives[id] & (Knife_Second Knife_Third) == (Knife_Second Knife_Third))
{
    
// They have both the second and third knives


Search and read up on a few tutorials on bitsums and bit arithmetic around AlliedModders (or even somewhere else). There you will most likely find some useful macros to make it easier for you to deal with bitsums.

I will try your way, thanks :)

skz 04-28-2016 16:14

Re: 1, 2, 4, 8, 16... system
 
Quote:

Originally Posted by KliPPy (Post 2415000)
PHP Code:

enum Knives (<<= 1)
{
    
Knife_First 1
    Knife_Second
,
    
Knife_Third
    Knife_Nth
};

new 
Knivesg_PlayerKnives[33]; 

To add a knife to a player:
PHP Code:

g_PlayerKnives[id] |= Knife_First

To remove it:
PHP Code:

g_PlayerKnives[id] &= ~Knife_First

And somewhere later on to check if they have a specific knife
PHP Code:

if(g_PlayerKnives[id] & Knife_First)
{
    
// They have the first knife
}
if(
g_PlayerKnives[id] & (Knife_Second Knife_Third))
{
    
// They have at least the second or third knife
}
if(
g_PlayerKnives[id] & (Knife_Second Knife_Third) == (Knife_Second Knife_Third))
{
    
// They have both the second and third knives


Search and read up on a few tutorials on bitsums and bit arithmetic around AlliedModders (or even somewhere else). There you will most likely find some useful macros to make it easier for you to deal with bitsums.

g_PlayerKnives[id] |= Knife_First; that gives me an error

klippy 04-28-2016 16:39

Re: 1, 2, 4, 8, 16... system
 
Quote:

Originally Posted by skz (Post 2415014)
g_PlayerKnives[id] |= Knife_First; that gives me an error

It would be really helpful if you said what that error is. I don't get it how you people even expect help when you give no information... That happens a lot, sadly.

skz 04-28-2016 20:57

Re: 1, 2, 4, 8, 16... system
 
Sorry man, you're right!

I solved that error, but now i have another one:

PHP Code:

g_PlayerKnives[id] = SQL_ReadResult(Query4); 

warning 213: tag mismatch

klippy 04-29-2016 01:24

Re: 1, 2, 4, 8, 16... system
 
PHP Code:

g_PlayerKnives[id] = Knives:SQL_ReadResult(Query4); 

No idea why did I tag it in the first place. Anyway, any time you need to assign a value that isn't from the Knives enumeration, or a variable that's not tagged with Knives, you have to retag(cast) it to avoid the warning.

siriusmd99 04-29-2016 03:13

Re: 1, 2, 4, 8, 16... system
 
Mabe sure result is integer (*int) and not boolean, float or other type of data.
p.s. You can check its type when you create the SQL table.

And if it has wrong data type then you shall delete the table and let plugin create new one.


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

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