coding question -1 reply

  • 1
  • 2

Please wait...

DiNeRo

modeler & skinner

50 XP

16th May 2008

0 Uploads

217 Posts

0 Threads

#1 8 years ago

so i was wondering, if i were to code a random number generator, how would i implement it so it runs by a chat command (ex. /random 100 would create a random number between 0 and 99) and if such a thing would be able to run along with ja+




The One and Only VIP Member

I R Scary Eyeball

47,311 XP

16th October 2006

6 Uploads

4,091 Posts

256 Threads

#2 8 years ago

Like a dice roll sort of thing? eg. "PlayerWhatever rolled 56"

In g_cmds.c, in the client command function: else if ( !Q_stricmp( cmd, "random" ) ) Cmd_Random_f( ent );

Then make a new function (just somewhere else in that file, outside of any other functions)

static void Cmd_Random_f( gentity_t *ent ) { int p; // Maximum value

if ( trap_Argc() < 2 ) {//Didn't provide enough args. trap_SendServerCommand( ent-g_entities, va( "print \"^5Command Usage: ^7random ^3Rolls a random number from 0 to max value\n\"" ) ); return; } else { p = atoi ConcatArgs( 1 );

int number = Q_irand (0, p);

trap_SendServerCommand( -1, va("cp \"%s\"", number) ); G_LogPrintf( "roll: %s: %s\n", ent->client->pers.netname, number ) }

I think that would work, I haven't actually tried it myself but that should be about right. As for working with JA+, not unless you can get hold of the source code for it and make this alteration.




eezstreet

Lord of the .DCC files

50 XP

16th June 2008

0 Uploads

2,662 Posts

0 Threads

#3 8 years ago
DiNeRo;5375509so i was wondering, if i were to code a random number generator, how would i implement it so it runs by a chat command (ex. /random 100 would create a random number between 0 and 99) and if such a thing would be able to run along with ja+

This is very easy.

It might look something like this: g_cmds.c In ClientCommand:

else if(Q_stricmp(cmd, "random")){ //compare what we've entered in the console to 'random' if(trap_Argc == 2){ //we need two arguments char buf[MAX_STRING_CHARS]; //we init a char that will store our buffer char buf2[MAX_STRING_CHARS]; //same as above trap_Argv(1, buf, sizeof(buf)); trap_Argv(2, buf2, sizeof(buf2)); int number = Q_irand(atoi(buf)), atoi(buf2)); //long story short, what this does is grab our args, convert them to an int, and do a rand() on our converted args trap_SendServerCommand(ent->s.number, va("cp \"Your number is %i\"", number)); //again, long story short, this draws the text to the center of the screen } }




DiNeRo

modeler & skinner

50 XP

16th May 2008

0 Uploads

217 Posts

0 Threads

#4 8 years ago

The One and Only;5375540Like a dice roll sort of thing? eg. "PlayerWhatever rolled 56"

In g_cmds.c, in the client command function: else if ( !Q_stricmp( cmd, "random" ) ) Cmd_Random_f( ent ); Then make a new function (just somewhere else in that file, outside of any other functions)

static void Cmd_Random_f( gentity_t *ent ) { int *p; // Maximum value

if ( trap_Argc() < 2 ) {//Didn't provide enough args. trap_SendServerCommand( ent-g_entities, va( "print \"^5Command Usage: ^7random ^3Rolls a random number from 0 to max value\n\"" ) ); return; } else { int number = Q_irand (0, p);

trap_SendServerCommand( -1, va("cp \"%s\"", number) ); G_LogPrintf( "roll: %s: %s\n", ent->client->pers.netname, number ) } I think that would work, I haven't actually tried it myself but that should be about right. As for working with JA+, not unless you can get hold of the source code for it and make this alteration.

i see, thanks a lot :D




The One and Only VIP Member

I R Scary Eyeball

47,311 XP

16th October 2006

6 Uploads

4,091 Posts

256 Threads

#5 8 years ago

Just edited that, check it again =P

Edit: And infact, again...




eezstreet

Lord of the .DCC files

50 XP

16th June 2008

0 Uploads

2,662 Posts

0 Threads

#6 8 years ago
DiNeRo;5375546i see, thanks a lot :D

If you have anymore coding questions, join the JACoders Community: JA Coders

And if you need on-the-spot coding help, join our IRC: server: irc.arloria.net channel: #JACoders

Right now we have about 8 people on, our peak is 12, but on average we have 6-10 people on the channel.




DiNeRo

modeler & skinner

50 XP

16th May 2008

0 Uploads

217 Posts

0 Threads

#7 8 years ago

eezstreet;5375551If you have anymore coding questions, join the JACoders Community: JA Coders

And if you need on-the-spot coding help, join our IRC: server: irc.arloria.net channel: #JACoders

Right now we have about 8 people on, our peak is 12, but on average we have 6-10 people on the channel.

i might do so, once i learn c++ a tad better than i do now lol




The One and Only VIP Member

I R Scary Eyeball

47,311 XP

16th October 2006

6 Uploads

4,091 Posts

256 Threads

#8 8 years ago
DiNeRo;5375558i might do so, once i learn c++ a tad better than i do now lol

That won't help, learn C :)




Didz

Ten seconds flat.

50 XP

3rd April 2009

0 Uploads

288 Posts

0 Threads

#9 8 years ago

If you want this to run along JA+, you can make the code client-side by adding a new command to cg_consolecmds.c instead.

You can't use the JA+ client-side plugin along with your own client-side plugin though.




Crazy Assassin

Overlord of Various Things

50 XP

25th August 2008

0 Uploads

428 Posts

0 Threads

#10 8 years ago

If you want to run this along side another mod, it might help if you sent a feature-request to BobaFett, who could then add a random number command to UU when he goes back to working on it.




  • 1
  • 2