Blenderplayer parsing arguments to script

Hello,

I have been pointed at using the “Blenderplayer” after asking the question “How is it possible to run BGE using the command line?” in the Game Engine Support and Discussion – Forum two days ago.

Running the “Blenderplayer” using command line arguments is rather straight forward. Unfortunately, this does not hold – at least for me - for passing arguments to the script running in the logic bricks of the related blender file.

The blenderplayer help suggests using the syntax “[…/blenderplayer.exe” , “-g”, “- my_argument”, blender_file]”. Is this the correct position? Since the blenderplayer is starting, it seems like to be. Even though, I haven’t any example code beyond that.

I am really struggling parsing the argument in the script running in the logic bricks again. The

def get_args():
 
    parser = argparse.ArgumentParser()
    parser.add_argument("- my_argument", help="Coordinates")
    all_arguments = parser.parse_args()
    dash_index = all_arguments.index('-')
    script_args = all_arguments[dash_index + 2: ]
    parsed_script_args,_ = parser.parse_args(script_args)
    return parsed_script_args
 
args = get_args()
the_argument=args.my_argument

doesn’t work. I am aware that the parsed arguments are string. They are split in the code afterwards.

Any hint and idea how to restructure to code is welcome.

Thanks for your help.

Hello,

after a lot of trail and error I figured out a working solution. For references this works - at least for me:

parser = argparse.ArgumentParser()

_, all_arguments = parser.parse_known_args()

parser.add_argument("-arg1","- string_agr1", help="arg1")
parser.add_argument("-arg2","- string_arg2", help="arg2")
parser.add_argument("-arg3","- string_arg3", help="arg3")

# Add arguments into dictonary
arg_dict = {
        '- string_arg1': None,
        '- string_arg2': None,
        '- string_arg3': None
}

for known_arg in arg_dict.keys():
    arg_value_index = all_arguments.index(known_arg) + 1
    arg_string = all_arguments[arg_value_index].replace('[', '').replace(']', '')
    arg_dict[known_arg] = arg_string.split(', ')

Hope that this also might help someelse.

Best regards