# Basic Script examples and interpretations

  • Once we have a basic understanding of the scripting mechanism, we can try out what scripts can do.
  • Create a new developer mode map in the game, try the following example!

Example 1:

Put stone block in 3 different coordinates

Block:replaceBlock(105, 3, 7, 3, FACE_DIRECTION.DIR_POS_Y)
Block:replaceBlock(105, 5, 7, 5, FACE_DIRECTION.DIR_POS_Y)
Block:replaceBlock(105, 7, 7, 7, FACE_DIRECTION.DIR_POS_Y)

--Parameter definition:
--Block:replaceBlock(Block ID, X coordinate, Y coordinate, Z coordinate, orientation)

Code Dissected:

  • To place a Block, firstly we need to use the Block Management APIs, so we use theBlock Management API
  • The replaceBlock() API is used to place the block.
  • After checking the APIs, there are 5 parameters in replaceBlock() that need to be filled in, which are:
  • This code is called after the coordinates are selected to generate blocks in these three locations

Example 2:

Place the same stone block at three coordinates

local blockId = 105  --declare a variable named blockID, and valued it with block id 105
local blockFace = FACE_DIRECTION.DIR_POS_Y --declare a variable named blockFace, and valued it with coordinates
Block:replaceBlock(blockId, 3, 7, 10, blockFace)
Block:replaceBlock(blockId, 5, 7, 10, blockFace)
Block:replaceBlock(blockId, 7, 7, 10, blockFace)

Code Dissected:

  • Since all three blocks are the same, we can use a "variable" instead of an ID
  • The variable is the same as the name, so you name it, and put in the ID of the block.
  • local blockId = 105 This is how to put the ID in a variable.
  • If you want to place down other blocks, just change the local blockId = 105

Example 3:

Spawn a Jockey in front of a coordinate

local actorId = 3102 --declare a variable, put in monster ID
local num1, num2 = 1, 2 --declare 2 variables, put in 1 monsters, and 2 monsters
local x, y, z = 5, 7, 5 --declare 3 variables, put in the coordinates
World:spawnCreature(x, y, z, actorId, num1+num2)

--Parameter definition:
--World:spawnCreature(X coordinate, Y coordinate, Z coordinate, monster ID, number of monsters)

Code Dissected:

  • The first three lines is to declaring variables.
  • In addition, scripts can support the "+ - * /" calculation, and variables can also do the calculation
  • The API used to spawn monster is spawnCreature() fromWorldManagementAPI
  • Creaturedef

Example 4:

Spawn 1 to 10 Jockey randomly in the coordinates, output the number in chat box.

local actorId = 3102 --declare a variable, put in monster ID
local num = math.random(1,10)--declare a variable num, put in the range of the random number you want to obtain from
local x, y, z = 5, 7, 5 --declare a variable, put in coordinates
World:spawnCreature(x, y, z, actorId, num)
Chat:sendSystemMsg("randommobnum:"..num)

--Parameter definition:
--Chat:sendSystemMsg("TextsWriteHere"..the variable that storage random number num)

Code Dissected:

  • The implementation of random Numbers: math.random(1,10), means you will get a random number between 1 to 10
  • To output text to chat, need to use sendSystemMsg() fromchatmanagement
Last Update: 4/15/2020, 6:29:50 PM