Utils

This module provides you with various util functions of BGECore.

Files

core.utils.getBlendFilepath()

Returns the blend file absolute filepath, including the name.

core.utils.getLocalDirectory()

Returns the directory where local data can be stored. By default the same directory than the game. If there is no write acces then a directory inside the user folder.

Configuration file

core.utils.loadGameProperty(name)

Loads a property from your config.txt file.

The property is loaded as string, then you use the type name to get the apropiate type. e.j: media.device.volume = float(utils.loadGameProperty("volume"))

Raises KeyError if the property is not found.

Parameters:name (string) – Name of the property to load.
Returns:A string containing the value of the property.
core.utils.saveGameProperty(name, value)

Saves a property to your config.txt file.

Parameters:
  • name (string) – Name of the property to load.
  • value – Value to save, will be converted into a string.

Debug

You can define constants on your project “script/constant.py” file.

core.utils.debug(text)

Prints text if CORE_DEBUG_PRINT is enabled.

core.utils.verbose(text)

Prints text if CORE_DEBUG_VERBOSE is enabled.

Math and movement

core.utils.rand10()

Generates a rondom integer from 0 to 9

core.utils.randRGB(r=None, g=None, b=None, a=1)

Generates a random vector representing a color, paramaters not None will use that value instead of generating a new one.

core.utils.getNearestObject(obj, property='', max_distance=0)

Returns the closest object to ‘obj’. If there isn’t any, returns itself.

Parameters:
  • obj – The base object
  • property (float) – If any, will filter only objects with this property. If it’s a Bool must be checked as True
  • max_distance – Filter objects within this distance. If 0, do not filter distance.
core.utils.getNearestVertexToPoly(object, poly, point)

Returns the nearest vertext to a poligon.

Parameters:
  • poly (KX_PolyProxy) – The poligon if wich vertex you want to check.
  • point (mathutils.Vector) – The point to check, in world coordinates.
core.utils.getPolyNormal(poly)

Returns the normal of poligon based on the position of their vertex. It calculates the normal, it doesn’t return manually modified normals.

Parameters:poly (KX_PolyProxy) – The poligon.
core.utils.recalculateNormals(obj)

Recalculates the normals of a KX_GameObject, KX_MeshProxy or KX_PolyProxy.

It iterates through all the given vertex, it may be a slow operation, use with caution.

core.utils.vectorFrom2Points(origin, dest, module=None)

Returns a mathutils.Vector form 2 points in the space.

Parameters:
  • origin (mathutils.Vector) – Point A
  • dest (mathutils.Vector) – Point B
  • module (float) – If setted, the returned vector will have this maxium lenght. The new lenght will never be greater than the original.
core.utils.moveObjectToObject(origin, dest, speed=1)

Moves origin to dest at a speed of speed. Must by called every frame for a complete movement.

Parameters:
  • origin (KX_GameObject) – Object to move.
  • dest (KX_GameObject) – Destination object.
  • speed (float) – The amount of movment to do in one frame.
Returns:

True if the object has been moved, false otherwise.

core.utils.moveObjectToPosition(origin, dest, speed=1)

Moves origin to dest at a speed of speed. Must by called every frame for a complete movement.

Parameters:
  • origin (KX_GameObject) – Object to move.
  • dest (mathutils.Vector) – Destination object.
  • speed (float) – The amount of movment to do in one frame.
Returns:

True if the object has been moved, false otherwise.

core.utils.removeAll(original_list, sublist)

Removes all ocurrences of any of the values of sublist from original_list