Scripting event on starting/ending a game

Since Batocera 5.23 we can execute scripts right before and after a game ends. There are several reasons for doing this.

  • Joypad setup
  • Automatic Scraping
  • Change screen resolution
  • Transfer Savestates to external device
  • … give me your idea here

Place an executable script with correct setted shebang (first line!) or an executable binary into directory /userdata/system/scripts. You can add as many subfolder as you want, every script placed there will be executed. To distinguish between START or STOP condition the first arguments parsed to your script carries this information

  • gameStart is parsed to your scripts if you select a game from Emulationstation (Game Starts!)
  • gameStop is parsed to your scripts if you are going back to Emulationstation (Game Stops!)

If you do not set cases for first argument, then the scrip is executed every start and on every end of a game

Table of parsed arguments in correct order and it's function

Args. Parsed Parameter Usage
1 gameStart or gameStop to distinguish between START or STOP condition
2 systemName like in your es_system.cfg, for eg. atari2600
3 system.config['emulator'] The emulator settings, for eg. libretro
4 system.config['core'] The emulato core you have choosen, for eg. stella
5 args.rom The full rom path, for eg. /userdata/roms/atari2600/Mysterious Thief, A (USA).zip

At first create the directory were the scripts needs to be setted up, so type mkdir /userdata/system/scripts. After this we setup our first script so we type nano /userdata/system/scripts/

now fill in this code
#This is an example file how Events on START or STOP can be uses
#Set logfile location and filename
#Case selection for first parameter parsed
case $1 in
        echo "START" > $logfile
        echo "$@" >> $logfile
        echo "END" >> $logfile

Make the script executable with chmod +x /userdata/system/scripts/

Now you can see a logfile in /tmp/scriptlog.txt, that parsed all arguments in this file. This is just a small test of course.

  • script_events_on_starting_ending_a_game.txt
  • Last modified: 3 weeks ago
  • by lala