Raised This Month: $51 Target: $400
 12% 

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


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
skz
Senior Member
Join Date: Jul 2014
Location: Portugal
Old 04-28-2016 , 15:31   1, 2, 4, 8, 16... system
Reply With Quote #1

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?
__________________
skz is offline
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 04-28-2016 , 15:39   Re: 1, 2, 4, 8, 16... system
Reply With Quote #2

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

OciXCrom is offline
Send a message via Skype™ to OciXCrom
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 04-28-2016 , 15:40   Re: 1, 2, 4, 8, 16... system
Reply With Quote #3

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.
klippy is offline
siriusmd99
Veteran Member
Join Date: Oct 2013
Location: Republic of Moldova
Old 04-28-2016 , 15:46   Re: 1, 2, 4, 8, 16... system
Reply With Quote #4

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...
siriusmd99 is offline
skz
Senior Member
Join Date: Jul 2014
Location: Portugal
Old 04-28-2016 , 15:47   Re: 1, 2, 4, 8, 16... system
Reply With Quote #5

Quote:
Originally Posted by OciXCrom View Post
PHP Code:
switch(szKnife[id])
{
    case 
137: { }

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

Quote:
Originally Posted by KliPPy View Post
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
__________________

Last edited by skz; 04-28-2016 at 15:47.
skz is offline
skz
Senior Member
Join Date: Jul 2014
Location: Portugal
Old 04-28-2016 , 16:14   Re: 1, 2, 4, 8, 16... system
Reply With Quote #6

Quote:
Originally Posted by KliPPy View Post
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
__________________
skz is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 04-28-2016 , 16:39   Re: 1, 2, 4, 8, 16... system
Reply With Quote #7

Quote:
Originally Posted by skz View Post
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.

Last edited by klippy; 04-28-2016 at 16:39.
klippy is offline
skz
Senior Member
Join Date: Jul 2014
Location: Portugal
Old 04-28-2016 , 20:57   Re: 1, 2, 4, 8, 16... system
Reply With Quote #8

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
__________________

Last edited by skz; 04-28-2016 at 21:07.
skz is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 04-29-2016 , 01:24   Re: 1, 2, 4, 8, 16... system
Reply With Quote #9

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.
klippy is offline
siriusmd99
Veteran Member
Join Date: Oct 2013
Location: Republic of Moldova
Old 04-29-2016 , 03:13   Re: 1, 2, 4, 8, 16... system
Reply With Quote #10

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.

Last edited by siriusmd99; 04-29-2016 at 03:16.
siriusmd99 is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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