Back to Content

CopperCube JavaScript reference


CopperCube provides a very simple interface to manipulate all aspects of a 3D scene. See CopperCube Scripting overview for a short description on how to do this. This file lists all available functions.

Most of these functions manipulate so called 'scene nodes'. A scene node is nothing more than a 3d object with a position, rotation, scale, its materials and children. The name 'scene node' is there because CopperCube is internally using a scene graph to represent the 3d scene.

Scene Node Handling

ccbCloneSceneNode
ccbGetActiveCamera
ccbSetActiveCamera
ccbGetChildSceneNode
ccbGetRootSceneNode
ccbGetSceneNodeChildCount
ccbGetSceneNodeProperty
ccbSetSceneNodeProperty
ccbGetSceneNodeFromName
ccbGetSceneNodeMaterialCount
ccbGetSceneNodeMaterialProperty
ccbSetSceneNodeMaterialProperty
ccbRemoveSceneNode
ccbSetSceneNodeParent
ccbSetSceneNodePositionWithoutCollision

Events

ccbRegisterKeyDownEvent
ccbRegisterKeyUpEvent
ccbRegisterMouseDownEvent
ccbRegisterMouseUpEvent
ccbRegisterOnFrameEvent
ccbUnregisterOnFrameEvent

Drawing

ccbDrawColoredRectangle
ccbDrawTextureRectangle
ccbDrawTextureRectangleWithAlpha

Collision

ccbGet3DPosFrom2DPos
ccbGet2DPosFrom3DPos
ccbGetCollisionPointOfWorldWithLine
ccbDoesLineCollideWithBoundingBoxOfSceneNode

Various

ccbEndProgram
ccbLoadTexture
ccbGetMousePosX
ccbGetMousePosY
ccbSetMousePos
ccbGetScreenWidth
ccbGetScreenHeight
ccbSetCloseOnEscapePressed
ccbSetCursorVisible
ccbSwitchToScene
ccbPlaySound
ccbStopSound
ccbGetCopperCubeVariable
ccbSetCopperCubeVariable
ccbReadFileContent
ccbWriteFileContent
ccbFileExist
ccbFileDelete
ccbGetPlatform
ccbInvokeAction
ccbGetCurrentNode
ccbCleanMemory
ccbSwitchToFullscreen
ccbDoHTTPRequest
ccbCancelHTTPRequest
ccbCreateMaterial
ccbSetShaderConstant
ccbSetPhysicsVelocity
ccbUpdatePhysicsGeometry
ccbAICommand
ccbSteamSetAchievement
ccbSteamResetAchievements
ccbSaveScreenshot
ccbSaveTexture
ccbSwitchToCCBFile
print
system

Only in the editor

confirm
alert
prompt
editorAddSceneNode
editorFocusPosition
editorGetFileNameFromDialog
editorGetSelectedSceneNode
editorGetSelectedTexture
editorUpdateAllWindows
editorRegisterMenuEntry
editorSetSelectedSceneNode
editorImportStatic3DMesh
editorImportAnimated3DMesh

Mesh Editing

ccbGetSceneNodeMeshBufferCount
ccbRemoveMeshBuffer
ccbAddMeshBuffer
ccbGetMeshBufferVertexCount
ccbGetMeshBufferIndexCount
ccbAddMeshBufferIndex
ccbGetMeshBufferIndexValue
ccbSetMeshBufferIndexValue
ccbAddMeshBufferVertex
ccbGetMeshBufferVertexPosition
ccbSetMeshBufferVertexPosition
ccbGetMeshBufferVertexTextureCoord
ccbSetMeshBufferVertexTextureCoord
ccbGetMeshBufferVertexNormal
ccbSetMeshBufferVertexNormal
ccbGetMeshBufferVertexColor
ccbSetMeshBufferVertexColor
ccbUpdateSceneNodeBoundingBox

Advanced

ccbGetAnimatedJointCount
ccbGetAnimatedJointName
ccbGetCurrentAnimationFrame
ccbSetCurrentAnimationFrame
ccbSetAnimationFromSceneNode
ccbGetTextureWidth
ccbGetTextureHeight
ccbPauseAllSounds
ccbSetDopplerEffectParameters
ccbSplitScreen
ccbGetAnimatedJointPosition
ccbSetGameTimerSpeed
ccbEmulateKey

Other:

vector3d - a vector class
Standard Library
(basic functions for math, strings, arrays, regex etc)


ccbCloneSceneNode(node)

Note: This function is not available in the editor.

Creates a new scene node based on an existing scene node.

The parameter 'node' must be an exiting scene node. You can get an existing scene node forexample with ccbGetSceneNodeFromName()

Returns: The new scene node.

Examples:

var sourceNode = ccbGetSceneNodeFromName("myNode");
var newscenenode = ccbCloneSceneNode(sourceNode);
This example will create a copy of an existing scene node with the name 'myNode'.


ccbGetActiveCamera()

Note: This function is not available in the editor.

Returns the currently active camera of the scene.



ccbSetActiveCamera(node)

Note: This function is not available in the editor.

Sets the currently active camera to the scene. The parameter 'node' must be a camera scene node.



ccbGetChildSceneNode(parentSceneNode, childIndex)

Returns the child scene node of a parent scene node. ChildIndex must be >= 0 and < ccbGetSceneNodeChildCount.

Example:

var root = ccbGetRootSceneNode();
var count = ccbGetSceneNodeChildCount(root);

for(var i=0; i<count; ++i)
{
var child = ccbGetChildSceneNode(root, i);
print("node:" + ccbGetSceneNodeProperty(child, "Name") + "\n");
}
This example prints the name of all child scene nodes of the root. 

 


