Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
starwolfy opened this issue Jul 22, 2021 · 4 comments

Comments

@starwolfy
Copy link

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!

@quentincaffeino
Copy link
Owner

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()

@starwolfy
Copy link
Author

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

@starwolfy
Copy link
Author

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!

@quentincaffeino
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants