Provides often-needed functionality like generating a random number between 2 values. More...

Import Statement: import VPlay 2.0



Detailed Description

This component allows easy access to often-needed functionality like generating a random number between 2 values.

Other functions will follow based on customer requests, to simplify often-needed tasks that are not supported by default as dedicated functions in JavaScript.

Usually, you can use the functionality from the GameWindow::utils property, so you do not need to make more instances of this component as the utils property is accessible globally in all QML files.

Example Usage

The following example shows how to call the Utils::generateRandomValueBetween() method from a TestEntity.qml file:

 import VPlay 2.0

 EntityBase {

   Component.onCompleted: {

     // the utils property is available to all elements, because it is contained in the root GameWindow element
     console.debug("random value between 10 and 15:", utils.generateRandomValueBetween(10, 15) )

Method Documentation

clamp(num, minimum, maximum)

Returns a number whose value is limited to the given range minimum and maximum.

For example, limit the output of this computation to between 0 and 255

 var x = Math.random()*512
 console.debug("Limit to [0,255]: ", utils.clamp(x, 0, 255) )

cropPath(path, cropExtension)

Crops any input path and returns only the file name without path but with file ending. The second parameter specifies if the file extension should be cropped too.

 console.debug("crop results in fireParticle.png : ", utils.cropPath(particles/fireParticle.png) )
 console.debug("crop results in fireParticle : ", utils.cropPath(particles/fireParticle.png, true) )

decimals(number, scientific)

Returns the number of decimals for a given number. By default scientific is false and a simple algorythm is used to count zeros after the decimal point, therefore only simple floats (0.003) can be used and no scientific numbers (0.003005 or .34e1). Enable the scientific flag to use scientific numbers.

The following code shows how it works.

 import QtQuick 2.2
 import VPlay 2.0

 GameWindow {
   Scene {
     Component.onCompleted: {
       console.debug("2 has "+utils.decimals('2')+" decimals") // Answer: 0
       console.debug(".3 has "+utils.decimals('.3')+" decimals") // Answer: 1
       console.debug("0.2 has "+utils.decimals('.02')+" decimals") // Answer: 2
       console.debug("0.2 has "+utils.decimals('.02002')+" decimals") // Answer: 2
       console.debug("0.2 has "+utils.decimals('.02002',true)+" decimals") // Answer: 5
       console.debug("34e-100 has "+utils.decimals('34e-100',true)+" decimals") // Answer: 100
       console.debug("1.7e-99 has "+utils.decimals('1.7e-99',true)+" decimals") // Answer: 100
       console.debug(".3e1 has "+utils.decimals('.3e1',true)+" decimals") // Answer: 0
       console.debug(".34e1 has "+utils.decimals('.34e1',true)+" decimals") // Answer: 1

generateRandomValueBetween(minimum, maximum)

Returns a floating point value between minimum and maximum.

For example the call generateRandomValueBetween(10, 15) returns a random value between 10 and 15.

The minimum value can also be negative, e.g. generateRandomValueBetween(-40, 30) returns a value between -40 and 30, including the possibility to return 0.

Internally, Math.random() is used for calculating the random number.


Returns a random color.

For example the call randomColor() returns a random value between #FFFFFF and #000000.

This QML method was introduced in V-Play 2.0.2.

Voted #1 for:

  • Easiest to learn
  • Most time saving
  • Best support

Develop Cross-Platform Apps and Games 50% Faster!

  • Voted the best supported, most time-saving and easiest to learn cross-platform development tool
  • Based on the Qt framework, with native performance and appearance on all platforms including iOS and Android
  • Offers a variety of plugins to monetize, analyze and engage users
create apps
create games
cross platform
native performance
3rd party services
game network
level editor
easiest to learn
biggest time saving
best support