Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[TUTORIAL] How to create a command - Xonotic QuakeC programming

#1
Hi, all! Big Grin
This is my first tutorial created, it talks about how to create commands and this is the first time that I develop the code:
How I started to write my first code, the used notepad is Code::Blocks, it can be opened in another notepad (I don't recommend using a notepad like Windows default notepad):
IMPORTANT NOTE: as it's server side, keep in mind that it only works for your own server and not for other servers(Internet).

1. Open a file called cmd.qc in xonotic/data/xonotic-data.pk3dir/qcsrc/server/command folder

2. Write the following in the line 281 (NOTE: This line is specific, if you see something not empty, don't do. The code may change in some time due to the development, so don't write inside the functions, write it outside the functions)
Code:
//LegendGuard first command code test 2020/04/22 during the coronavirus pandemic crisis
void ClientCommand_hello(entity caller, int request)
{
   switch (request)
   {
       case CMD_REQUEST_COMMAND:
       {
           sprint(caller, "\nHELLO BUDDY IM TALKING TO YOU FROM QC.\n");
           return;  // never fall through to usage
       }

       default:
       case CMD_REQUEST_USAGE:
       {
           sprint(caller, "\nUsage:^3 cmd hello\n");
           sprint(caller, "  Where 'hellomessage' is the string of text to say the hello text.\n");
           return;
       }
   }
}


3. Write the following macro to call the command function in the line 860 more or less where says "Macro system for networked commands":
Code:
CLIENT_COMMAND("hello", ClientCommand_hello(ent, request), "Print a message of hello from qc lol") \


4. Save the file cmd.qc and go to console MSYS2 (as I use Windows)


5. In the console, write and execute ./all compile -qc -r


6. After, run the game with ./all run


7. Execute map any_mapname command in the game (NOTE: alternatively you can go in Multiplayer >> Create >> Select a map >> Start multiplayer! , you're creating a local server)


8. When the map is loaded in the game, run the following created command: cmd hello 
IMPORTANT NOTE: if you don't want to write cmd and you want to execute the hello command directly, then do this: go to and open xonotic/data/xonotic-data.pk3dir/commands.cfg, go to where says "cmd (client-to-server command) - server/command/cmd.qc" and qc_cmd_cmd things, inside of those aliases, write a new line inside those aliases:
Code:
alias hello "qc_cmd_cmd hello ${* ?}" // Prints a hello message in the console
you can organize a bit your line to be like the other aliases, after save the commands.cfg


9. And enjoy!!  Cool

cmd hello
[Image: unknown.png]


hello
[Image: unknown.png]
Believing is power
Reply

#2
Thank you. Hopefully this will inspire more folks to join the force.
Reply

#3
(05-27-2020, 03:55 AM)BuddyFriendGuy Wrote: Thank you. Hopefully this will inspire more folks to join the force.

I didn't see this answer, but anyways thank you! Big Grin
Believing is power
Reply

#4
More tutorial for this!!!
In this section, it will show you how to develop a Basic Calculator command:
1. Open a file called cmd.qc in xonotic/data/xonotic-data.pk3dir/qcsrc/server/command folder

2. Write the following in the line 103 (NOTE: This line is specific, if you see something not empty, don't do. The code may change in some time due to the development, so don't write inside the functions, write it outside the functions)
Code:
//LegendGuard calc command code, enjoy the calculator command!
void ClientCommand_calc(entity caller, int request, int argc)
{
  switch (request)
  {
      case CMD_REQUEST_COMMAND:
      {
          if (argv(1) != "")
          {
              if (argv(1) != "+" && argv(1) != "-" && argv(1) != "*" && argv(1) != "/" && argv(1) != "^" && argv(1) != "%")
              {
                  if (argv(2) == "+" || argv(2) == "-" || argv(2) == "*" || argv(2) == "/" || argv(2) == "^" || argv(2) == "%")
                  {
                      if (argv(3) != "+" && argv(3) != "-" && argv(3) != "*" && argv(3) != "/" && argv(3) != "^" && argv(3) != "%")
                      {
                          string operator;
                          float num1, num2, result;
                          num1 = stof(argv(1));
                          operator = argv(2);
                          num2 = stof(argv(3));

                          //if the parameter is pi number
                          if (argv(1) == "pi" || argv(1) == "π") //if the user writes pi in the first parameter to calculate using pi number
                          {
                              num1 = 3.14159265358979323;
                          }
                          if (argv(3) == "pi" || argv(3) == "π") //if the user writes pi in the third parameter to calculate using pi number
                          {
                              num2 = 3.14159265358979323;
                          }

                          //if the parameter is tau number
                          if (argv(1) == "tau" || argv(1) == "τ") //if the user writes tau in the first parameter to calculate using tau number
                          {
                              num1 = 2 * 3.14159265358979323;
                          }
                          if (argv(3) == "tau" || argv(3) == "τ") //if the user writes tau in the third parameter to calculate using tau number
                          {
                              num2 = 2 * 3.14159265358979323;
                          }

                          switch (operator)
                          {
                              case "+":
                              {
                                  result = num1 + num2;
                                  sprint(caller, sprintf("^5The sum result is: ^6%f\n", result));
                                  return;
                              }
                              case "-":
                              {
                                  result = num1 - num2;
                                  sprint(caller, sprintf("^5The substraction result is: ^6%f\n", result));
                                  return;
                              }
                              case "*":
                              {
                                  result = num1 * num2;
                                  sprint(caller, sprintf("^5The multiplication result is: ^6%f\n", result));
                                  return;
                              }
                              case "/":
                              {
                                  result = num1 / num2;
                                  sprint(caller, sprintf("^5The division result is: ^6%f\n", result));
                                  return;
                              }
                              case "^":
                              {
                                  result = pow(num1, num2);
                                  sprint(caller, sprintf("^5The exponent result is: ^6%f\n", result));
                                  return;
                              }
                              case "%":
                              {
                                  result = num1 % num2;
                                  sprint(caller, sprintf("^5The modulus result is: ^6%f\n", result));
                                  return;
                              }
                              default:
                              {
                                  return;
                              }
                          }
                          return;
                      }
                  }
              }
          }
      }
      default:
          sprint(caller, sprintf("Incorrect parameters for ^2%s^7\n", argv(0)));
      case CMD_REQUEST_USAGE:
      {
          sprint(caller, "\nUsage:^3 cmd calc\n");
          sprint(caller, "  Where '+', '-', '*', '/', '^' and '%' are the operators to calculate.\n");
          sprint(caller, "  Examples:\n^3 cmd calc 4 + 5\n^3 cmd calc 3 ^ 2\n^3 cmd calc 8 % 2\n^3 cmd calc pi * 0.5\n^3 cmd calc 32.46 / π\n^3 cmd calc τ * 0.112\n^3 cmd calc 4.1 / tau\n");
          return;
      }
  }
}



3. Write the following macro to call the command function in the line 860 more or less where says "Macro system for networked commands":

Code:
CLIENT_COMMAND("calc", ClientCommand_calc(ent, request, arguments), "Basic Calculator") \

4. Save the file cmd.qc

5. If you don't want to write cmd and you want to execute the calc command directly, then do this: go to and open xonotic/data/xonotic-data.pk3dir/commands.cfg, go to where says "cmd (client-to-server command) - server/command/cmd.qc" and qc_cmd_cmd things, inside of those aliases, write a new line inside those aliases:
Code:
alias calc "qc_cmd_cmd calc ${* ?}" // Basic Calculator in the console

you can organize a bit your line to be like the other aliases, after save the commands.cfg

5. Go to console MSYS2 (as I use Windows), in the console, write and execute ./all compile -qc -r


6. After, run the game with ./all run


7. Execute map any_mapname command in the game (NOTE: alternatively you can go in Multiplayer >> Create >> Select a map >> Start multiplayer! , you're creating a local server)


8. When the map is loaded in the game, run the following created command, for example: cmd calc 5 + 5 

9. Enjoy!!! Cool


calc
[Image: unknown.png]

Using pi (π) number
[Image: unknown.png]

Using tau (τ) number
[Image: unknown.png]
Believing is power
Reply



Possibly Related Threads...
Thread Author Replies Views Last Post
Question [NEED HELP] How to create warpzones????????????????????????? Kotangens 2 177 Yesterday, 03:14 AM
Last Post: Kotangens
  What was easy for you in development? (Darkplaces and QuakeC programming) LegendGuard 2 222 08-08-2020, 05:25 PM
Last Post: LegendGuard
  Moving away from QuakeC Lyberta 17 3,538 07-17-2020, 07:36 AM
Last Post: LegendGuard
  [NEED HELP] [Commission] To update Jeff's Modpack - QuakeC programmer wanted breakfast 5 1,361 07-08-2020, 12:45 PM
Last Post: breakfast
  New QuakeC virtual machine Lyberta 8 3,545 07-14-2017, 04:01 PM
Last Post: poVoq
Brick A script engine written in QuakeC Melanosuchus 9 7,866 10-14-2014, 02:01 AM
Last Post: Melanosuchus
  QuakeC: if chains? WannabeUser 2 2,601 03-13-2014, 07:28 AM
Last Post: WannabeUser
  QuakeC: Variadic macros? WannabeUser 2 2,265 03-10-2014, 05:16 PM
Last Post: WannabeUser
  Learning QuakeC? phim 13 13,551 12-24-2013, 03:07 AM
Last Post: aa
  A question of getting started with Quakec timetopat 5 5,522 07-02-2012, 07:55 AM
Last Post: timetopat

Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB original theme © iAndrew 2016, remixed by -z-