In this tutorial, we will get and set screen resolution of our game,
let's get started.
Fire up the game engine, create a new text file, save it as reso.py, import the game engine module and necessary stuff
try:
import Range as bge
except:
import bge
cont = bge.logic.getCurrentController()
own = cont.owner
def reso(cont):
pass
Now we get the screen resolution and print it on a property.
I am a lazy guy, i dont create property by going to logic bricks,
create a property, create a new one, blah blah blah...
No, I got a new method for you where u can create a property inside python and
debug without touching logic bricks! For that, we create two new properties named
as "X" and "Y" which will be inside a init function.
def init(cont):
if "init" not in own or own["init"] != True:
own["init"] = True
# List of properties goes here.
So we've created an "init" function that must run only once, it only runs once even if true level triggering was enabled in a sensor. Let's add some properties and debug them.
def init(cont):
if "init" not in own or own["init"] != True:
own["init"] = True
own["X"] = 0.0 # There's a ".", so it's a float
own["Y"] = 0.0 # Float type
Here we've added two properties and its value is not 0
but 0.0 which means, its a floating point integers.
If you assign a True or False,
the list would be a boolean type property.
Now let's debug the property.
def init(cont):
if "init" not in own or own["init"] != True:
own["init"] = True
own["X"] = 0.0 # There's a ".", so it's a float
own["Y"] = 0.0 # Float type
own.addDebugProperty("X", True)
own.addDebugProperty("Y", True)
To check if this works, add an always sensor, two python controller with script type set as Module, name the first one as reso.init, the second one as reso.reso and connect them.
Now we'll move to the looping function, we'll get the screen width and height.
def reso(cont):
own["X"] = bge.render.getWindowWidth()
own["Y"] = bge.render.getWindowHeight()
Now we got our info, now we need to set the resolution. We can either set the resolution either with a property or just be defining them, We'll go to our init function and create two new properties that sets the resolution.
def init(cont):
if "init" not in own or own["init"] != True:
own["init"] = True
own["X"] = 0.0 # There's a ".", so it's a float
own["Y"] = 0.0 # Float type
own.addDebugProperty("X", True)
own.addDebugProperty("Y", True)
if "width" or "height" not in own:
own["width"] = 640 # Integer type
own["height"] = 480 # Integer type
In this code we check that if the property width
or height doesn't exist, we create them
and set their values as 640x480.
Now let's jump to the looping function, and write the code that set the
resolution we need. We should not be keep setting the resolution, so we need
a keyboard sensor that does the job once. Add a keyboard sensor,
enable Tap,
connect it with the reso.reso controller.
def reso(cont):
own["X"] = bge.render.getWindowWidth()
own["Y"] = bge.render.getWindowHeight()
if cont.sensors["Keyboard"].positive:
bge.render.setWindowSize(own["width"], own["height"])
Note
render.setWindowSize() only works in the standalone player mode, not in the in-built embedded player.
Now launch the game in standalone player mode and trigger the keyboard,
it must set the window size to the values we've set.