ccbGetRootSceneNode()

Returns the root scene node. You cannot remove it and it does not make a lot of sense to change its attributes, but you can use it as starting point to iterate the whole scene graph. Take a look at ccbGetSceneNodeChildCount for an example.

Returns: The root scene node.


ccbGetSceneNodeChildCount(sceneNode)

Returns the amount of children of a scene node. 

Example:

var root = ccbGetRootSceneNode();
var count = ccbGetSceneNodeChildCount(root);

print("Scene nodes in the top level of the scene graph:" + count);
This example will return '2' if there are two scene nodes in your scene.

 


ccbGetSceneNodeFromName(name)

Searches the whole scene graph for a scene node with this name. Please note that the name is case sensitive. If it is found, it is returned, otherwise null is returned.

Example:

var s = ccbGetSceneNodeFromName("cubeMesh1");

if (s) print("found node.\n");
else
print("not found the node.\n")
Searches for a scene node with name 'cubeMesh1' and prints some text.

 


ccbGetSceneNodeMaterialCount(sceneNode)

Returns the amount of materials of the scene node.

Example:

var s = ccbGetSceneNodeFromName("cubeMesh1");
var n = ccbGetSceneNodeMaterialCount(s);

print("the scene node has " + n + " materials");
Prints the amount of materials in a scene node named 'cubeMesh1'

 


ccbGetSceneNodeMaterialProperty(sceneNode, materialIndex, propertyName)

Returns the property of the material of a scene node.
Parameters:

Example:

var s = ccbGetSceneNodeFromName("cubeMesh1");
var t = ccbGetSceneNodeMaterialProperty(s, 0, "Texture1");

print("texture of the cube is: " + t);
Prints the name of the texture set in an existing scene node named 'cubeMesh1'. Prints something like 'texture of the new cube is: textures/editor_defaults/default_texture.png'

 


ccbLoadTexture(filename)

Loads a texture into the texture cache. Returns the texture object if sucessful, which then can be used for example in ccbSetSceneNodeMaterialProperty() calls with the texture parameter. Note that the texture is only loaded once. You can call this multiple times with the same texture file, but CopperCube won't try to load it multiple times if it has been loaded once already.

Parameters:

Example:

var node = ccbGetSceneNodeFromName("cubeMesh1");
var tex = ccbLoadTexture("example.jpg");
ccbSetSceneNodeMaterialProperty(node, 0, "Texture1", tex);
Loads a texture and sets it as material onto an object name 'cubeMesh1'.


ccbRemoveSceneNode(sceneNode)

Removes the scene node from the scene, deleting it. Doesn't work for the root scene node.

Example:

ccbRemoveSceneNode( ccbGetSceneNodeFromName("cubeMesh1") );
Deletes a scene node with the name 'cubeMesh1' if it exists. 


ccbSetSceneNodeParent(sceneNode, newparentnode)

Sets the parent scene node of a scene node. If this node has already a parent, it will be removed from that parent. Note that by setting a new parent, position, rotation and scale of this node becomes relative to that of the new parent.


ccbSetSceneNodeMaterialProperty(sceneNode, materialIndex, propertyName, value)

Sets the property of the material of a scene node.
Parameters:

Possible material parameters are:

Parameter name Possible values
Type "solid", "lightmap", "lightmap_add", "lightmap_m2", "lightmap_m4", "reflection_2layer", "trans_add", "trans_alphach", "trans_reflection_2layer"
Texture1 A texture for texture layer 1
Texture2 A texture for texture layer 2
Lighting true or false to enable or disable dynamic lighting
Backfaceculling true or false to enable or disable backface culling


Also, if you are writing a plugin for the editor, there are some advanced properties available: "Type", "Ambient", "Diffuse", "Emissive", "Specular", "Shininess", "Param1", "Param2", "Texture1", "Texture2", "Texture3", "Texture4", "Wireframe", "GouraudShading", "Lighting", "ZBuffer", "ZWriteEnable", "BackfaceCulling", "BilinearFilter", "TrilinearFilter", "AnisotropicFilter", "FogEnable", "NormalizeNormals".

Example:

var s = ccbGetSceneNodeFromName("cubeMesh1");
ccbSetSceneNodeMaterialProperty(s, 0, "Lighting", true);
Makes a node with the name 'cubeMesh1' to use dynamic light.

Example 2:

var node = ccbGetSceneNodeFromName("cubeMesh1");
var tex = ccbLoadTexture("example.jpg");
ccbSetSceneNodeMaterialProperty(node, 0, "Texture1", tex);
Loads a texture and sets it as material onto an object name 'cubeMesh1'.

ccbGetSceneNodeProperty(sceneNode, propertyName)

Gets the property value of a scene node. The propertyName is also the name displayed in the property window of the editor in the left column. Possible names depend on the scene node type, but usually names like 'Name', 'Position', 'PositionAbs' (absolute position), 'Rotation', 'Scale', 'Visible' are available at least. Please note that the name is case sensitive.

Example:

var s = ccbGetSceneNodeFromName("cubeMesh1");

var position = ccbGetSceneNodeProperty(s, "Position");

print("The cube is at " + position);
If the scene contains a scene node with the name 'cubeMesh1', this example prints something like "The cube is at (-2.15537, -0.751433, -15.6934)"

 

Also see ccbSetSceneNodeProperty().

ccbSetSceneNodeProperty(sceneNode, propertyName, value)
ccbSetSceneNodeProperty(sceneNode, propertyName, x, y, z)
ccbSetSceneNodeProperty(sceneNode, propertyName, r, g, b)

