Raised This Month: $32 Target: $400
 8% 

For(new i) help


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Crackhead69
Member
Join Date: Feb 2021
Old 02-14-2021 , 04:25   For(new i) help
Reply With Quote #1

Hello!
I am having a hard time understanding
Code:
for(new i;i<0;i++)
I know it is suppose to catch all the data 1 by 1 from somewhere, as example
Code:
public plugin_precache(){	
	for(new i = 0; i < sizeof(ItemsConst); i++)
		precache_***(ItemConst[i]);
}
Getting all the data from a const and then counting it in the precache function.
But is there a way to make it count 1 by 1, not all the items, but a line check after line check.
Like for example i want to make it like this:
Code:
new Const[][]{
	"number 1",
	"number 2",
	"number 3"
}

public plugin_init(){
	register_clcmd("say /count","CountNumbers")
}

public CountNumbers(id){
	for(new i;i<sizeof (Const);i++){
		client_print(id,print_chat,"The number is: %s", Const[i])
	}
}
So when player types /count, instead of ALL items to get printed, i want it first to appear Number 1 and then after he types /count again, Number 2 will apear and etc

Tried to search for answers and what not, but haven't found any, and i got a headache by trying to understand this on my own

Last edited by Crackhead69; 02-14-2021 at 04:26.
Crackhead69 is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 02-14-2021 , 05:13   Re: For(new i) help
Reply With Quote #2

You don't need a for loop at all. You need to have a counter for each player that keeps track of which message they should get when they run the command. Here is a simple example:

