Skip to content

Commit

Permalink
Reworked some lua tables to oop pattern with constructor dependency i…
Browse files Browse the repository at this point in the history
…njection
  • Loading branch information
Ismoh committed Sep 17, 2023
1 parent 8c736f1 commit 38ac443
Show file tree
Hide file tree
Showing 20 changed files with 772 additions and 1,591 deletions.
3 changes: 1 addition & 2 deletions .debug/lua-definitions/noitapatcher.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---@meta 'noitapatcher'
---@module noitapatcher

---@class noitapatcher
local noitapatcher = {}

---Enable OnProjectileFired and OnProjectileFiredPost callbacks.
Expand Down
39 changes: 27 additions & 12 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,15 @@ Make sure to **use the following naming convention** for classes and **also add*
---@class ExampleClass
---Short description of the class.
local ExampleClass = {
-- Imports
customProfiler = require("customProfiler"), -- mandatory
foo = require("foo"),
bar = require("bar"),
-- Attributes
--[[ Imports ]]

---@type CustomProfiler
customProfiler = nil, -- mandatory
foo = nil,
bar = nil,

--[[ Attributes ]]

exampleAttribute = "exampleValue",
nestedTable = {
nestedAttribute = "nestedValue",
Expand All @@ -65,18 +69,20 @@ local ExampleClass = {
-- private functions

---Short description of the function.
---@private
---@param classObjectOrSelf ExampleClass This parameter is used for self reference in private functions. Used `self` in public functions.
---@param param string Description of the parameter.
---@return string Description of the return value.
local examplePrivateFunction = function(classObjectOrSelf, param) --[[ private ]]
local examplePrivateFunction = function(classObjectOrSelf, param)
local cpc = classObjectOrSelf.customProfiler:start("ExampleClass:examplePrivateFunction")
-- code
classObjectOrSelf.customProfiler:stop("ExampleClass:examplePrivateFunction", cpc)
return value
end

---Short description of the function.
local anotherPrivateFunction = function() --[[ private ]]
---@private
local anotherPrivateFunction = function()
-- code
end

Expand Down Expand Up @@ -110,13 +116,20 @@ end
---Constructor of the class. This is mandatory!
---@param objectToInheritFrom ExampleClass This can be any object that you want to inherit from.
---@return ExampleClass
function ExampleClass:new(objectToInheritFrom)
local exampleObject = objectToInheritFrom or {}
function ExampleClass:new(objectToInheritFromOrObjectItself, customProfiler, importClass, foo, bar)
local exampleObject = objectToInheritFromOrObjectItself or self or {} -- Use self if this is called as a class constructor
setmetatable(exampleObject, self)
self.__index = self
local cpc = self.customProfiler:start("ExampleClass:new")
-- more init code
self.customProfiler:stop("ExampleClass:new", cpc)

local cpc = customProfiler:start("ExampleClass:new")

-- Initialize all imports to avoid recursive imports
self.customProfiler = customProfiler or require("CustomProfiler"):new()
self.importClass = importClass or require("importClass"):new()
self.foo = foo or require("foo"):new()
self.bar = bar or require("bar"):new()

customProfiler:stop("ExampleClass:new", cpc)
return exampleObject
end

Expand All @@ -128,6 +141,8 @@ end
return ExampleClass
```

### How to require classes?

## How does NoitaMP work?

NoitaMP is a mod that uses the [Noita API](https://noita.wiki.gg/wiki/Modding), but there is something that you need to know about the API.\
Expand Down
Loading

0 comments on commit 38ac443

Please sign in to comment.