Sets the property value of a scene node. The propertyName is also the name displayed in the property window of the editor in the left column. Possible names depend on the scene node type, but usually names like 'Name', 'Position', 'Rotation', 'Scale', and 'Visible' are available at least. Please note that the name is case sensitive. When setting a vector, you can use x,y,z parameters, or a vector3d object. For colors, you can use a single int or 3 RGB values like 255,128,0.

Examples:

var s = ccbGetSceneNodeFromName("cubeMesh1");
ccbSetSceneNodeProperty(s, "Visible", false);
Makes a node with the name 'cubeMesh1' invisible.

var s = ccbGetSceneNodeFromName("cubeMesh1");
ccbSetSceneNodeProperty(s, "Rotation", 20, 90, 0);
Changes the rotation of a node with the name 'cubeMesh1' to (20, 90, 0)
Also see ccbGetSceneNodeProperty().

ccbSetSceneNodePositionWithoutCollision(sceneNode, x, y, z)

Sets a new position of a scene node, even if the scene node has a 'collide with walls' behavior attached to it. So it it possible to move such a scene node through walls. Note that you have to ensure that the new position of the scene node is not inside a wall, otherwise the node will be stuck.

Examples:

var s = ccbGetSceneNodeFromName("cubeMesh1");
ccbSetSceneNodePositionWithoutCollision(s, -22.097015, 9.848448, -40.738777);
Makes a scene node change its position, independent of its collision behavior.

ccbRegisterKeyDownEvent(funcstr)

Note: This function is not available in the editor, and only in the Windows and Mac OS X app target. For getting key events, use the behavior functions onKeyEvent().

Registers a function for receiving a key down event. The function registered must take one parameter which will be the key code.

Examples:
ccbRegisterKeyDownEvent("keyPressedDown");

function keyPressedDown(keyCode)
{
print("A key was pressed down:" + keyCode);
}
Prints which key was pressed when it is pressed.


ccbRegisterKeyUpEvent(funcstr)

Note: This function is not available in the editor, and only in the Windows and Mac OS X app target. For getting key events, use the behavior functions onKeyEvent().

Registers a function for receiving a key down event. The function registered must take one parameter which will be the key code.

Examples:

ccbRegisterKeyUpEvent("keyPressedUp");

function keyPressedUp(keyCode)
{
print("A key was left up:" + keyCode);
}
Prints which key was left up when it is left up.

ccbRegisterMouseDownEvent(funcstr)

Note: This function is not available in the editor, and only in the Windows and Mac OS X app target. For getting mouse events, use the behavior functions onMouseEvent().

Registers a function for receiving a key down event. The function registered must take one parameter which will be the mouse button pressed (1 for left button, 2 for right button, 3 for middle button).

Examples:

ccbRegisterMouseDownEvent("mousePressedDown");

function mousePressedDown(button)
{
print("A mouse button was presssed down:" + button);
}
Prints which mouse button was pressed when it is pressed

ccbRegisterMouseUpEvent(funcstr)

Note: This function is not available in the editor, and only in the Windows and Mac OS X app target. For getting mouse events, use the behavior functions onMouseEvent().

Registers a function for receiving a key down event. The function registered must take one parameter which will be the mouse button left up (1 for left button, 2 for right button, 3 for middle button)

Examples:

ccbRegisterMouseUpEvent("mouseLeftUp");

function mouseLeftUp(button)
{
print("A mouse button was left up:" + button);
}
Prints which mouse button was left up when it is left up.

ccbRegisterOnFrameEvent(func)
ccbUnregisterOnFrameEvent(func)

Note: These functions are not available in the editor.

ccbRegisterOnFrameEvent registers a function for receiving a 'on frame' event, an event which is called every frame the screen drawn. The function registered must take no parameters. Inside this function, it is possible to draw own, custom things like user interfaces.
After you no longer need events, call ccbUnregisterOnFrameEvent() to unregister your function.

Examples:

function onFrameDrawing()
{
// draw a red, transparent rectangle at the position of the mouse
var mouseX = ccbGetMousePosX();
var mouseY = ccbGetMousePosY();

ccbDrawColoredRectangle(0x77ff0000, mouseX-10, mouseY-10, mouseX+10, mouseY+10);
}

ccbRegisterOnFrameEvent(onFrameDrawing);
Draws a red rectangle at the position of the mouse cursor (or at the center if the mouse cursor is controlling a FPS style camera) 

ccbDrawColoredRectangle(color, x1, y1, x2, y2)

Note: This function is not available in the editor.

Draws a colored rectangle. This  function can only be used inside a frame event function which must have been registered with ccbRegisterOnFrameEvent().

The color is a 32 bit value with alpha, red, green and blue components (0xaarrggbb, like the color values known from HTML but with an alpha channel value added in front, for the transarency). 0x77ff0000 is for example a transparent red, 0xff0000ff is a not-transparent blue.

Examples:

function onFrameDrawing()
{
// draw a red, transparent rectangle at the position of the mouse
var mouseX = ccbGetMousePosX();
var mouseY = ccbGetMousePosY();

ccbDrawColoredRectangle(0x77ff0000, mouseX-10, mouseY-10, mouseX+10, mouseY+10);
}

ccbRegisterOnFrameEvent(onFrameDrawing);
Draws a red rectangle at the position of the mouse cursor (or at the center if the mouse cursor is controlling a FPS style camera) 

ccbDrawTextureRectangle(file, x1, y1, x2, y2)

Note: This function is not available in the editor.

Draws a textured rectangle. This  function can only be used inside a frame event function which must have been registered with ccbRegisterOnFrameEvent().

This function will ignore the alpha channel of the texture. Use ccbDrawTextureRectangleWithAlpha if you want the alpha channel to be taken into account as well.

