Title: [ENH] Add LuaObject(UserData) · Issue #1 · GmodNET/Extensions · GitHub
Open Graph Title: [ENH] Add LuaObject(UserData) · Issue #1 · GmodNET/Extensions
X Title: [ENH] Add LuaObject(UserData) · Issue #1 · GmodNET/Extensions
Description: Description I want to add easy UserData support to GmodDotNet LuaObject is a inheritable class that allows to push and pop c#-style userdata. It already implements some basic metamethods such as __gc and __tostring, with virtual methods ...
Open Graph Description: Description I want to add easy UserData support to GmodDotNet LuaObject is a inheritable class that allows to push and pop c#-style userdata. It already implements some basic metamethods such as __...
X Description: Description I want to add easy UserData support to GmodDotNet LuaObject is a inheritable class that allows to push and pop c#-style userdata. It already implements some basic metamethods such as __...
Opengraph URL: https://github.com/GmodNET/Extensions/issues/1
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[ENH] Add LuaObject(UserData)","articleBody":"### Description\r\nI want to add easy UserData support to GmodDotNet\r\n\r\n`LuaObject` is a inheritable class that allows to push and pop c#-style userdata. It already implements some basic metamethods such as `__gc` and `__tostring`, with virtual methods `Destroy` and `Name` for child classes to override.\r\nAt the module startup, every class inherited from `LuaObject` and with `[UserData(string Name)]` attribute would be registered.\r\nRegistering luaobject consists of steps:\r\n\r\n1. Create metatable using `lua.CreateMetatable` using name from `[UserData]` attribute\r\n2. Write TypeName, TypeId from `CreateMetaTable` and Type to internal Dictionary\r\n3. Get all static methods with signature `int (ILua lua)` and `[MetaMethod]` attribute from the class and push them as metamethods\r\n4. Create table and push all static methods with the same signature but without `[MetaMethod]` attribute\r\n5. Make new table an `__index` for metatable\r\n\r\n### Usage\r\nCreating LuaObject\r\n```cs\r\n [UserData(Name = \"MyLuaObject\")]\r\n public class MyLuaObject : LuaObject\u003cMyLuaObject\u003e\r\n {\r\n public override string Name() =\u003e \"MyLuaObject\";\r\n public override void Destroy() =\u003e Console.WriteLine(\"Garbage collected!\");\r\n public int SomeInt = 5;\r\n private static int ReturnNumber(ILua lua)\r\n {\r\n var self = Pop(lua);\r\n lua.PushNumber(self.SomeInt);\r\n return 1;\r\n }\r\n }\r\n```\r\n\r\nPushing and poping\r\n```cs\r\n int SomePushLuaMethod(ILua lua)\r\n {\r\n MyLuaObject obj = new MyLuaObject();\r\n obj.Push(lua);\r\n return 1;\r\n }\r\n int SomePopLuaMethod(ILua lua)\r\n {\r\n MyLuaObject obj = MyLuaObject.Pop(1); // Pop first argument\r\n Console.WriteLine(obj.SomeInt);\r\n }\r\n```\r\nUsage in lua is as follows:\r\n```lua\r\n local myObj = SomePushLuaMethod()\r\n print(myObj:ReturnNumber())\r\n SomePopLuaMethod(myObj) -- 5 in console\r\n myObj = nil --__gc would be called, also calling Destroy virtual method\r\n```","author":{"url":"https://github.com/JenyaRostov","@type":"Person","name":"JenyaRostov"},"datePublished":"2021-06-04T19:37:04.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":8},"url":"https://github.com/1/Extensions/issues/1"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:e1c315fe-c9e1-1f81-b2ee-9b213f957958 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A0A6:1AC9BC:321BB1E:45D9DB6:6A63486D |
| html-safe-nonce | 260734f5931c918dab9e4cb86570176d407e17f144a66c7a820f99277156538b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMEE2OjFBQzlCQzozMjFCQjFFOjQ1RDlEQjY6NkE2MzQ4NkQiLCJ2aXNpdG9yX2lkIjoiNzQ3MjAwNzYwNjUzMjUyNDE0MSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 053fcf3e119d375c6895967fef205547aa9a74358cb06b12e83b3b90d449000f |
| hovercard-subject-tag | issue:957304263 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/GmodNET/Extensions/1/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c4433ff5bf2eb07ae6feed328e9e791103c869288cdea12f78dbb7112a869b6a/GmodNET/Extensions/issues/1 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c4433ff5bf2eb07ae6feed328e9e791103c869288cdea12f78dbb7112a869b6a/GmodNET/Extensions/issues/1 |
| og:image:alt | Description I want to add easy UserData support to GmodDotNet LuaObject is a inheritable class that allows to push and pop c#-style userdata. It already implements some basic metamethods such as __... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | JenyaRostov |
| hostname | github.com |
| expected-hostname | github.com |
| None | 59e55daad7174ca59d63c6974d58276ccb5477442e550bebb3c035e1bef11c94 |
| turbo-cache-control | no-preview |
| go-import | github.com/GmodNET/Extensions git https://github.com/GmodNET/Extensions.git |
| octolytics-dimension-user_id | 69189476 |
| octolytics-dimension-user_login | GmodNET |
| octolytics-dimension-repository_id | 391443093 |
| octolytics-dimension-repository_nwo | GmodNET/Extensions |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 391443093 |
| octolytics-dimension-repository_network_root_nwo | GmodNET/Extensions |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 990295d92a4cc7b63fbbd83a046217cd7d77d49c |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width