Wizards of Lua

The Art of Spell Crafting


Entities - Finding Entities

The Entities module provides access to all Entity objects currently loaded.

Here is an overview of the Entities functions:

Function Parameters Results
 find  selector table
 summon  nbt Entity

Functions

Below you find short descriptions about each of the functions and some examples about how to use them in your spells.


find (selector) -> table

The ‘find’ function returns a table of Entity objects that match the given selector.

Example

Printing the number of all players currently logged in.

found = Entities.find("@a")
print(#found)

Example

Printing the position of player mickkay.

found = Entities.find("mickkay")[1]
print(found.pos)

Example

Printing the positions of all cows in the (loaded part of the) world.

found = Entities.find("@e[type=cow]")
for _,cow in pairs(found) do
  print(cow.pos)
end

Example

Printing the names of all dropped items in the (loaded part of the) world.

found = Entities.find("@e[type=item]")
for _,e in pairs(found) do
  print(e.name)
end

summon (nbt) -> Entity

The ‘summon’ function returns a freshly created entity of the given type, having the optionally given Nbt values.

Example

Creating a pig and moving it half a meter upwards.

pig = Entities.summon("pig")
pig:move("up",0.5)

Example

Creating a creeper with no AI.

Entities.summon("creeper", {NoAI=1})

Example

Creating a zombie with no AI that is spinning around.

z = Entities.summon("zombie", {NoAI=1})
while true do
  z.rotationYaw = z.rotationYaw + 10
  sleep(1)
end