Examples:

function onFrameDrawing()
{
// draw a red, textured rectangle at the position of the mouse
var mouseX = ccbGetMousePosX();
var mouseY = ccbGetMousePosY();

ccbDrawTextureRectangle("someFile.png", mouseX-100, mouseY-100, mouseX+100, mouseY+100);
}

ccbRegisterOnFrameEvent(onFrameDrawing);
Draws a textured rectangle at the position of the mouse cursor (or at the center if the mouse cursor is controlling a FPS style camera) 

ccbDrawTextureRectangleWithAlpha(file, x1, y1, x2, y2)

Note: This function is not available in the editor.

Draws a textured rectangle with alpha channel. This  function can only be used inside a frame event function which must have been registered with ccbRegisterOnFrameEvent(). 

This function will take the alpha channel of the texture into account, use ccbDrawTextureRectangleWithAlpha the texture does not have an alpha channel.

Examples:

function onFrameDrawing()
{
// draw a red, textured rectangle at the position of the mouse
var mouseX = ccbGetMousePosX();
var mouseY = ccbGetMousePosY();

ccbDrawTextureRectangleWithAlpha("someFile.png", mouseX-100, mouseY-100, mouseX+100, mouseY+100);
}

ccbRegisterOnFrameEvent(onFrameDrawing);
Draws a textured rectangle at the position of the mouse cursor (or at the center if the mouse cursor is controlling a FPS style camera) 


ccbGet3DPosFrom2DPos(x,y)

Returns the 3d position of a 2d position on the screen. Note: A 2d position on the screen does not represent one single 3d point, but a actually a 3d line. So in order to get this line, use the 3d point returned by this function and the position of the current camera to form this line.

Examples:

var mouseX = ccbGetMousePosX();
var mouseY = ccbGetMousePosY();

var anode = ccbGetSceneNodeFromName("somenode");
var pos3d = ccbGet3DPosFrom2DPos(mouseX, mouseY);
ccbSetSceneNodeProperty(anode, "Position", pos3d);
Sets the position of a scene node with the name 'somenode' to the 3d position behind the mouse cursor.


ccbGet2DPosFrom3DPos(x,y,z)

Returns the 2D position of a 3D position or nothing if the position would not be on the screen (for example behind the camera).

Examples:

var pos = ccbGet2DPosFrom3DPos(20, 30, 30);  
print("Position on screen: " + pos);
Prints the position of a 3d coordinate in 2D.

ccbGetCollisionPointOfWorldWithLine(startX, startY, startZ, endX, endY, endZ)

Returns the collision point with a line and the world. Returns null if there is no collision.



ccbDoesLineCollideWithBoundingBoxOfSceneNode(node, startX, startY, startZ, endX, endY, endZ)

Returns if the bounding box of the given scene node collides with the line between two given points.

Examples:

function onFrameDrawing()
{
var mouseX = ccbGetMousePosX();
var mouseY = ccbGetMousePosY();

// test collision

var cube = ccbGetSceneNodeFromName("cubeMesh");
var endPoint3d = ccbGet3DPosFrom2DPos(mouseX, mouseY);
var startPos3D = ccbGetSceneNodeProperty(ccbGetActiveCamera(), "Position");

if (ccbDoesLineCollideWithBoundingBoxOfSceneNode(cube, startPos3D.x, startPos3D.y,
startPos3D.z, endPoint3d.x, endPoint3d.y, endPoint3d.z))
{
ccbDrawColoredRectangle(0x77ff0000, mouseX-10, mouseY-10, mouseX+10, mouseY+10);
}
else
ccbDrawColoredRectangle(0x770000ff, mouseX-10, mouseY-10, mouseX+10, mouseY+10);
}

ccbRegisterOnFrameEvent(onFrameDrawing);
Draws a red rectangle at the position of the mouse cursor (or the center of the screen if controlled by a FPS style camera controller) if the mouse cursor is over a scene node named 'cubeMesh', and draws a blue rectangle if not.

ccbEndProgram()

Note: This function is not available in the editor.

Ends the application. On Flash and WebGL, this closes the window (if it was opened before as popup by a script).



ccbGetMousePosX()

Note: This function is not available in the editor.

Returns the current X position of the mouse cursor in pixels.

Examples:

function onFrameDrawing()
{
// draw a red, transparent rectangle at the position of the mouse
var mouseX = ccbGetMousePosX();
var mouseY = ccbGetMousePosY();

ccbDrawColoredRectangle(0x77ff0000, mouseX-10, mouseY-10, mouseX+10, mouseY+10);
}

ccbRegisterOnFrameEvent(onFrameDrawing);
Draws a red rectangle at the position of the mouse cursor (or at the center if the mouse cursor is controlling a FPS style camera) 

ccbSetMousePos()

Note: This function is not available in the editor. This function is only available in the Windows .exe and macOS .app target.

Set new position of the mouse on the screen



ccbGetMousePosY()

Note: This function is not available in the editor.

Returns the current Y position of the mouse cursor in pixels.

Examples:

function onFrameDrawing()
{
// draw a red, transparent rectangle at the position of the mouse
var mouseX = ccbGetMousePosX();
var mouseY = ccbGetMousePosY();

ccbDrawColoredRectangle(0x77ff0000, mouseX-10, mouseY-10, mouseX+10, mouseY+10);
}

ccbRegisterOnFrameEvent(onFrameDrawing);
Draws a red rectangle at the position of the mouse cursor (or at the center if the mouse cursor is controlling a FPS style camera) 

ccbGetScreenWidth()

Note: This function is not available in the editor.

Returns the current with of the screen in pixels.


ccbGetScreenHeight()

Note: This function is not available in the editor.

