godot-console icon indicating copy to clipboard operation
godot-console copied to clipboard

Optionally forward command name as first argument to command execution callback function

Open starwolfy opened this issue 4 years ago • 4 comments
trafficstars

I am implementing this console add-on for a client in a networked server-client project.

When my client connects to the server, the client & console addon will dynamically register new commands that get sent from the server.

The problem is that I can only link all the callback functions to the same function as GDScript doesn't allow me to generate new function bodies and link those, see example below:

func consoleAddCommands(commandName : String, commandDescription : String, commandArguments : Dictionary) -> void:

  var functionName : String = "sendServerSideCommand" + str(commandArguments.size())
  var commandBuilder = Console.add_command(commandName, self,functionName)\
  .set_description(commandDescription)

  for argumentName in commandArguments:
	  commandBuilder.add_argument(argumentName,typeof(commandArguments[argumentName]))

  commandBuilder.register()




func sendServerSideCommand0(commandName) -> void:
	networkhound.send_message("serverSideCommand", [commandName])
func sendServerSideCommand1(commandName, arg0) -> void:
	networkhound.send_message("serverSideCommand", [commandName, arg0])
func sendServerSideCommand2(commandName, arg0, arg1) -> void:
	networkhound.send_message("serverSideCommand", [commandName, arg0, arg1])
func sendServerSideCommand3(commandName, arg0, arg1, arg2) -> void:
	networkhound.send_message("serverSideCommand", [commandName, arg0, arg1, arg2])
=> Etc..

This code does not work because the console add-on does not provide commandName to the callback functions, a way to circumvent that is to have the user input the same command twice, both as command name and as the 1st argument but this is not ideal. So each time a callback fires from a dynamically added command, I get the correct variating arguments, but no way of identifying which exact command was executed.

Implementing a way to optionally register some commands that are expected to provide the commandName as the first argument to their callback functions would be really useful!

starwolfy avatar Jul 22 '21 10:07 starwolfy

Hello!

I've been thinking about giving users an ability to create custom commands and creating aliases for already existing commands which would solve your issue and give a very nice structure to the code. But I don't have much time to implement that so in the mean time if you are fine with a temporary solution I can add an optional flag to CommandBuilder which is going to be removed later when I implement a proper way of doing this stuff.

This is the example of my proposal:

Console.add_command("cmdname", self, "funcname")\
  ...
  .pass_command_name() # It would also accept an optional boolean as an argument in case someone decides to enable/disable this behaviour
  ...
  .register()

quentincaffeino avatar Jul 22 '21 11:07 quentincaffeino

The proposed solution most definitely includes the changes I need and can make good use of! Thank you for your time!

starwolfy avatar Jul 22 '21 11:07 starwolfy

Thanks to your suggested proposal I have been able to implement this quickly in a pretty rough and non-idiomatic way with small modifications to Command.gd and CommandBuillder.gd which suffices for my case.

So please, if no one else requires such functionality other than me, perhaps you do not need to implement it, because I do have a quickly mashed up version of this working now!

starwolfy avatar Jul 22 '21 12:07 starwolfy

Glad to hear that and thank you for your understanding)

I'm gonna leave this issue open for now to reference it later when the functionality will be implemented.

quentincaffeino avatar Jul 22 '21 12:07 quentincaffeino