SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help > Discussion

Reply
 
Thread Tools Display Modes
Old 13/12/2017, 08:33 PM   #221
Gammix
High-roller
 
Gammix's Avatar
 
Join Date: Jan 2015
Location: Canada
Posts: 1,754
Reputation: 805
Default Re: Tips & Tricks

Quote:
Originally Posted by Logic_ View Post
Sorry for the bump, but shouldn't this
PHP Code:
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 Code:
for (new istring[i] != EOFi++) {

__________________
Gammix is offline   Reply With Quote
Old 13/12/2017, 08:46 PM   #222
Logic_
High-roller
 
Logic_'s Avatar
 
Join Date: Jun 2015
Location: Oblivion
Posts: 1,485
Reputation: 263
Default Re: Tips & Tricks

Quote:
Originally Posted by Gammix View Post
EOF is value 0 so you are stoping at index 1! Its 0 in c/c++.

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

Yep right, I mis-typed my code
__________________
Logic_ is offline   Reply With Quote
Old 14/12/2017, 09:35 AM   #223
Misiur
High-roller
 
Misiur's Avatar
 
Join Date: Jul 2009
Location: Poland
Posts: 2,538
Reputation: 552
Default Re: Tips & Tricks

EOS is '\0', that is: 0. So, use the one you feel is right in the context.
Misiur is offline   Reply With Quote
Old 09/06/2018, 09:56 AM   #224
RoboN1X
Huge Clucker
 
RoboN1X's Avatar
 
Join Date: Feb 2011
Location: Indonesia
Posts: 305
Reputation: 57
Default Re: Tips & Tricks

I want to ask about bit flags on enum, why do you need a colon after the symbol name
PlayerFlags:(<<= 1)
Code:
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?:
Code:
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):
Code:
// 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:
Code:
 // 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:
Code:
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?
Code:
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...
__________________
Need Help? Use Search, WIKI, and FAQ

Last edited by RoboN1X; 09/06/2018 at 10:46 AM.
RoboN1X is offline   Reply With Quote
Old 09/06/2018, 12:17 PM   #225
Y_Less
Spam Machine
 
Y_Less's Avatar
 
Join Date: Jun 2008
Location: 629 - git.io/Y
Posts: 14,821
Reputation: 3111
Default Re: Tips & Tricks

The wiki doesn't use a colon. YSI doesn't use a colon. pawn-lang.pdf doesn't use a colon. I've literally never seen any code that uses a colon, and I'm amazed it even compiles with a colon.

Frankly, I'm not even sure why you're asking. You've found that using a colon means that the enum can't be used in many situations, and not using a colon does the same thing, so why even pursue the clearly inferior route?

I did just do some experimentation, I think this is what's happening:

PHP Code:
enum E_THING (*= 3)
{
    
E_A

That is an enum called "E_THING" that increases every value 3-fold.

PHP Code:
enum E_THING:(+= 77)
{
    
E_A

That is an anonymous enum, which means that you can't do things like `new arr[E_THING]`, because that enum doesn't exist with that name. Instead, it is the same as doing:

PHP Code:
enum (+= 77)
{
    
E_A

But with a tag override on the increment. Which is interesting, because that means that all the enum elements still have the TAG `E_THING`, but the enum called `E_THING` doesn't exist.

Edit: Actually, the tagging without being usable as array sizes has use.

Edit 2: After some discussion on discord, this also works:

PHP Code:
enum e_TAG:
{

Which is a nameless (sizeless) enum with a tag of `e_TAG`.

However, totally unrelated to this point. Your original question was about using this in arrays. If you do:

PHP Code:
enum E_NUM (<<= 1)
{
    
E_NUM_0 1,
    
E_NUM_1,
    
E_NUM_2,

Then the total size (the value of `E_NUM`) will be 8, not 3, because it is still subject to the same shifting rules.

Last edited by Y_Less; 09/06/2018 at 12:50 PM.
Y_Less is online now   Reply With Quote
Reply

Thread Tools
Display Modes

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Needs Scripting Tips! KomplettMaster General 12 26/12/2012 04:04 PM
[Tutorial] [InfiniTuts - Tutorials for beginners] Tips and Tricks regarding 'logical thinking' - Making your own script Infinity Tutorials 6 29/04/2012 11:34 PM
Motor Tricks by D12 D12Mr Screenshots and Videos 6 24/05/2009 06:04 PM


All times are GMT. The time now is 03:41 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.