Returns the current with of the screen in pixels.


ccbSetCloseOnEscapePressed(bClose)

By default, the application on Windows and Mac OS X will close when the user presses escape in debug mode. By calling ccbSetCloseOnEscapePressed(false) will disable this feature. On other targets, this function has no effect.



ccbSetCursorVisible(bVisible)

Note: This function may not do anything depending on the target it runs on (web apps for example have no access to the cursor).

Sets if the mouse cursor is visible or not. Call ccbSetCursorVisible(false) to make it invisible, call ccbSetCursorVisible(true) to make visible again.



ccbSwitchToScene(sceneName)

Note: This function is not available in the editor.

Switch to the scene with the specified name. ccbSwitchToScene("my scene") will switch to a scene named "my scene" if there is one. Note: The name is case sensitive.



ccbPlaySound(filename)

Will play a sound or music file. The following file formats are supported on Windows and Mac OS X: WAV, OGG, MOD, XM, IT, S3M. For the other targets, this depends on the browser and platform. But usually, MP3 and OGG should be fine.




ccbStopSound(filename)

Will stop a sound or music, which has been started either by ccbPlaySound or the "Play Sound" action.




ccbSetCopperCubeVariable(varname, value)

Will set a CopperCube variable to a certain value. CopperCube variables can be set and changed using the Variable Actions in the editor.




ccbGetCopperCubeVariable(varname)

Will get the value of a CopperCube variable. CopperCube variables can be set and changed using the Variable Actions in the editor.




ccbReadFileContent(filename)

Note: This function may not do anything depending on the target it runs on (websites for example have no access to the file system).

reads a full (text) file into a string




ccbWriteFileContent(filename, content)

Note: This function may not do anything depending on the target it runs on (websites for example have no access to the file system).

writes a full (text) file from a string




ccbFileExist(filename)

Note: This function may not do anything depending on the target it runs on (websites for example have no access to the file system).

tests if a file exists




ccbFileDelete(filename)

Note: This function may not do anything depending on the target it runs on (websites for example have no access to the file system).

deletes a file from disk




ccbInvokeAction(actionId, currentNode)

Starts a CopperCube action. To be used together with extension script behaviors or actions: If some action was defined in the editor to be executed for this behavior or action, you can run it with this command.

Parameters:

Example:

Assume you have a script extension action with the following definition in it:

<property name="ActionWhenFinished" type="action" />

This means that the JavaScript instance of this action will have a property named 'ActionWhenFinished' at runtime then, referencing the action defined by the user in the editor. You can start this action using this call then:
ccbInvokeAction(this.ActionWhenFinished);
                                                        

Note that most actions assume the 'currentNode' to be set for this action, so set this to a real action which makes sense to be the current node in this context, like this:
ccbInvokeAction(this.ActionWhenFinished, this.TheNodeIamCurrentlyWorkingWith);
                                                        



ccbGetCurrentNode()

Returns the current scene node. When running some JavaScript code via an 'execute JavaScript' action, there is always a "current node" set, usually the node in which the action is being run.




ccbCleanMemory()

Cleans up the current memory usage. Tries to free up as much memory as possible by freeing up unused textures, vertex and index buffers and similar. This is useful for calling it for example after switching scenes to increase performance of the app. May have different effects on different platforms.



ccbGetPlatform()

Returns a string identifying the system the CopperCube app is running on. For now, the following return types are possible:



ccbSwitchToFullscreen(enablePointerLock, elementToSwitchToFullscreen, switchBackToWindowed)

When running as Windows .exe, WebGL or Flash app, switches the app to fullscreen, and also enables pointer lock for Flash or WebGL, if wanted. Note that this function does nothing or doesn't even exist on platforms which are not Windows .exe, WebGL or Flash.

Parameters:


There are a few limitations to this function:

ccbDoHTTPRequest(url, callback)
ccbCancelHTTPRequest(connectionIdToCancel)

Note: This function is not available in the editor.

ccbDoHTTPRequest() makes a GET network request via HTTP to any web server, and ccbCancelHTTPRequest can cancel this request while it is running. This allows to communicate with any web/database/multiplayer server with ease. For ccbDoHTTPRequest parameters are:


The function returns an unique Id, for identifying this request. You can use this id as parameter for ccbCancelHTTPRequest(), to cancel the running request, if it takes too long for example.

Note: On some targets, like WebGL and Flash, it is not possible to do cross domain requests because of security reasons. Usually you should only do requests to the same server your script is running on.

Examples:

function finishedRequest(dataReceived)
{
  print("finished request! Data size:" + dataReceived.length);
}

ccbDoHTTPRequest("http://www.ambiera.com/index.html", finishedRequest);
Downloads the front page of www.ambiera.com and shows how many bytes the page contains.



ccbCreateMaterial(vertexShader, fragmentShader, baseMaterialType, shaderCallback)
ccbSetShaderConstant(type, name, value1, value2, value3, value4)

Note: This function is not available in the editor.

ccbCreateMaterial() creates a new material based on vertex and pixel shaders. See Shader Programming in CopperCube for details.
Paramers for ccbCreateMaterial() are:

The function returns an unique material id, which you can use to set the material type of any node to your new material using ccbSetSceneNodeMaterialProperty(). It returns -1 if an error happened.


ccbSetShaderConstant() may only be called during the material callback. Paramers for ccbSetShaderConstant() are:


Examples can be found in Shader Programming in CopperCube.


ccbSetPhysicsVelocity(sceneNode, x, y, z)

Note: This function is not available in the editor.

Sets the linear velocity of an object simulated by the physics engine. This only works when physics simulation is turned on and available for the current platform.
Paramers are:



