{"id":13409749,"url":"https://github.com/Sleitnick/rbxts-proton","last_synced_at":"2025-03-14T15:30:37.387Z","repository":{"id":53876272,"uuid":"521722841","full_name":"Sleitnick/rbxts-proton","owner":"Sleitnick","description":"Experimental framework for Roblox game development","archived":false,"fork":false,"pushed_at":"2023-02-15T04:29:35.000Z","size":174,"stargazers_count":25,"open_issues_count":1,"forks_count":4,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-05-20T03:17:43.207Z","etag":null,"topics":["framework","roblox","roblox-ts","typescript"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/@rbxts/proton","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Sleitnick.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-08-05T17:25:49.000Z","updated_at":"2024-07-30T22:51:29.143Z","dependencies_parsed_at":"2024-07-30T23:09:01.276Z","dependency_job_id":null,"html_url":"https://github.com/Sleitnick/rbxts-proton","commit_stats":{"total_commits":58,"total_committers":4,"mean_commits":14.5,"dds":"0.051724137931034475","last_synced_commit":"5adb1582a53465254e2da73ad17badebad71a191"},"previous_names":[],"tags_count":25,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sleitnick%2Frbxts-proton","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sleitnick%2Frbxts-proton/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sleitnick%2Frbxts-proton/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sleitnick%2Frbxts-proton/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Sleitnick","download_url":"https://codeload.github.com/Sleitnick/rbxts-proton/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221477209,"owners_count":16829000,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["framework","roblox","roblox-ts","typescript"],"created_at":"2024-07-30T20:01:03.158Z","updated_at":"2024-10-26T00:31:02.214Z","avatar_url":"https://github.com/Sleitnick.png","language":"TypeScript","readme":"# Proton\n\n[![Lint](https://github.com/Sleitnick/rbxts-proton/actions/workflows/lint.yaml/badge.svg)](https://github.com/Sleitnick/rbxts-proton/actions/workflows/lint.yaml)\n[![Release](https://github.com/Sleitnick/rbxts-proton/actions/workflows/release.yaml/badge.svg)](https://github.com/Sleitnick/rbxts-proton/actions/workflows/release.yaml)\n\nProton is an easy-to-use framework for Roblox game development.\n\nLike the proton of an atom, Proton aims at adding stability to your game development, all while only remaining a portion of the whole. Use Proton to structure and connect the top-level design of your game.\n\n\u003e **Warning**\n\u003e Proton is early in development. Due to currently limitations with roblox-ts, Proton does not yet support dependency injection, which is a necessary feature to make Proton production-ready. Other portions of Proton may also change, as the overall API is not solidified.\n\n## Providers\n\nProviders are the core of Proton. A provider _provides_ a specific service or utility to your game. For example, a game might have a `DataProvider` (or `DataService`/`DataManager`/etc.) that provides the logic for handling data for players in the game.\n\n### Structure\nThe minimum structure of a provider looks like the following:\n```ts\nimport { Provider } from \"@rbxts/proton\";\n\n@Provider()\nexport class MyProvider {}\n```\n\nThat's it. The `@Provider()` decorator communicates data about the class to Proton, but does _not_ mutate the given provider at all. Proton will create a singleton of the provider once Proton is started.\n\n### Extensible\nProviders can have any number of methods or fields added to them. They're just plain classes. Add anything to them.\n\n```ts\n@Provider()\nexport class MyProvider {\n\tprivate message = \"hello!\";\n\n\t// Optional constructor method\n\tconstructor() {\n\t\t// Use the constructor to set up any necessary functionality\n\t\t// for the provider (e.g. hooking up network connections).\n\t}\n\n\t// Custom method\n\thelloWorld() {\n\t\tprint(this.message);\n\t}\n}\n```\n\n### Built-in Start Lifecycle\nProton includes an optional built-in lifecycle method `ProtonStart`, which is fired when Proton is started. All `ProtonStart` lifecycle methods are called at the same time using `task.spawn`, which means that these methods can yield without blocking other providers from starting. It is common practice to use `ProtonStart` as a place to have long-running loops (e.g. a system that drives map loading and rotation every round).\n\n```ts\nimport { Lifecycle, ProtonStart, Provider } from \"@rbxts/proton\";\n\n@Provider()\nexport class MyProvider {\n\tconstructor() {\n\t\tprint(\"MyProvider initialized\");\n\t}\n\n\t@Lifecycle(ProtonStart)\n\tonStart() {\n\t\tprint(\"MyProvider started\");\n\t}\n}\n```\n\n## Starting Proton\n\nFrom both a server and client script, call the `Proton.start()` method.\n\n```ts\nimport { Proton } from \"@rbxts/proton\";\n\nProton.start();\n```\n\nIf another script requires Proton to be started, `Proton.awaitStart()` can be used, which will yield until Proton is fully started.\n\n```ts\nimport { Proton } from \"@rbxts/proton\";\n\nProton.awaitStart();\n```\n\n### Loading Providers\nModules are not magically loaded. Thus, if your providers exist in their own modules but are never imported by any running code, then Proton will never see them and they will not start. This is common for top-level providers that no other code relies on. In such cases, they must be explicitly imported:\n\n```ts\nimport { Proton } from \"@rbxts/proton\";\n\n// e.g.\nimport \"./providers/my-provider.ts\"\n\nProton.start();\n```\n\n## Getting a Provider\n\nOnce Proton is started, use `Proton.get()` to get a provider:\n\n```ts\nconst myProvider = Proton.get(MyProvider);\nmyProvider.helloWorld();\n```\n\nProviders can also access other providers:\n\n```ts\n@Provider()\nexport class AnotherProvider {\n\tprivate readonly myProvider = Proton.get(MyProvider);\n\n\tconstructor() {\n\t\tmyProvider.helloWorld();\n\t}\n}\n```\n\n## Network\n\nThe recommended way to do networking in Proton is to create a `network.ts` file in a shared directory (e.g. accessible from both the server and the client), and then create a `Network` namespace with the desired `NetEvent` and `NetFunction` objects. Optionally, multiple different namespaces can be created to separate between network functionality.\n\n```ts\n// shared/network.ts\nimport { NetEvent, NetEventType, NetFunction } from \"@rbxts/proton\";\n\nexport namespace Network {\n\t// Send a message to a player\n\texport const sendMessageToPlayer = new NetEvent\u003c[message: string], NetEventType.ServerToClient\u003e();\n\n\t// Get fireBullet from player\n\texport const fireBullet = new NetEvent\u003c[pos: Vector3, dir: Vector3], NetEventType.ClientToServer\u003e();\n\n\t// Allow client to fetch some data\n\texport const getData = new NetFunction\u003cvoid, [data: string]\u003e();\n\n\t// Client sends request to buy something\n\texport const buy = new NetFunction\u003c[item: string, category: string], [bought: boolean]\u003e();\n\n\t// Client gets sent multiple variables\n\texport const getMultiple = new NetFunction\u003cvoid, {msg1: string, msg2: string, msg3: string}\u003e();\n}\n```\n\nExample of the above Network setup being consumed:\n\n```ts\n// server\n\nNetwork.sendMessageToPlayer.server.fire(somePlayer, \"hello world!\");\nNetwork.fireBullet.server.connect((pos, dir) =\u003e {\n\t// Handle bullet being fired\n});\nNetwork.getData.server.handle((player) =\u003e {\n\treturn \"Some data\";\n});\nNetwork.buy.server.handle((player, item, category) =\u003e {\n\t// Buy item\n\treturn false;\n});\nNetwork.getMultiple.handle((player) =\u003e {\n\treturn { msg1: \"hello\", msg2: \"world\", msg3: \"how are you\" };\n});\n```\n\n```ts\n// client\n\nNetwork.sendMessageToPlayer.client.connect((message) =\u003e {\n\tprint(message);\n});\nNetwork.fireBullet.client.fire(new Vector3(), Vector3.zAxis);\nconst data = Network.getData.client.fire();\nconst { msg1, msg2, msg3 } = Network.getMultiple.client.fire();\n```\n\n## Lifecycles\n\nCustom lifecycles can be added. At their core, lifecycles are just special event dispatchers that can hook onto a class method. For example, here is a lifecycle that is fired every Heartbeat.\n\n```ts\n// shared/lifecycles.ts\nimport { ProtonLifecycle } from \"@rbxts/proton\";\n\nexport interface OnHeartbeat {\n\tonHeartbeat(dt: number): void;\n}\n\nexport const HeartbeatLifecycle = new ProtonLifecycle\u003cOnHeartbeat[\"onHeartbeat\"]\u003e();\n\nRunService.Heartbeat.Connect((dt) =\u003e HeartbeatLifecycle.fire(dt));\n```\n\nA provider can then hook into the lifecycle:\n\n```ts\nimport { Provider, Lifecycle } from \"@rbxts/proton\";\n\n@Provider()\nexport class MyProvider implements OnHeartbeat {\n\t@Lifecycle(HeartbeatLifecycle)\n\tonHeartbeat(dt: number) {\n\t\tprint(\"Update\", dt);\n\t}\n}\n```\n\nHere is a more complex lifecycle that is fired when a player enters the game.\n\n```ts\n// shared/lifecycles.ts\nexport interface OnPlayerAdded {\n\tonPlayerAdded(player: Player): void;\n}\n\nexport const PlayerAddedLifecycle = new ProtonLifecycle\u003cOnPlayerAdded[\"onPlayerAdded\"]\u003e();\n\n// Trigger lifecycle for all current players and all future players:\nPlayers.PlayerAdded.Connect((player) =\u003e PlayerAddedLifecycle.fire(player));\nfor (const player of Players.GetPlayers()) {\n\tPlayerAddedLifecycle.fire(player);\n}\n\n// Trigger lifecycle for all players for any new callbacks that get registered later on during runtime:\nPlayerAddedLifecycle.onRegistered((callback) =\u003e {\n\tfor (const player of Players.GetPlayers()) {\n\t\ttask.spawn(callback, player);\n\t}\n});\n```\n\n```ts\n@Provider()\nexport class MyProvider implements OnPlayerAdded {\n\t@Lifecycle(PlayerAddedLifecycle)\n\tonPlayerAdded(player: Player) {\n\t\tprint(`Player entered the game: ${player}`);\n\t}\n}\n```\n\nHaving the `OnPlayerAdded` interface just helps to keep explicit typings across consumers of the lifecycle. However, it is not entirely necessary. The above example could also have a lifecycle definition and consumer look like such:\n\n```ts\nexport const PlayerAddedLifecycle = new ProtonLifecycle\u003c(player: Player) =\u003e void\u003e();\n\n@Provider()\nexport class MyProvider {\n\t@Lifecycle(PlayerAddedLifecycle)\n\tonPlayerAdded(player: Player) {}\n}\n```\n\n\n## Components\n\nBind components to Roblox instances using the Component class and CollectionService tags.\n\n```ts\nimport { BaseComponent, Component } from \"@rbxts/proton\";\n\n@Component({ tag: \"MyComponent\" })\nclass MyComponent extends BaseComponent\u003cBasePart\u003e {\n\tonStart() {}\n\tonStop() {}\n}\n```\n\nIn initialization file:\n\n```ts\nimport { Proton } from \"@rbxts/proton\";\n\nimport \"./wherever/my-component\";\n\nProton.start();\n```\n","funding_links":[],"categories":["Frameworks"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSleitnick%2Frbxts-proton","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FSleitnick%2Frbxts-proton","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSleitnick%2Frbxts-proton/lists"}