Take a screenshot at runtime

My work is to build a scene according to the database.
After building the scene I need to take a screenshot and save in local.

I used

bge.render.makeScreenshots("//")

at the end of my script

however,
it sometimes save a all grey image while sometimes it correctly save the screenshot that what I need

I think that it depends on the performance of the computer, although all the codes are ran but the rendering job had not yet finished therefore it takes the all grey image.

what should i do in order to get the proper screenshot?
Thank you.

import bge  

co = bge.logic.getCurrentController() 
obj = co.owner  

number = str("%04d" % obj['frame'])
bge.render.makeScreenshot("//frame" + number + ".png")
obj['frame'] += 1

this is some code i had in my notes of making a screen shot hope it helps.

Thanks for the code, I have tried but still same result.

I think the problem is a time gap between the code executed and the rendering finished.
Now the code executed and screenshot is captured before the rendering is finished.

I believe that this script example serve, tested here and worked:

Download link is in a practical example.

Could you simply wait till the next frame to take the screen shot?

A while ago I’d created an screenshotsystem too.
It includes an save-system too. :wink:

http://www.blenderartists.org/forum/showthread.php?271400-Simple-Screenshotsystem-BGE-(V-1-5-0)

Thanks for the script
I believe that it will work perfectly if it wait for the user pressing a key to activate it.
however, i need the screenshot made automatically when the rendering is finished.

and my problem is the codes executed before the rendering is finished. the screenshot is made but captured nothing.

is there any way to have it keep trying to call the new image until it can? (like as a texture ?)

that way it will keep throwing a error as the file is not written yet?

and have

if texture != None:
call texture?

I don’t know the exact code here… as I have only really messed with the game engine physics, and ray cast so far,

maybe just put the take screenshot code on its own script.py and controller and check the icon on the controller that looks like a folder with a red tag coming out of it. make it to be executed b4 other scripts.