We'll be taking screenshots with Python and saving them to our desired directories and preventing screenshots from being overwritten, let's get started/
Let's create a simple scene by adding some lights, some cubes adding some mist to the scene, etc. I added:
Just a rough scene, we just want know how the screenshot is taking with python.
We'll working without the keyboard logic brick this time. Let's import our main module which is Range if it is Range Engine and bge if its UPBGE and we'll specify our directory with logic.expandPath() like so.
try:
import bge
except:
import Range as bge
from os import path
user_name = path.expanduser("~") # Get user's directory
dir = bge.logic.expandPath(user_name+"\\Documents") # Join with Documents folder
cont = bge.logic.getCurrentController()
def scrnsht(cont):
keysdown = bge.logic.keyboard.activeInputs
space = bge.events.SPACEKEY
if space in keysdown:
print(dir)
The code might look complicated but lemme break it down for you:
Because Range developers has changed the bge name as Range. So we import the module, as bge so our code is easy to edit.
So when the print the dir from the python code its prints your user name with the documents folder.
This help us to execute the code a lot easier without the need of changing the
user name when the path has another user's name and shared with others.
If you've gotten the directories printed more than one time, we need to setup
a property that only triggers the print(dir) once.
We add a property "once" and use
it to prevent from triggering thr print() more than once. Here's the code.
try:
import bge
except:
import Range as bge
from os import path
user_name = path.expanduser("~") # Get user's directory
dir = bge.logic.expandPath(user_name+"\\Documents") # Join with Documents folder
cont = bge.logic.getCurrentController()
def scrnsht(cont):
own = cont.owner
if "once" not in own: # Add "once" property if it doesn't exist
own["once"] = True # Set it as TRUE
keysdown = bge.logic.keyboard.activeInputs
space = bge.events.SPACEKEY
if space in keysdown and own["once"] == True: #If user pressed and once is true
print(dir) # print our "dir"
own["once"] = False #set "once" as FALSE, prevents from running again.
if space not in keysdown: # If "space" is not pressed
own["once"] = True # Set "once" to True
Nice, now for the screenshot part we need to add bge.render.makeScreenshot() and inside the function parenthesis, we'll add our "dir" which holds our directory data and our image name which looks like this.
try:
import bge
except:
import Range as bge
from os import path
user_name = path.expanduser("~") # Get user's directory
dir = bge.logic.expandPath(user_name+"\\Documents\\bro#") # Join with Documents folder
# and take multiple shots without overwriting the image
cont = bge.logic.getCurrentController()
def scrnsht(cont):
own = cont.owner
if "once" not in own: # Add "once" property if it doesn't exist
own["once"] = True # Set it as TRUE
keysdown = bge.logic.keyboard.activeInputs
space = bge.events.SPACEKEY
if space in keysdown and own["once"] == True: #If user pressed and once is true
bge.render.makeScreenshot(dir) # Take screenshots
own["once"] = False #set "once" as FALSE, prevents from running again.
if space not in keysdown: # If "space" is not pressed
own["once"] = True # Set "once" to True
nice, it works here and it must be working for you too.
Avoiding low-res screenshots
The screenshots are taken respective to the screensize your game is running in. If your game size is 1920x1080 your screenshots will be in that dimensions.