Tips & Tricks
#81

Quote:
Originally Posted by dipsnark
View Post
Did you test bit-flags? BitFlag_Get macro awlays returns zero.
The comment in the first post says:

pawn Code:
// Returns zero (false) if the flag isn't set.
So you have to use BigFlag_Set first.
Reply
#82

There is no BitFlag_Set but BitFlag_On and BitFlag_Off. And I set it to on/off then used BitFlag_Get but it only returns 0.

Test this code:
Code:
g_PlayerFlags[0] = PlayerFlags:0;
	BitFlag_On(g_PlayerFlags[0], PLAYER_IS_LOGGED_IN);
	printf("___ %i", BitFlag_Get(g_PlayerFlags[0], PLAYER_IS_LOGGED_IN));
	BitFlag_Off(g_PlayerFlags[0], PLAYER_IS_LOGGED_IN);
	printf("__ %i", BitFlag_Get(g_PlayerFlags[0], PLAYER_IS_LOGGED_IN));
	printf("_S_%i", BitFlag_Get(g_PlayerFlags[0], PLAYER_HAS_GANG));
	BitFlag_On(g_PlayerFlags[0], PLAYER_HAS_GANG);
	printf("_B_%i", BitFlag_Get(g_PlayerFlags[0], PLAYER_HAS_GANG));
	BitFlag_Off(g_PlayerFlags[0], PLAYER_HAS_GANG);
	printf("_C_%i", BitFlag_Get(g_PlayerFlags[0], PLAYER_HAS_GANG));
}
It not only returns 0 but the line with prefix '_B_' returns 2. And another returns 4(can buy property).

EDIT: OMFG I'm such a noob just realised I can check if it is set or not so don't have to check it's value...
Nice tut btw.
Reply
#83

Quote:
Originally Posted by Y_Less
View Post
You can put "char" on any array where the highest value to be stored will be 255, and the lowest 0 (or -128 - 127).
Question how would you use char arrays on player variables which are created with an enum.

like:

pawn Code:
enum pInfo
{
    pName[MAX_PLAYER_NAME],
    pIP[16],
    pInfected,
    pDiedAsSurvivor,
    pMuted,
    pAdmin,
    pLastTimeHit
}
new PlayerInfo[30][pInfo];
maybe like so ? new PlayerInfo[30][pInfo char];
Reply
#84

I think you can not use Char-Array in 2-3 Dimensional array
pawn Code:
error 051: invalid subscript, use "[ ]" operators on major dimensions
but you could do this inside an enumerator ( only arrays )

pawn Code:
#if (defined MAX_PLAYERS)
    #undef MAX_PLAYERS
    #define MAX_PLAYERS (30)
#endif

enum pInfo
{
    pName[ MAX_PLAYER_NAME char]
}
new PlayerInfo[MAX_PLAYERS][pInfo];

public OnPlayerConnect(playerid)
{
    return GetPlayerName(playerid, PlayerInfo[playerid]{pName}, MAX_PLAYER_NAME);
}
Reply
#85

Very nice! This will be used for my GM.
Reply
#86

Patrick_: You can, you are just using it on the wrong dimension. However, things with the same syntax as arrays inside enums, often referred to as arrays by people, are not.
Reply
#87

Quote:
Originally Posted by Y_Less
View Post
Patrick_: You can, you are just using it on the wrong dimension. However, things with the same syntax as arrays inside enums, often referred to as arrays by people, are not.
This was posted 5 months ago Anyways thanks for pointing that out.
Reply
#88

Quote:
Originally Posted by Yashas
View Post
Its num args by the way..
right
Reply
#89

I think my post is going to get hidden in this thread.The newer replies will cover it up.

You can also do getargs using assembly by accessing each element on the stack.

For example,
Code:
#emit LOAD.S 16 //Give the location (stack) of the argument in bytes
#emit LOAD.I //Get value stored from address
#emit STOR.pri YOUR_VARIABLE_NAME //Will copy the argument to the variable
You can do lot of things with #emit
Reply
#90

If you have boolean variable like
Code:
new isEventRunning;
instead of doing this
Code:
if(isEventRunning) {
    isEventRunning = false;
    // do stuff
}
else {
    isEventRunning = true;
    // do stuff
}
you can instead do this
Code:
if((isEventRunning = !isEventRunning)) {
    // event is running
}
else {
    // event isn't running
}
or
Code:
if((isEventRunning ^= 1)) {
    // event is running
}
else {
    / event is not running
}
Reply
#91

if(BitFlag_Get(g_PlayerFlags[playerid], Spawned)) return SendClientMessage(playerid,COLOR_ERROR,"{FB0000}ER ROR: {FFFFFF}You must be alive and spawned.");

