AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Two Dimensional Array (https://forums.alliedmods.net/showthread.php?t=168013)

OvidiuS 09-23-2011 10:18

Two Dimensional Array
 
Code:

new g_playername[33][33]

public client_putinserver(id)
{
        if(is_user_bot(id) || is_user_hltv(id))
                return;
               
        set_task(5.0, "LoadDataClient", id);
}

public client_disconnect(id)
        g_playername[id][0] = 0;

public LoadDataClient(id)
{
        if(!is_user_connected(id))
                return;

        get_user_name(id, g_playername[id], charsmax(g_playername))
        set_task(1.0, "ForwardCheck", id)
}

public ForwardCheck(id)
{       
        client_cmd(id, "name ^"%s^"", g_playername[id])
        g_playername[id][0] = 0
}

this is simple example, and my question is:
do i need to use two dimensional array? :S

hleV 09-23-2011 10:22

Re: Two Dimensional Array
 
You could pass the name to task.
PHP Code:

new name[32];
get_user_name(idnamecharsmax(name));
set_task(1.0"ForwardCheck"idnamesizeof name); 

PHP Code:

public ForwardCheck(name[], id)
    
client_cmd(id"name ^"%s^""name); 


OvidiuS 09-23-2011 10:23

Re: Two Dimensional Array
 
Quote:

Originally Posted by hleV (Post 1561431)
You could pass the name to task.
PHP Code:

new name[32];
get_user_name(idnamecharsmax(name));
set_task(1.0"ForwardCheck"idnamesizeof name); 

PHP Code:

public ForwardCheck(name[], id)
    
client_cmd(id"name ^"%s^""name); 


i didn't know that is possible, thanks a lot :)

fysiks 09-23-2011 12:15

Re: Two Dimensional Array
 
Or just get the name only when you need it. Also, use set_user_info(id, "name", namvarhere). Also, your code makes no sense.

OvidiuS 09-23-2011 12:18

Re: Two Dimensional Array
 
Can i pass 2 arguments in set_task and how?
fysiks it's poor example and not fully written, just a part xD

what if name changes while doing the function? i need to restore it.

Bugsy 09-23-2011 12:23

Re: Two Dimensional Array
 
Yes, you can pack as much data as you need in the array.

new iData[ 35 ];

iData[ 0 ] = 55; //random integer
iData[ 1 ] = 99; //random integer
get_user_name( id , iData[ 2 ] , charsmax( iData ) - 2 );

You can also do multiple strings but you need to space them properly in the array so they can be retrieved in the called function.

That's just an example, if you can't figure it out show exactly what data you want passed to the function.

Edit: If you are worried about data changing between when set_task() is called and the task function you should just use global arrays or monitor the change and remove_task() then set_task() with corrected data.

OvidiuS 09-23-2011 12:39

Re: Two Dimensional Array
 
Quote:

Originally Posted by Bugsy (Post 1561500)
Yes, you can pack as much data as you need in the array.

new iData[ 35 ];

iData[ 0 ] = 55; //random integer
iData[ 1 ] = 99; //random integer
get_user_name( id , iData[ 2 ] , charsmax( iData ) - 2 );

You can also do multiple strings but you need to space them properly in the array so they can be retrieved in the called function.

That's just an example, if you can't figure it out show exactly what data you want passed to the function.

Edit: If you are worried about data changing between when set_task() is called and the task function you should just use global arrays or monitor the change and remove_task() then set_task() with corrected data.

that's what i'm worried about, how can i be sure that data won't change?
Only way is to pass it with the task, or use global array.
I can use global array, but like in the case i posted above, it need's to be two dimensional (or does it?), and i think that is wasting of resource, for such a task.

edit: by the way thanks everyone for helping :)

Bugsy 09-23-2011 12:57

Re: Two Dimensional Array
 
Your first code looks ok (youre already using a 2d array), should be charsmax( g_playername[] ) though.

You will need to hook name changes and update g_playername[] if name gets changed or just get name in the task function.

OvidiuS 09-23-2011 13:04

Re: Two Dimensional Array
 
Quote:

Originally Posted by Bugsy (Post 1561523)
Your first code looks ok (youre already using a 2d array), should be charsmax( g_playername[] ) though.

You will need to hook name changes and update g_playername[] if name gets changed or just get name in the task function.

i decided to pass arguments in set_task, i managed to do it, thank you :)


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

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