Skip to main content

Package

A Package is a normal Roblox ModuleScript that can return any datatype. SILK conveniently provides a number of pre-written packages known as essentials. Navigate to "Included Packages" to view a complete list.

External Dependencies

Including external dependencies inside the framework is as easy as just dropping it in. Simply drag and drop any ModuleScript dependencies inside a folder containing the rest of your packages and access it like you would for a regular package.


Implementation

Since a Package is just a ModuleScript, the implementation of one allows for flexibility for developers to adapt the package to their needs. However, the most common implementation of a Package is shown below.

Typical implementation of a Package:
--|| Package.lua ||

local package = {}
package.__index = package

-- Optionally indicate that the package is a singleton
package.__singleton = true

-- This is called whenever this package is referenced or once if the package is a singleton
-- Conveniently access the Silk object
package.__initialize = function(silk)

	-- Store silk within the package for future use
	package.silk = silk
	
	-- This is the value that is returned during runtime
	-- If the package is a singleton, this return value is cached internally
	return package
end

function package.new()
	return setmetatable({}, package)
end

-- If a package contains package.__initialize, the method is called and the value that it returns is cached instead during runtime
return package

Management

All packages should be added in using the Silk.AppendPackages method during the initializer phase.

Storing Packages

When storing package, place them in a secure location alongside all your other packages. For example, a folder containing all your shared packages.

Adding in packages through the initializer script:
--|| initializer.server.lua ||

silk:AppendPackages{

	-- Directory that contains all the packages
	silk.ReplicatedStorage:WaitForChild('Packages'),
}

Usage

Initialize and return contents of package:
-- Access packages immediately after they're added
local package = silk.Packages.Package

Alternatively, when intialization for singleton packages is required during the initializer phase, instead of initializing the package directly using silk.Packages.<Package>, use Silk.InitPackage.

Intializing a package directly:
-- Initialize the package directly
silk:InitPackage('Package')

Properties

__domain

Package.__domain: string

An optional meta attribute used to place domain restriction on a package. The default behaviour is shared. Change the attribute to server or local to restrict access.

__singleton

Package.__singleton: boolean

An optional meta attribute that can be included in any package. If set to true, a cached reference to the package is returned whenever the package is referenced.

info

If Package.__initialize is also provided, the return value recieved after calling this method is cached instead.

Functions

__initialize

Package.__initialize(silkSilk) → any

An optional meta function that can be included in any package. This method should typically be used when further initialization (using Silk) is required before returning the package.

Show raw api
{
    "functions": [
        {
            "name": "__initialize",
            "desc": "An optional meta function that can be included in any package. This method should typically be used when further initialization (using [Silk]) is required before returning the package.",
            "params": [
                {
                    "name": "silk",
                    "desc": "",
                    "lua_type": "Silk"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "any"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 657,
                "path": "src/init.lua"
            }
        }
    ],
    "properties": [
        {
            "name": "__domain",
            "desc": "An optional meta attribute used to place domain restriction on a package. The default behaviour is `shared`. Change the attribute to `server` or `local` to restrict access.",
            "lua_type": "string",
            "source": {
                "line": 637,
                "path": "src/init.lua"
            }
        },
        {
            "name": "__singleton",
            "desc": "An optional meta attribute that can be included in any package. If set to true, a cached reference to the package is returned whenever the package is referenced.\n\n:::info\nIf [Package.__initialize] is also provided, the return value recieved after calling this method is cached instead.\n:::",
            "lua_type": "boolean",
            "source": {
                "line": 648,
                "path": "src/init.lua"
            }
        }
    ],
    "types": [],
    "name": "Package",
    "desc": "A Package is a normal Roblox [ModuleScript] that can return any datatype. SILK conveniently provides a number of pre-written packages known as *essentials*. Navigate to \"Included Packages\" to view a complete list.\n\n:::tip External Dependencies\nIncluding external dependencies inside the framework is as easy as just dropping it in. Simply drag and drop any [ModuleScript] dependencies inside a folder containing the rest of your packages and access it like you would for a regular package.\n:::\n\n---\n\n### Implementation\n\nSince a Package is just a [ModuleScript], the implementation of one allows for flexibility for developers to adapt the package to their needs. However, the most common implementation of a Package is shown below.\n\n##### Typical implementation of a Package:\n```lua\n--|| Package.lua ||\n\nlocal package = {}\npackage.__index = package\n\n-- Optionally indicate that the package is a singleton\npackage.__singleton = true\n\n-- This is called whenever this package is referenced or once if the package is a singleton\n-- Conveniently access the Silk object\npackage.__initialize = function(silk)\n\n\t-- Store silk within the package for future use\n\tpackage.silk = silk\n\t\n\t-- This is the value that is returned during runtime\n\t-- If the package is a singleton, this return value is cached internally\n\treturn package\nend\n\nfunction package.new()\n\treturn setmetatable({}, package)\nend\n\n-- If a package contains package.__initialize, the method is called and the value that it returns is cached instead during runtime\nreturn package\n```\n\n---\n\n### Management\n\nAll packages should be added in using the [Silk.AppendPackages] method during the *initializer phase*.\n\n:::tip Storing Packages\nWhen storing package, place them in a secure location alongside all your other packages. For example, a folder containing all your *shared* packages.\n:::\n\n##### Adding in packages through the initializer script:\n```lua\n--|| initializer.server.lua ||\n\nsilk:AppendPackages{\n\n\t-- Directory that contains all the packages\n\tsilk.ReplicatedStorage:WaitForChild('Packages'),\n}\n```\n\n---\n\n### Usage\n\n##### Initialize and return contents of package:\n```lua\n-- Access packages immediately after they're added\nlocal package = silk.Packages.Package\n```\n\nAlternatively, when intialization for singleton packages is required during the initializer phase, instead of initializing the package directly using `silk.Packages.<Package>`, use [Silk.InitPackage].\n\n##### Intializing a package directly:\n```lua\n-- Initialize the package directly\nsilk:InitPackage('Package')\n```",
    "source": {
        "line": 630,
        "path": "src/init.lua"
    }
}