Zum Hauptinhalt springen
Version: bleeding-edge 🩸

Classes Guide

All you need to know about Classes

All entities in nanos world are represented by a Class. In Lua, classes are represented by tables. That means that every interaction with entities (Players, Characters, Props, etc) are made with Classes/tables methods following an OOP (Object-oriented programming pattern). In this page we will explain more about that.

tip

In nanos world we have 4 types of Classes (or structures): Classes, Static Classes, Structs and Utility Classes.

Klassen​

If you read our Quick Start guide, you noticed we were spawning entities in the following format:

Server/Index.lua
-- Spawning an entity with its Constructor (location, rotation and mesh)
local my_prop = Prop(Vector(0, 0, 100), Rotator(), "nanos-world::SM_Cube")

-- Interacting with the entity with its methods
my_prop:SetLocation(Vector(100, 0, 100))
tip

Access to Entities / Spawned Objects Methods are made with :.

This is how OOP works! You create objects and call functions/catch events on that object. Unlike other scripting games which have tons of global functions and events to interact with entities, nanos world scripting is a modern approach to modern programming.

Base Classes​

Under the hood, nanos world Classes follow an Inheritance Pattern, which means we have base parent classes, and their children which "inherit" all functions, events and properties from them. For example, all Classes which exist physically in the world (such as Character and Prop) are Base Actor, so it is possible to call the function :SetLocation() on them.

info

The Base Entity is the base of all Classes, including the ones which are not in the world (such as Player, WebUI or Database). It contains the methods shared by every Class (like Subscribe, SetValue and Destroy) and the Static Methods to retrieve spawned entities (like GetAll, GetPairs and GetByIndex).

The Base Actor is the base of all Classes which exist in the world, and contains the methods to move, rotate, attach and change the visuals of them.

Spawn-Entitäten​

Spawning entities in nanos world is extremely easy and straightforward, let's say we want to spawn a Character:

Server/Index.lua
local my_character = Character(Vector(0, 0, 100), Rotator(), "nanos-world::SK_Male")
tip

Each Class will contain it's own Constructor, Properties, Methods and Events.

Check all Classes that nanos world provides in the sidebar SCRIPTING REFERENCE -> Classes item.

info

Keep in mind that some Classes can only be spawned in the Server, others can only be spawned in the Client.

Zerstöre Entitäten​

All classes (but Player) allow you to destroy them with the method :Destroy():

Server/Index.lua
my_character:Destroy()
info

Destroying an Entity will trigger the event Destroy and also will automatically detach all attached entities it had attached with :AttachTo()¹.

¹If you attached entities with lifespan_when_detached parameter other than -1, all attached entities will be destroyed as well 😉.

Statische Klassen​

Static Classes in nanos world are Classes which you cannot Spawn. Instead you can use it's methods directly with ..

For example, if you want to interact with Sun/Sky/Weather, you will need the Sky Static Class:

Client/Index.lua
-- Makes it 9:25 AM
Sky.SetTimeOfDay(9, 25)
tip

Access to Static Classes / Static Methods are made with ..

Structs & Utility Classes​

Besides Classes and Static Classes, we have the Structs and the Utility Classes! Structs are the data types used everywhere in the API, such as Vector, Rotator and Color. Utility Classes are libraries with helper functions, such as JSON and NanosMath.

tip

All Utility Classes are Open Sourced at https://github.com/nanos-world/nanos-world-lua-lib. Feel free to push merge requests and suggest changes!