AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Need Help - Newbie Scripter dont expect much (https://forums.alliedmods.net/showthread.php?t=12528)

eXist 04-17-2005 13:29

Need Help - Newbie Scripter dont expect much
 
I'm working on my first plugin, and its desiged to stack multiple people on the admin that gave the command. Heres my product, it compiles fine but the problem is it just does work, plain and simple. I have a feeling its a problem with the loop more than anything, but I'm stumped. Help is much appriciated.

Also 1 more question. I'm having trouble figuring out the difference between the commands
Code:
cmd_access(id,level,cid,1)
and
Code:
cmd_access(id,level,cid,0)

Thanks for all your help eXist.

Code:
#include <amxmodx> #include <fun> #include <amxmisc> #define PLUGIN_NAME "Stack X" #define PLUGIN_VERSION "1.0" #define PLUGIN_AUTHOR "eXist`" public plugin_init()     {         register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR) register_concmd("amx_stackall", "stack_all", ADMIN_KICK, "user All Players in the server will be stacked upon the admin.") public stack_all(id,level,cid)     {         if (!cmd_access(id,level,cid,1))             {                 return PLUGIN_HANDLED             }         new players[32], origin[3], playercount, player, a         get_user_origin(id, origin, 0)         get_players(players, playercount, "a")         for (a=0; a<playercount, a++; )             {                   player = players[a]                 origin[2] = origin[2] + 50                 set_user_origin(player, origin)         }                         return PLUGIN_HANDLED }

PM 04-17-2005 14:40

There is no difference, the last parameter specifies the minimal amount of arguments, but every command has at least one argument (namely the command name itself), so 0 and 1 will have the same effect. I think.

You should learn how to indent code properly ;)

Code:
#include <amxmodx> #include <fun> #include <amxmisc> #define PLUGIN_NAME "Stack X" #define PLUGIN_VERSION "1.0" #define PLUGIN_AUTHOR "eXist`" public plugin_init() {    register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR)    register_concmd("amx_stackall", "stack_all", ADMIN_KICK, "user All Players in the server will be stacked upon the admin.") } public stack_all(id,level,cid) {    if (!cmd_access(id,level,cid,1))       return PLUGIN_HANDLED    new players[32], origin[3], playercount, player, a    get_user_origin(id, origin, 0)    get_players(players, playercount, "a")    for (a=0; a<playercount; a++)    {           player = players[a]       origin[2] += 50       set_user_origin(player, origin)    }    return PLUGIN_HANDLED }

Your problem was in the loop control structure.
You had:
Code:
for (a=0; a<playercount,a++;)

The program will do this on the for loop:
Code:
for (initializer; condition; increment) {    loop body }

1) Execute the initializer
2) Check condition; if it's false, don't continue
3) Execute the loop body
4) Execute the increment part
5) Jump to 2

In your case, it will do this:
1) Execute the initializer: set a to 0
2) Check condition:
Firstly, you'll need to know something about the comma operator:
If you write:
a,b
a will be evaluated first, and the value of the expression will be b, which will be evaluated second
So, you have this condition:
a<playercount,a++
a<playercount will be evaluated first, but it's value gets lost, so it doesn't matter
Then, a++ will be evaluated. The value of the whole a<playercount,a++ expression will be the value of a++ (as it's the right part of the comma operator). a++ increments a, but its value is still a (as opposed to ++a, which increments a, and its value is the already incremented a). So, a will be set to 1, and the value of a++ will be the original a, which is 0, so that the value of a<playercount,a++ will be also 0. 0 means false, so the loop is not continued, because the condition is false. So your loop body is never entered.

When you change it to:
Code:
for (a=0; a<playercount;a++)
it should work :D

eXist 04-17-2005 14:55

Thank you very much for the complete and thurough reply. It is working correctly now and the funny thing is I meant to put a semi colen there, but hit comma by accident and at that point I didnt know what a comma was, finally in the end I learned what is was from you post. (sorry i ramble alot)

Thanks alot eXist`

v3x 04-17-2005 15:35

Code:
cmd_access(id,level,cid,4)
amx_command <2> <3> <4>
The first is the command itself. :)

eXist 04-17-2005 16:26

Good work V3x
thanks


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

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