Passer au contenu principal
Version: bleeding-edge 🩸

Guide des classes

Tout ce que vous devez savoir sur les Classes

Toutes les entités du monde nanos sont représentées par une Classe. 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). Dans cette page, nous allons en expliquer plus à ce sujet.

tip

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

Classes​

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! Vous créez des objets et appelez des fonctions/événements de capture sur cet objet. 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.

Classes de base​

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.

Apparition d'Entités​

Faire apparaître des entités dans Nanos World est extrêmement facile et simple, disons que nous voulons faire apparaître un personnage:

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.

Destruction d'Entités​

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

Server/Index.lua
mon_personnage: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 😉.

Classes statiques​

Les classes statiques dans le monde nanos sont des classes que vous ne pouvez pas faire apparaître. Au lieu de cela, vous pouvez utiliser leurs méthodes directement avec ..

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. N'hésitez pas à proposer des demandes de fusion et à suggérer des modifications !