ccbUpdatePhysicsGeometry()

Note: This function is not available in the editor.

Updates the collision geometry of the physics simulation. Call this when you modified the static geometry of the world and want the physics simulation to respect that. This only works when physics simulation is turned on and available for the current platform.


ccbAICommand(sceneNode, command, param)

Note: This function is not available in the editor.

Sends a command to an object with the behavior 'Game Actor with Health'. In this way, it is possible to make the object move somewhere, attack something, or similar.
Paramers are:


Possible commands and parameters for the AI are:
Examples:

var s = ccbGetSceneNodeFromName("man");
var otherNode = ccbGetSceneNodeFromName("soldier");
ccbAICommand(s, "attack", otherNode);
Lets an AI with the name "man" attack another node with the name "soldier".

var s = ccbGetSceneNodeFromName("man");
ccbAICommand(s, "moveto", new vector3d(-16.4, 4.5, -38.7));
Lets an AI with the name "man" move to the position -16.4, 4.5, -38.7.


ccbSteamSetAchievement(achievementId)

Note: This function only works in the Windows .exe target.

Sets a Steam achievement as achieved. The 'achievementId' is the string you specify in the Steam admin interface when creating the achievement. Will draw the achivement overlay as well if set for the first time. See the Steam support description for details.




ccbSteamResetAchievements()

Note: This function only works in the Windows .exe target.

Resets all Steam achievemts for the current user. See the Steam support description for details.




ccbSaveScreenshot(filename)

Note: This function only works in the Windows .exe and Mac OS X .app target.

Makes a screenshot: Saves the current content of the screen to a image file on the disk. The only parameter must be a file name, like "test.jpg" or "C:\\test.jpg". Supported file formats are .bmp, .jpg, .tga, .psd, .pcx, .png and .ppm.




ccbSaveTexture(texture, filename)

Note: This function only works in the editor and the Windows .exe and Mac OS X .app target.

Saves a texure as file. The first parameter is the texture, the second must be a file name, like "test.jpg" or "C:\\test.jpg". Supported file formats are .bmp, .jpg, .tga, .psd, .pcx, .png and .ppm.


Example:
var s = ccbGetSceneNodeFromName("cubeMesh1");
var t = ccbGetSceneNodeMaterialProperty(s, 0, "Texture1");
ccbSaveTexture(t, "testout.jpg");



ccbSwitchToCCBFile(filename)

Note: This function only works in the Windows .exe and Mac OS .app target.

If your game uses a lot of data like textures / sounds / scenes etc, you can dynamically load levels and scenes at runtime using this function. When invoked this will wipe the memory and load/start that new file. This makes it possible to split your game up easily into multiple files. Example:

ccbSwitchToCCBFile("otherfile.ccb");




print(text)

Prints a string into the output window of the editor or into the debug console window of the app.




alert(text)

This function is only available in the CopperCube editor
Shows a string in a modal message box.


Examples:

alert("Hello World");
Shows the text Hello World! in a modal message box


confirm(text)

This function is only available in the CopperCube editor
Shows a string in a modal message box and asks to press either OK or CANCEL.


Examples:

if (confirm("Continue?"))
   print("User pressed OK!");
else
   print("User pressed Cancel.");
Shows the text Continue? in a modal message box and prints a text depending on what button the user pressed.


prompt(text, edittext)

This function is only available in the CopperCube editor
Shows an edit box in a modal message box and asks to press either OK or CANCEL.


Examples:

var ret = prompt("Please enter a text", "some text");
if (ret)
  print("User entered this:" + ret);
else
  print("User pressed cancel");
Shows the text Please enter a text? in a modal message box and an additional edit box.


system(commandline)
system(commandline, hideWindow)

Executes the given command line. Can be used to run commands or launch programs.
If the optional parameter 'hideWindow' is set to 'true', then no console window will be shown when running this command.


Examples:

system("type C:\\Windows\\WindowsUpdate.log");
On windows, this will print the content of the file WindowsUpdate.log (if available) to the screen.
system("type C:\\Windows\\WindowsUpdate.log", true);
Same, but without the console window becoming visible.


Mesh Editing

Functions for manipulating the 3D geometry in CopperCube:
Note: Only available in the editor and on the Mac OS X and Windows app targets, for now.

ccbGetSceneNodeMeshBufferCount(node) - Returns amount of mesh buffers in a scene node
ccbRemoveMeshBuffer(node, meshbufferindex) - Erases a mesh buffer in a scene node
ccbAddMeshBuffer(node) - Adds a new mesh buffer to a scene node

ccbGetMeshBufferVertexCount(node, meshbufferindex) - Returns amount of vertices of a mesh buffer
ccbGetMeshBufferIndexCount(node, meshbufferindex) - Returns amount of indices in a mesh buffer

ccbAddMeshBufferIndex(node, meshbuffernumber, indexvalue) - Adds a new index value to a mesh buffer
ccbGetMeshBufferIndexValue(node, meshbuffernumber, indexnumber) - Returns the value of an index in a mesh buffer
ccbSetMeshBufferIndexValue(node, meshbuffernumber, indexnumber, indexvalue) - Changes the value of an index

ccbAddMeshBufferVertex(node, meshbuffernumber, vertexposition) - Adds a vertex to a mesh buffer
ccbGetMeshBufferVertexPosition(node, meshbufferindex, vertexindex) - Returns 3D position of a vertex
ccbSetMeshBufferVertexPosition(node, meshbufferindex, vertexindex, vertex) - Sets the 3D position of a vertex

ccbGetMeshBufferVertexTextureCoord(node, meshbufferindex, vertexindex) - Gets texture coordinate of a vertex
ccbSetMeshBufferVertexTextureCoord(node, meshbufferindex, vertexindex, coord) - Sets texture coordinate of a vertex