PHP Code:
new g_iCounter[33// Stores the counters for each of the players
new g_szStringArray[][] = {"hello""world"}

public function(
id)
{
    
// Use the string from the array based on the player's counter
    
client_print(idprint_chat"%s"g_szStringArray[g_iCounter[id]])

    
// Increment and wrap the counter for the player
    
g_iCounter[id] = ++g_iCounter[id] % sizeof(g_szStringArray)

So, the counter will cycle through each of the indices for g_szStringArray repeatedly as the function gets called. You can have as many strings in g_szStringArray as you want and you won't need to change any other code to have it still work.
__________________
fysiks is offline
Crackhead69
Member
Join Date: Feb 2021
Old 02-14-2021 , 07:49   Re: For(new i) help
Reply With Quote #3

Thank you! While it is exactly what i needed, i still have some questions.
First is, what is the use of " % " in
Code:
g_iCounter[id] = ++g_iCounter[id] % sizeof(g_szStringArray)
how does that work exactly

And also i'd like to ask if i were to Limit those options, how could i do so?
Example is if i created enum for the array
Code:
enum _:contains
{
	Name[32],
	Number
}

new g_szStringArray[][contains] = {"hello" 2}, {"world", 5}
Is there any simplier way than just checking with
Code:
new iNumber ++iNumber
 if(iNumber == g_szStringArray[1][Number])
return
for every single option, while if iNumber reaches the limit of
Code:
g_szStringArray[x][Number]
only the
Code:
(x)
option to stop working, while the remaining continue to appear

Pardon me if im not making much sense,
if needed i would try to make a functional example script to represent my desires.

Last edited by Crackhead69; 02-14-2021 at 07:50.
Crackhead69 is offline
CrazY.
Veteran Member
Join Date: May 2015
Location: SP, Brazil
Old 02-14-2021 , 08:45   Re: For(new i) help
Reply With Quote #4

Quote:
Originally Posted by Crackhead69 View Post
Thank you! While it is exactly what i needed, i still have some questions.
First is, what is the use of " % " in
Code:
g_iCounter[id] = ++g_iCounter[id] % sizeof(g_szStringArray)
how does that work exactly
That's called modulus operator. I'm not an expert on the subject so I'm just going to quote something I found in a webpage
Quote:
C++ provides the modulus operator, %, that yields the remainder after integer division. The modulus operator can be used only with integer operands. The expression x % y yields the remainder after x is divided by y. Thus, 7 % 4 yields 3 and 17 % 5 yields 2.
source: https://www.informit.com/articles/ar...5%20yields%202.

Quote:
Originally Posted by Crackhead69 View Post
And also i'd like to ask if i were to Limit those options, how could i do so?
Example is if i created enum for the array
Code:
enum _:contains
{
	Name[32],
	Number
}
Is there any simplier way than just checking with
Code:
new iNumber ++iNumber
 if(iNumber == g_szStringArray[1][Number])
return

The code fysiks provided already does that
Code:
sizeof(g_szStringArray)
sizeof as the name suggests returns the size of the specified array.
__________________









Last edited by CrazY.; 02-14-2021 at 08:46.
CrazY. is offline
Crackhead69
Member
Join Date: Feb 2021
Old 02-14-2021 , 09:08   Re: For(new i) help
Reply With Quote #5

Thank you for the reply!

the provided code from fysiks works infinitely, without anything to stop a line from the const of working (as i required).

But i also want to see if there's any simplier way than the one i have mentioned above, without having to check if each line has reached the number & then return it in order to get to the next line whom number is not yet reached.

Last edited by Crackhead69; 02-14-2021 at 09:10.
Crackhead69 is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 02-14-2021 , 09:40   Re: For(new i) help
Reply With Quote #6

Use the modulu operator which will returns the division remains.

For example 5 % 2 will return 1 since 5 has two twos and remains 1 extra.
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !


Last edited by Natsheh; 02-14-2021 at 09:44.
Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
Crackhead69
Member
Join Date: Feb 2021
Old 02-14-2021 , 12:39   Re: For(new i) help
Reply With Quote #7

Quote:
Originally Posted by Natsheh View Post
Use the modulu operator which will returns the division remains.

For example 5 % 2 will return 1 since 5 has two twos and remains 1 extra.
i have no previous nor current knowledge of bitsum things or whatever they're called,
so im havin a hard time understading it.
I have made it into a function given the code from fysiks so i'll be grateful if somebody could rewrite it as requested, since i cannot come to understand it on my own.
PHP Code:
#include <amxmodx>

enum _:Items
{
    
Name[32],
    
Number
}

new 
g_iCounter[33]        //option 1    //option 2
new g_szStringArray[][Items] ={{"hello"3}, {"world"6}}

public 
plugin_init(){
    
set_task (2.0"PrintMessage"___"b")
}

public 
PrintMessage(id){
    new 
gCounter
    
new = ++g_iCounter[id] % sizeof(g_szStringArray)
    
client_print(idprint_chat"%s"g_szStringArray[x][Name])
    
    
gCounter++
    if(
gCounter == g_szStringArray[x][Number]){
        return
// i want it to return the specific option Only whom gCounter reached
    
}
    
// then after it reached option 1, to spam only option 2 until gCounter reaches its Number


Last edited by Crackhead69; 02-14-2021 at 12:44.
Crackhead69 is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 02-14-2021 , 16:17   Re: For(new i) help
Reply With Quote #8

It's not quite clear what exactly you're requesting (because you use the term "limit" in one post and then "spam" in another which are effectively opposites). Could you please write out the sequence of messages that you want to occur. For example: "hello, hello, world, world, hello, hello" or "hello, hello, hello, world, hello hello hello, world", etc.

The modulus is not a bitwise operation and is a common question asked. Therefore, there are many articles/posts that explain how it works. For example, this one looks like it has a good explanation.

Prefixing a variable with "g" or "g_" is most often used to indicate a variable is a global variable so it's confusing when you have a variable (gCounter) that is not global.
__________________
fysiks is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 02-15-2021 , 08:55   Re: For(new i) help
Reply With Quote #9

Mostly likely global variables should be named all capital letters most recommended rather than using the g_ prefix.


The g_ prefix is a short cut for get.
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !


Last edited by Natsheh; 02-15-2021 at 08:56.
Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 02-15-2021 , 22:08   Re: For(new i) help
Reply With Quote #10

Quote:
Originally Posted by Natsheh View Post
Mostly likely global variables should be named all capital letters most recommended rather than using the g_ prefix.


The g_ prefix is a short cut for get.
Not in Pawn for AMX Mod X plugins. It's very common around here to use g_ as a prefix for global. I've never heard of using all caps for global variables. I've heard of using all caps for specific types of global variables, not all of them.
__________________
fysiks is offline
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

Forum Jump


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


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