Scripting
Scripting functions and events included in parcels and features.
Scriptingâ
The scripting interface lets you run javascript code on your own server and send updates into the world. Everyone that connects to the world sees the same view of your scripted parcel. You can write scripts inside any feature on a parcel, and access other features from that script.
Once you own a parcel, you can start using the feature scripting engine. Scripting in cryptovoxels is mainly client based. That is, most scripts will be run by the client and not the server.
The hierarchy is:
- The world
For scripting examples: click here
A more up-to-date documentation exists at blog.cryptovoxels.com
The parcelâ
This section includes properties, functions and events common to all plots.
Propertiesâ
ID - parcel.id = 500 - Returns an integer; the parcel's id
Owner - parcel.owner = "0x..." - Returns a string; the parcel's owner
Contributors - parcel.contributors = [] - Returns an array; the parcel's contributors list
allowLoggedInOnly - parcel.allowLoggedInOnly = false- Returns a boolean; When set to true, all non-logged in users will be kicked out.
isPrivate - parcel.isPrivate = false- Returns a boolean; Returns a boolean on whether the parcel is private or not. When set to true, all players not allowed on the parcel will be kicked out. See allow,disallow.
allowedWallets - parcel.allowedWallets = ["0x..."]- Returns an array; The list of wallets allowed to enter the parcel if the parcel is private. Cannot be set, use allow,disallow.
Note: Setting a parcel private using isPrivate will still require the user to be in the parcel first before being kicked out. isPrivate, allow, and disallow are disabled in free-spaces.
Functionsâ
getFeatures - parcel.getFeatures()â
- Returns: A list of Feature objects
let features = parcel.getFeatures();
getFeatureById - parcel.getFeatureById(id)â
- Arguments: id - the desired feature's id; a string
- Returns: a Feature object
let door = parcel.getFeatureById('door');
console.log('door:', door);
getFeatureByUuid - parcel.getFeatureByUuid(Uuid)â
- Arguments: Uuid - the desired feature's Uuid; a string
- Returns: a Feature object
let feature = parcel.getFeatureById('3ed2bdd2-7570-485d-85b3-e5fd950bf3c6');
console.log('feature:', feature);
getFeaturesByType - parcel.getFeaturesByType(type)â
- Arguments: type - the desired feature type; a string
- Returns: a list of Feature objects
let allVoxModels = parcel.getFeaturesByType('vox-model');
Valid types are:
'vox-model''button''image''sign''polytext''audio''nft-image''megavox''text-input''slider-input''video'
createFeature - parcel.createFeature(type)â
- Arguments: type - the desired feature type; a string
- Returns: a Feature object
let newFeature = parcel.createFeature('vox-model');
When a feature is created it has a scale and position of [0,0,0]. Thus, remember to give it a position and a scale once created. To learn how to that, scroll down to Feature object -:::caution Properties. :::
removeFeature - parcel.removeFeature(f)â
- Arguments: f - the feature to remove; a Feature object
- Returns: an event
let chair = parcel.getFeatureById('chairvox')
parcel.removeFeature(chair);
getPlayers - parcel.getPlayers()â
- Arguments: none
- Returns: a list of Player objects.
getPlayersWithinParcel - parcel.getPlayersWithinParcel()â
- Arguments: none
- Returns: a list of Player objects. Only those inside the parcel
fetchSnapshots(callback?) - parcel.fetchSnapshots(callback?)â
- Arguments: (optional) a callback function called when the snapshots have been fetched. The callback function must have one argument, which will be an array of snapshots.
- Returns: void
- Note This does not work in spaces.
setSnapshot(snapshot_id) - parcel.setSnapshot(snapshot_id)â
- Arguments: an integer that represents the id of the selected snapshot.
- Returns: void
- Note This does not work in spaces.
Example:
function myCallback(snapshots){
parcel.setSnapshot(snapshots[0].id)
}
feature.on('click',e=>{
parcel.fetchSnapshots(myCallback)
})
Two things to remember:
One, when reverting your parcel to a snapshot via scripting, it will not save on the server. (only the client can see). Note also that your script will be gone because it will be overwritten.
Two, if you edit the parcel AFTER calling setSnapshot it will save the snapshot on the server.
allow(wallet) - parcel.allow("0x..")â
Add wallet to the list of allowed wallets when the parcel is Private.
- Arguments: A wallet address;
- Returns: void;
disallow(wallet) - parcel.disallow("0x..")â
Remove wallet from the list of allowed wallets when the parcel is Private.
- Arguments: A wallet address;
- Returns: void;
isWalletAllowedIfPrivate(wallet) - parcel.isWalletAllowedIfPrivate("0x..")â
Checks if wallet is allowed inside when the parcel is Private.
- Arguments: A wallet address;
- Returns: void;
Parcel Eventsâ
Player enter - parcel.on('playerenter', (e)=> {})
- Returns: an event
econtaining a player object with the player's information. - eg:
e.player.name -> returns "Fayelure" - Fired when the player enters inside the parcel
Player leave - parcel.on('playerleave', (e)=> {})
- Returns: an event
econtaining a player object with the player's information. - eg:
e.player.wallet -> returns "0xdbw2fr8..." - Fired when the player leave the parcel
Player nearby - parcel.on('playernearby', (e)=> {})
- Returns: an event
econtaining a player object with the player's information. - eg:
e.player.name -> returns "Fayelure" - Fired when the player enters the streets surrounding the parcel
Player nearby - parcel.on('playeraway', (e)=> {})
- Returns: an event
econtaining a player object with the player's information. - eg:
e.player.name -> returns "Fayelure" - Fired when the player leaves the area/street near the parcel
Player objectâ
Available from parcel.getPlayers() or feature.on('click', e => console.log(e.player)). Currently is just an object, but will become a class in the future.
Attributes:â
player.name=> 'captainbenis.eth'player.wallet=> '0x2D891ED45C4C3EAB978513DF4B92a35Cf131d2e2'player.uuid=> avatar uuid for this instance of the player (player may have multiple tabs open with seperate avatars)
There are multiple ways to spoof player.wallet and player.name, do not trust or send funds to this address. We will tighten up the security in the future for the hosted scripting server and update these docs when we do.
Functions:â
teleportTo(coordinates) - player.teleportTo(coordinates)â
- Arguments: A string of coordinates such as
'N@43W,250N,1U' - Returns: void
- Disabled on players outside the parcel See Example
hasWearable(tokenId,collectionId?) - player.hasWearable(tokenId,collectionId)â
- Arguments: An integer
tokenIdwhich is the token id of that wearable, andcollectionIdwhich is the id of the collection that wearable belongs to. - Returns: true of false.
emote(emoji) - player.emote('đ')â
- Arguments: A string; An emoji that is supported by cryptovoxels.
For a list of supported emojis see Emojis
You can also find the list within scripting
console.log(emojis). - Returns: void
- Disabled on players outside the parcel Note: This function is throttled to 500ms
animate(animation) - player.animate('Dance')â
This function has been deprecated as it was too invasive. It will no longer work (March 2022)
- Arguments: A string; The name of an animation. The list of supported animations is:
Idle , Dance, Wave, Sitting, Spin, Savage, kick, Uprock, Floss, backflip
You can also find the list within scripting console.log(animations).
- Returns: void
- Disabled on players outside the parcel Note: This function is throttled to 10 seconds
hasEthereumNFT(contract,tokenId,successCallback?,failCallback?) - player.hasEthereumNFT('0x...',5,(hasNFT)=>.., (reason)=>..â
- Arguments:
Contract: string; The contract of the transaction.
tokenId: string or number; The token ID to check ownership of.
successCallback: Function; A function that will be called on success. The given argument is a boolean indicating whether the player has the NFT or not. For example
(hasNFT)=>{console.log('player has NFT :',hasNFT})failCallback: Function; A function that will be called on fail. The given argument is a string which is the reason the API failed. - returns: void
kick() - player.kick()â
- Arguments: None
- Returns: None
- Disabled on players outside the parcel
Events:â
move - player.on('move', (e)=> {...})
- Returns: an event e.
click - player.on('click', (e)=> {...})
- Returns: an event e.
chat - player.on('chat', (e)=> {...})
- Returns: an event e containing the text of the chat.
Feature objectâ
This section includes properties, functions and events common to all features.
For feature-specific properties and methods, go to the features page in features.
Propertiesâ
ID - feature.idâ
- Returns: an integer; the feature's id.
- Can be set. eg:
feature.id = 'myvoxId'
Type - feature.typeâ
- Returns: an string; the feature's type.
Types include:
- 'vox-model'
- 'button'
- 'image'
- 'sign'
- 'polytext'
- 'richtext'
- 'audio'
- 'video'
- 'youtube'
- 'nft-image'
- 'megavox'
- 'text-input'
- 'spawn-point'
Position - feature.positionâ
- Returns: a vector of form [x (double),y (double),z (double)].
- Can be set. eg:
feature.position.set(1, 0.72, 2)orfeature.position.y = 0.72orfeature.set({position:[1,0.72,2]}) - Position is in parcel space (not world space)
- Position is in metersinfo
See scripting examples for Positions
Scale - feature.scaleâ
- Returns: a vector of form [x (double),y (double),z (double)].
- Can be set. eg:
feature.scale.set(0.75, 0.75, 0.75)orfeature.scale.y = 0.75orfeature.set({scale:[0.75,0.75,0.75]})infoSee scripting examples for scales.
Rotation - feature.rotationâ
- Returns: a vector of form [x (double),y (double),z (double)].
- This is a proxy vector object. Calling
seton it eg:feature.rotation.set(0, 0, 0)orfeature.rotation.y = 3.14. orfeature.set({rotation:[0,3.14,0]})
See scripting examples for rotations.
While the in-world user interface uses degrees, the scripting engine uses radians. 3.14 (pi) will rotate the item 180 degrees.
nb: Position, scale and rotation are Vector3 object, with a ES6 proxy watching for updates to x, y and z.
- Calling
feature.position.x = 1works. - Calling
feature.position.copyFrom(new Vector3(1, 2, 3))works - Calling
feature.position.set(1, 2, 3)works. - Calling
feature.position.addInPlace(new Vector3(4, 5, 6))works - Etc.
- Calling
feature.position = new Vector3()won't work.
Description - feature.descriptionâ
- Returns: a dictionary of all the attributes you can
seton the object. - Notes: Don't mutate the returned object, assume it's read only. Call
setto update attributes in the description.
Methodsâ
clone() - feature.clone()â
- Returns: a clone feature
remove() - feature.remove()â
Remove feature from the parcel
- Returns: void
set(dict) - feature.set({ url: 'http://', ... })â
Set the properties of the feature
- Argument: an object with a key and a value such as {url:'http...'}
- Returns: void
get() - feature.get('url')â
Set the properties of the feature
- Returns: any
createAnimation() - feature.createAnimation('position')â
see Animation API
startAnimations() - feature.startAnimations([animation])â
see Animation API
createBasicGui(id?,options?) - feature.createBasicGui('id',...)â
see GUI API
- Returns a FeatureBasicGui object.
removeGUI() - feature.removeGUI()â
removes the GUI present on this feature
Eventsâ
on clickâ
- Gets a click event. Can be obtain via click on
buttonorvox modelsor via trigger onimage,vox-model,nft-model,particle-system,megavox.
feature.on('click', (e)=> {
console.log(e.player) // Player object
console.log(e.point) // Vector3 of click in parcel space
console.log(e.normal) // Vector3 of normal at face where clicked
})
on triggerâ
- Gets a trigger event. Similar to the click event. However this event is fired only by features with
isTriggeredset totrueand if the user is within the trigger's range.
feature.on('trigger', (e)=> {
console.log(e.player) // Player object
})
Feature specificâ
For feature specific properties, go to your desired feature and scroll down to Scripting Properties.
Basic GUI Objectâ
This section includes properties, functions and events for basic GUIs.
Propertiesâ
ID - gui.idâ
- Returns: string, the GUI's unique id.
- Can be set. eg:
feature.id = 'myvoxId'
Uuid - gui.uuidâ
- Returns: string, the GUI's unique uuid.
feature - gui.featureâ
- Returns: Feature, the parent feature
showing - gui.showingâ
- Returns whether the GUI is shoing or not
listOfControls - gui.listOfControlsâ
- Returns: an array of guiControls.
defaultControl - gui.defaultControlâ
- Returns an example of default control.
Methodsâ
addButton(text=null,positionInGrid=[0,0],id=null) - gui.addButton('My button')â
- Arguments:
- text (optional): a string,
- Id (optional): a string,
- positionInGrid (optional): An array of 2 integers, the first integer being the row number, and the second being the column number.
- Returns: a guiControl of the button type.
addText(text=null,positionInGrid=[0,0],id=null) - gui.addtext('My text')â
- Arguments:
- text (optional): a string,
- positionInGrid (optional): An array of 2 integers, the first integer being the row number, and the second being the column number.
- Returns: a guiControl of the text type.
getControlById(id) - gui.getControlById('buttonId')â
- Arguments: a string
- Returns: a guiControl or Null
getControlByUuid(id) - gui.getControlByUuid('wdwdw-dwd-wd..')â
- Arguments: a string
- Returns: a guiControl or Null
getControlByPosition(array) - gui.getControlByUuid([1,0])â
- Arguments: an array of 2 integer.
- Returns: a guiControl or Null
show() - gui.show()â
- Returns: void
destroy() - gui.destroy()â
- Returns: void
guiControl Objectâ
This section includes properties, functions and events for basic GUIs.
Propertiesâ
gui - guiControl.guiâ
- Returns: a FeatureBasicGui object, the parent GUI
id - guiControl.idâ
- Returns: string, the guiControl's id.
- Can be set. eg:
control.id = 'myId'
Uuid - guiControl.uuidâ
- Returns: string, the guiControl's unique uuid.
type - guiControl.typeâ
- Returns: string, the type of guiControl
At the moment this can only return
buttonortext
text - guiControl.textâ
- Returns: string, the text of the Text or Button control
positionInGrid - guiControl.positionInGridâ
- Returns: an Array of 2 integers, the position of the control in a grid. Default is [0,0]
summary - guiControl.summaryâ
- Returns: an object with the basic information of the control.
- Useful for console.log in the grid
Methodsâ
update() - guiControl.update()â
Update the control.
- Returns void
A more up-to-date documentation exists at blog.cryptovoxels.com