ccbGetMeshBufferVertexNormal(node, meshbufferindex, vertexindex) - Returns normal of a vertex
ccbSetMeshBufferVertexNormal(node, meshbufferindex, vertexindex, coord) - Sets the normal of a vertex

ccbGetMeshBufferVertexColor(node, meshbufferindex, vertexindex) - Returns color of a vertex
ccbSetMeshBufferVertexColor(node, meshbufferindex, vertexindex, color) - Sets color of a vertex

ccbUpdateSceneNodeBoundingBox(node) - Calculates the new bounding box, necessary after changing the geometry

The functions listed above give access to all static 3D geometry in the editor, and make it possible to change it. It works like this: Every node which has 3D geometry in copperCube has a 3D mesh, representing this 3D geometry. A 3D mesh consists of several mesh buffers, one for each material. In each mesh buffer, there are several vertices, defining the 3D position of each point of the 3D geometry, and several indices, connecting theses points to form triangles. There is always a multiple of three indices, like for example 0, 2, 1 forming a triangle of the first three indices.


Examples:

Polygon and Material counting:
var meshnode = editorGetSelectedSceneNode();
var bufferCount = ccbGetSceneNodeMeshBufferCount(meshnode);
if (bufferCount == 0)
  alert('The selected node has no 3D geometry.');
else
{
  var totalVertexCount = 0;
  var totalIndexCount = 0;
  for (var i=0; i<bufferCount; ++i)
  {
    totalVertexCount += ccbGetMeshBufferVertexCount(meshnode, i);
    totalIndexCount += ccbGetMeshBufferIndexCount(meshnode, i);
  }

  alert('The selected node has ' + (totalIndexCount/3) + ' polygons ' +
        totalVertexCount + ' vertices, and ' +
        bufferCount + ' materials.' );
}
This will print the total amount of polygons, vertices and materials of the selected node in the editor, like this: The selected node has 12 polygons 36 vertices, and 1 materials. .

Modifying a 3D mesh:
var meshnode = editorGetSelectedSceneNode();
var bufferCount = ccbGetSceneNodeMeshBufferCount(meshnode);
if (bufferCount == 0)
  alert('The selected node has no 3D geometry.');
else
{
  for (var i=0; i<bufferCount; ++i)
  {
    var vertexcount = ccbGetMeshBufferVertexCount(meshnode, i);
    for (var v=0; v<vertexcount; ++v)
    {
       var pos = ccbGetMeshBufferVertexPosition(meshnode, i, v);
        
       pos.x *= 2; 
       pos.y *= 2;
       pos.z *= 2;
        
       ccbSetMeshBufferVertexPosition(meshnode, i, v, pos);
    }
  }  
  ccbUpdateSceneNodeBoundingBox(meshnode);
}
This will scale all vertices of the currently selected node in the editor by the factor 2. Note: this is different from setting the scale value of the node, it actually will move all vertex positions.
There is also tutorial available on how to Create 3D geometry using JavaScript.



editorAddSceneNode(type)
editorAddSceneNode(type, x, y, z)
editorAddSceneNode(type, x, y, z, size)

Creates a new scene node of type (must be a string) at position x, y, z, applies the default editor settings to it, selects it and updates all editor views. This is just like if the user used the 'edit->insert' command from the editor menu.

The parameter 'type' must be one of these strings:

"cube", "sphere", "cylinder", "cone", "plane", "light", "camera", "billboard", "skybox", "hotspot", "3dsound","overlay2d"

x,y,z is the position where the scene node will be created. If not used, the position will be the default editor position.

The parameter 'size' is optional, and specifies a default size for the object, like 10 as widht and height of the cube.

Returns: The new scene node.

Examples:

var count = 3;
var size = 12;
for (var x=0; x<count; ++x) for (var y=0; y<count; ++y) for (var z=0; z<count; ++z) editorAddSceneNode("cube", x*size, y*size, z*size);
This example will create 27 cubes placed in a hypercube.

editorAddSceneNode("billboard");
This example will create a billBoard placed directly on front of the 3d camera in the main view.

 


editorFocusPosition(position)

Lets the cameras of all view ports focus the given position.

Example:

editorFocusPosition(vector3d(0,10,20));
Lets all cameras focus the position (0,10,20)

 


editorGetSelectedSceneNode()

Returns the currently selected scene node or 0 if nothing selected.

Example:

var s = editorGetSelectedSceneNode();

if (s) alert("Name of the selected node is: '" + ccbGetSceneNodeProperty(s, "Name") + "'"); else alert("nothing selected");
This example prints the name of the currently selected scene node.

 


editorGetSelectedTexture()

Returns the currently selected texture from the texture manager window.

Example:

var t = editorGetSelectedTexture();
var s = editorAddSceneNode("cube");

ccbSetSceneNodeMaterialProperty(s, 0, "Texture1", t);
if (t == "") print("added no texture"); else print("set texture to: " + t );
editorUpdateAllWindows();
Creates a new cube and applies the currently selected texture to it.

 


editorGetFileNameFromDialog()
editorGetFileNameFromDialog(message)
editorGetFileNameFromDialog(message, fileExtensions)
editorGetFileNameFromDialog(message, fileExtensions, isOpenDialog)

Shows a file selection dialog prompting the user to select a file and returns the selected filename or an empty string if the user cancelled.

Parameters:

Example:

var s = editorGetFileNameFromDialog("please select something");

if (s != "")
alert ("user selected " + s);
else
alert("user cancelled selection");
This example prompts the user to select a filename and prints which file was selected when finished.

 


