From a5afe1b71d54be9f16f4e3edf75f4de194e05250 Mon Sep 17 00:00:00 2001 From: Linden <65407488+thelindat@users.noreply.github.com> Date: Sun, 27 Nov 2022 12:03:01 +1100 Subject: [PATCH] ox_core concepts Try to explain core concepts like classes in a simple-ish way. --- docs/ox_core/Getting Started/concepts.md | 46 ++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 docs/ox_core/Getting Started/concepts.md diff --git a/docs/ox_core/Getting Started/concepts.md b/docs/ox_core/Getting Started/concepts.md new file mode 100644 index 00000000..0350aa3e --- /dev/null +++ b/docs/ox_core/Getting Started/concepts.md @@ -0,0 +1,46 @@ +# Concepts + +We'll try to explain some technical but core concepts used in ox_core here. + +## Classes + +Lua doesn't have support for true clases, but rather prototype-based inheritance. +A prototype can hold its own variables and methods, which another object (an instance) can reference or call. + +```lua +-- CPlayer will hold all inherited variables and methods for instances of it. +local CPlayer = {} + +-- Sets the "index metamethod" of CPlayer to reference itself. +-- This is how instances will know to look up the table. +CPlayer.__index = CPlayer + +-- Set a new metatable on the table passed to the function. +-- The important code here is { __index = CPlayer }. +function CPlayer.new(player) + return setmetatable(player, CPlayer) +end + +-- Using the `:` syntax will pass the object as the first parameter. +function CPlayer:getUsername() + return self.username +end + +-- Create our instance of CPlayer. +local player = CPlayer.new({ + username = 'Bob the Builder' +}) + +-- This is the same as CPlayer.getUsername(player). +local username = player:getUsername() + +-- 'Bob the Builder' +print(username) +``` + +When using the imported classes in your resources you can treat the methods as if you are calling a regular function. + +```lua +local player = Ox.GetPlayer(playerId) +print(player.getUsername()) +```