Download

How can you access command line arguments with Blueprint?

I’d like to pass in information from the command line and then use that info to populate variables in Blueprint. Does anyone know how that could be done?

For example:

mygame.exe ?charactername=Bob

Then in blueprint, I’d like to be able to access ‘Bob’ in my character blueprint, and assign it to my character name variable. I have other variable’s I’d like populate this way as well.

Don’t think it’s possible to do it for now with Blueprints, it’s possible with C++ with this snippet of code;



#include <iostream>
int main(int argc, char** argv) {
    std::cout << "We have " << argc << " arguments. Commands Entered;" << std::endl;
    for (int i = 0; i < argc; ++i) {
        std::cout << argv* << std::endl;
    }
}


Couldn’t find anyway in the Blueprint to get the arguments, maybe its time for them add a “Get Console Arguments” option in it. :slight_smile:

Thanks for taking the time to look into that SOTO!

I better brush up on my c++ then.

So for my example should i collect the variable on start and store it in a temp variable, then use it again on my character construction to populate my variable?



#include <string>

int main (int argc, char* argv]) {

  string argtest = "";
  string argvalue = "";

  for (int i = 0; i < argc; i++){
    argtest = argv*;
    if (argtest.find ("charactername=") != string::npos) {
       argvalue = argtest.substr(15);
       }
  }
}


Then would I grab ‘argvalue’ in my character constructor and assign it to my blueprint variable ‘CharacterName’?

I’m not a programmer so if that is horrible to look at I apologize in advance! Any suggestions would be welcome!

Edit someone proved me wrong thankfully, time to use this; https://wiki.unrealengine.com/Blueprints,_Creating_C%2B%2B_Functions_as_new_Blueprint_Nodes
Trying to understand what you’re really wanting to do with that hacked together script. If you’re trying to find some confirmed words you’d better off using a switch statement which is would basically be like;



switch (consoleArgumentWord){
case "dogs": cout << "Found Dogs";
    break;
case "cats": cout << "Found Cats";
    break;
case "debug": cout << "Found debug";
    break;
default: cout << "Invalid or no Args";
    break;
}


Of course you can. Blueprint nodes just point at C++ functions, and there’s plenty of documentation in the gameplay programming docs to go with it.

Yes, using a switch looks to be a cleaner option. I guess I need to look at a basic code project and see how to get started with something like that.

So once you can isolate the argument your looking for, you store it in a temp variable, and then re-assign it to the variable in the blueprint somewhere in the constructor for the blueprint (in my case the character blueprint) when it is called later in the code?

I’ll check out some of the basic code project video tuts. Thanks for your help with this SOTO and ambershee. If it were possible that a “Get Console Arguments” node could be put into blueprint at some point in the future, I’m sure it would be a great addition.

So I’ve run through the code tutorial game now, so that’s a start. I’d like to modify the example to see if I can implement this idea now, how do you access the command line arguments in code? I can’t seem to find any reference info on it. Is there a function that you can call to grab a command line argument?

Bump - in case others come searching for this, there is now (4.16, possibly earlier) a blueprint node called “Get Command Line” that you can use to get the commandline string, which you can then process yourself. There are also some other options-related nodes but I’ve had mixed luck with them so just parsing the cmdline myself in BP for now.

6 years later it still wasn’t clear how to get cmd options.

Example mygame.exe ?charactername=Bob quite correct. Then you can just access Options String in GameMode:

[HR][/HR]
Note:
a. You have to use ? glyph as options separator. I don’t sure how to access options like -game from docs
b. When you call **Open Level** you can reset options with **Absolute **flag [HR][/HR]
Example:
[SPOILER]
My workaround for server/client switch from command line

game.cmd:


NetBoilerplate.exe ?mode=game


[/SPOILER]