editorUpdateAllWindows()

Updates all windows of the editor. Same as selecting the command Edit->UpdateAllWindows or pressing F5 in the editor.

Example:

editorAddSceneNode("cube");
editorUpdateAllWindows();
This example creates a new cube without default settings and updates the editor windows to show the new scene node in all windows.

 


editorRegisterMenuEntry(function, text)

Note: This function is only available in the editor.

Registers a new entry in the menu under 'Plugins' using the text provided in the 'text' parameter. When the menu command is selected by the user, the global function provided in the 'function' parameter will be executed.
Please note that editorRegisterMenuEntry only works when executed by a script run in the autostart mode, which means a script placed in the \Documents\CopperCube\plugins directory and with the extension '.js'.

Example:

function printHello()
{
print("Hello\n");
}

editorRegisterMenuEntry("printHello();", "Print Hello into the log");
Putting this script in a file named for example 'CopperCube\plugins\autostart_printhello.js' and starting the editor will add a new menu entry into the Tool menu which will print 'Hello'

 


editorSetSelectedSceneNode(node)

Sets a selected scene node. Set node to 0 to select nothing.

Example:

var s = editorAddSceneNode("sphere");
editorSetSelectedSceneNode(s);
Adds a scene node and selects it in the editor.

editorImportStatic3DMesh(filename)

Imports a static mesh file into the editor.

Returns the currently selected scene node or 0 if nothing imported.


Example:

var filename = 
  editorGetFileNameFromDialog("please select a mesh file to import");

editorImportStatic3DMesh(filename);
Imports a static mesh file which is selected by the user.

editorImportAnimated3DMesh(node)

Imports an animated mesh file into the editor.

Returns the currently selected scene node or 0 if nothing imported.


Example:

var filename = 
  editorGetFileNameFromDialog("please select a mesh file to import");

editorImportAnimated3DMesh(filename);
Imports an animated mesh file which is selected by the user.

ccbGetAnimatedJointCount(animatednode)

Note: This function is only available in the editor and the Windows and macOS target.

Returns the number of bones(joint) of an animated scenenode


ccbGetAnimatedJointName(animatednode,jointIndex)

Note: This function is only available in the editor and the Windows and macOS target.

Returns the name of the bone(joint) of an animated scenenode, joint index is the index of bone it should be less than the AnimatedJointCount


ccbGetCurrentAnimationFrame(animatednode)

Note: This function is only available in the editor and the Windows and macOS target.

Returns the current animation frame playing in the scene by the animated scenenode


ccbSetCurrentAnimationFrame(animatednode, framenumber)

Note: This function is only available in the editor and the Windows and macOS target.

Set the current animation to be played from a supplied animation framenumber.


ccbSetAnimationFromSceneNode(node1, node2, animationname)

Note: This function is not available in the editor. It is only available in the Windows and macOS target.

Set animation of animated scenenode from the animation of another animated scenenode that poses same skeleton(rig). First node is to which the animation will be applied, second node is from which the animation will be used, Animation name is the name of the animation that should be used.


ccbGetTextureWidth(texture)

Note: This function is only available in the editor and the Windows and macOS target.

Returns the Width of a texture.


ccbGetTextureHeight(texture)

Note: This function is only available in the editor and the Windows and macOS target.

Returns the Height of a texture.


ccbPauseAllSounds(pause)

Note: This function is only available in the editor and the Windows and macOS target.

Pause and unpause all sounds node (True means pause, False means unpause).


ccbSetDopplerEffectParameters(DopplerFactor, DistanceFactor)

Note: This function is only available in the editor and the Windows and macOS target.

Sets the DopplerEffect parameters to a different value. CC uses (1.0,1.0) as default parameters. The value ranges from 1.0 to 10.0


ccbRenderToTexture(node, camera, material index, Xresolution, Yresolution)

Note: This function is not available in the editor. It is only available in the Windows and macOS target.

Captures the screen and apply it as a texture on a scenenode.


ccbSplitScreen(cam1,x1,y1,x2,y2, cam2,x3,y3,x4,y4)

Note: This function is not available in the editor. It is only available in the Windows and macOS target.

Splits the Screen into two with 2 camera's rendering at the same time


ccbGetAnimatedJointPosition(animatednode,Jointindex)

Note: This function is only available in the editor and the Windows and macOS target.

Returns the Position of a bones(joint) of an animated scenenode


ccbSetGameTimerSpeed(speedFloat)

Note: This function is not available in the editor. It is only available in the Windows and macOS target.

Set the speed of the game timer to fast or slow


ccbEmulateKey(keyCode, pressedDown)

Note: This function is not available in the editor. It is only available in the Windows and macOS target.

Emulate a keyboard keyevent for the specified Key with the keycode


vector3d()
vector3d(x,y,z)

A class for holding 3 float coordinates - x, y, and z. Also provides helper methods:

Examples:

var v = new vector3d(0,0,0);
print(v);
prints (0, 0, 0)

var v1 = new vector3d(10,0,0);
var v2 = new vector3d(0,20,0);
var v3 = v1.add(v2);

v3.normalize();
print(v3 + " Length:" + v3.getLength())
prints (0.447214, 0.894427, 0) Length:1

 

Other routines available (for details please see the JavaScript Standard):

Math: Math.abs(x); Math.acos(x); Math.asin(x); Math.atan(x); Math.atan2(x, y); Math.ceil(x); Math.cos(x); Math.exp(x); Math.floor(x); Math.log(x); Math.pow(x, y); Math.random(); Math.round(), Math.sin(x); Math.sqrt(x); Math.tan(x);

Others All the JavaScript core functionality, like date, regexp, strings, arrays, etc.