How do you check if Spawned is false?
Reply
#92

Quote:
Originally Posted by Uberanwar
View Post
if(BitFlag_Get(g_PlayerFlags[playerid], Spawned)) return SendClientMessage(playerid,COLOR_ERROR,"{FB0000}ER ROR: {FFFFFF}You must be alive and spawned.");

How do you check if Spawned is false?
if(!BitFlag_Get(g_PlayerFlags[playerid], Spawned))
Not sure about bits, but give that a try?
Reply
#93

Wow nice
Reply
#94

Is it possible to use bit flags for float, strings and other arrays too? If so, can someone show an example? It looks interesting.
Reply
#95

Quote:
Originally Posted by Sjn
View Post
Is it possible to use bit flags for float, strings and other arrays too? If so, can someone show an example? It looks interesting.
Bit flags are actually 1 integer, but each bit would indicate a different thing, 1 character of a string needs 4 byte (or 1 if packed), so no, bits are only for booleans (0 or 1) and don't make sense for others.
Reply
#96

Quote:
Originally Posted by PrO.GameR
View Post
Bit flags are actually 1 integer, but each bit would indicate a different thing, 1 character of a string needs 4 byte (or 1 if packed), so no, bits are only for booleans (0 or 1) and don't make sense for others.
Alright, that pretty much makes sense. Thanks.
Reply
#97

Quote:
Originally Posted by Logic_
Посмотреть сообщение
Sorry for the bump, but shouldn't this
PHP код:
for(new strlen(string); != EOSi--) 
be the most fastest string loop?
EOF is value 0 so you are stoping at index 1! Its 0 in c/c++.

Here, without the use of strlen:
PHP код:
for (new istring[i] != EOFi++) {

Reply
#98

Quote:
Originally Posted by Gammix
Посмотреть сообщение
EOF is value 0 so you are stoping at index 1! Its 0 in c/c++.

Here, without the use of strlen:
PHP код:
for (new istring[i] != EOFi++) {

Yep right, I mis-typed my code
Reply
#99

EOS is '\0', that is: 0. So, use the one you feel is right in the context.
Reply

I want to ask about bit flags on enum, why do you need a colon after the symbol name
PlayerFlags:(<<= 1)
Код:
enum PlayerFlags:(<<= 1) {
	// It's important that you don't forget to put "= 1" on the first flag. If you don't, all flags will be 0.
	
	PLAYER_IS_LOGGED_IN = 1,   // 0b00000000000000000000000000000001
	PLAYER_HAS_GANG,           // 0b00000000000000000000000000000010
	// ...
};

new
	// Create an array with the same tag as the enum
	PlayerFlags:g_PlayerFlags[MAX_PLAYERS]
;
Can i just omit the colon like this?:
Код:
enum PlayerFlags (<<= 1)
will it affects/changes how it works?

There are reason i want to do this, one is to know the max size of enum (or whatever it is called):
Код:
// If use this
enum PlayerFlags: (<<= 1)

// These will give errors:
printf("enum max = %i", PlayerFlags); // undefined symbol
printf("enum max = %i", PlayerFlags:); // invalid expression
printf("enum max = %i", sizeof PlayerFlags); // i know this is not correct way
But if i omit the colon:
Код:
 // No colon after symbol name
enum playerFlags (<<= 1) // i weakened/renamed the symbol name to avoid tag mismatch

// This works fine
printf("enum max = %i", playerFlags);
I could also do this to give a player "random" flags:
Код:
g_PlayerFlags[playerid] = playerFlags:random(playerFlags);
Note that i know i could just use cellmax in case i have full 32 bits in the enum.

I just want to know the difference between this and in wiki



Also i want to know that if it's possible to have the bitflags enum on packed "char-array" in case i only need 8 bits instead of 32?
Код:
enum PlayerFlags:(<<= 1) {
	// It's important that you don't forget to put "= 1" on the first flag. If you don't, all flags will be 0.

	PLAYER_BLABLA_1 = 1,			// 00000001
	PLAYER_BLABLA_2,			// 00000010
	PLAYER_BLABLA_3,			// 00000100
	PLAYER_BLABLA_4,			// 00001000
	PLAYER_BLABLA_5,			// 00010000
	PLAYER_BLABLA_6,			// 00100000
	PLAYER_BLABLA_7,			// 01000000
	PLAYER_BLABLA_8				// 10000000
};

// Create an array with the same tag as the enum (packed)
new PlayerFlags:g_PlayerFlags[MAX_PLAYERS char];

// Use case
BitFlag_On(g_PlayerFlags{playerid}, PLAYER_BLABLA_8);
Is it okay to do that? does that make differences? Of course i know it would overflow if i add more bits...
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)