lolleko / gmod-typescript Goto Github PK
View Code? Open in Web Editor NEWTool to generate typescript definitions for garrysmod.
License: MIT License
Tool to generate typescript definitions for garrysmod.
License: MIT License
Currently, only numeric or bool defaults are supported for parameters.
Would it be possible to support optional arguments? An example of this would be https://wiki.facepunch.com/gmod/Global.EmitSound
declare function EmitSound(soundName: string, position: Vector, entity: number, channel: CHAN, volume: number, soundLevel: SNDLVL, soundFlags: SND, pitch: number, dsp: number): void;
When it should be:
declare function EmitSound(soundName: string, position: Vector, entity: number, channel?: CHAN, volume?: number, soundLevel?: SNDLVL, soundFlags?: SND, pitch?: number, dsp?: number): void;
Hi, I found this project and decided to try it out. You've done a great job! The GLua API works very well. And it's really cool that anyone can try out a working toolkit, not a theoretical one (I've tried CSharp.lua, but there's no complete GLua API for it anywhere).
I am faced with the problem that Vector, Angle and VMatrix have metamethods for operator overloading, but they cannot work in TypeScript. This is by design of the TypeScript, because the JavaScript does not have an operator overloading :<
However, I found that the developers of the TypeScriptToLua provided special mapping functions:
https://typescripttolua.github.io/docs/advanced/language-extensions#operator-map-types
Also Vector and Angle have index variables (x, y, z, p, y, r, pitch, yaw, roll) that can be declared in the interface, instead of doing them through mapping functions.
For example operators as methods:
interface Vector {
x: number, 1: number;
y: number, 2: number;
z: number, 3: number;
add: LuaAdditionMethod<Vector, Vector>;
sub: LuaSubtractionMethod<Vector, Vector>;
// mul: LuaMultiplicationMethod <Vector, Vector> & LuaMultiplicationMethod <number, Vector>; // two methods (+1 overload)
mul: LuaMultiplicationMethod <Vector | number, Vector>; // its also correct but one method
div: LuaDivisionMethod <number, Vector>;
neg: LuaNegationMethod <Vector>; // or 'negate' or 'unm'?
...
how it looks
let offset = Vector(0, 0, 10)
let basePoint = Vector(0, 0, 1000)
instance.pos = Vector()
instance.pos[3] = 30
instance.pos.z = 30
instance.pos.y = instance.pos.y + 20
instance.pos = instance.pos.add(basePoint)
instance.pos = instance.pos.mul(Vector(0, 0, 1000))
instance.pos = instance.pos.mul(2)
instance.pos = instance.pos.div(2)
instance.pos = instance.pos.neg().mul(2).add(basePoint).sub(offset)
and gets this lua code
local offset = Vector(0, 0, 10)
local basePoint = Vector(0, 0, 1000)
instance.pos = Vector()
instance.pos[3] = 30
instance.pos.z = 30
instance.pos.y = instance.pos.y + 20
instance.pos = instance.pos + basePoint
instance.pos = instance.pos * Vector(0, 0, 1000)
instance.pos = instance.pos * 2
instance.pos = instance.pos / 2
instance.pos = ((-instance.pos * 2) + basePoint) - offset
Could you add operators and index variables to your declarations?
If you try and access an Entity function on a Player, you'll get an error. Same with trying to compare a Player with an Entity. This is since these classes don't derive / extend the Entity
.
In my bindings for Haxe to starfall I ended up hardcoding these since the Gmod Wiki doesn't tell you anything about which classes inherit from what
https://github.com/Vurv78/SFHaxe/blob/528b30a2a78ac5f044aac5d8583c67da84f05955/src/generator/Hardwired.hx#L39
Will probably need to do the same here unfortunately, there's some basic/the most common ones in the link I provided
Functions like print
are generated as so right now:
declare function print(args: any[]): void;
They should be like this instead (as far as I know)
Works for me when I manually changed it.
declare function print(...args: any[]): void;
Nice project dude, I was thinking of making this but then was pointed to this after they found it when I somehow couldn't..
Another Note: I think you're supposed to make all of these functions @noSelf
because they're all passing nil as the first argument. https://typescripttolua.github.io/docs/the-self-parameter/
The hook.Run()
function is currently typed as Run(): void
when it should be Run(eventName: string, ...args: any[]): any
Information about realm of methods is lost in the declarations comments. Can you generate it automatically, right?
One plugin make a feature for drawing "realm" squares :)
https://marketplace.visualstudio.com/items?itemName=venner.vscode-glua-enhanced
Of course, it is enough just to write realm at the beginning of the method comment.
https://wiki.facepunch.com/gmod/States
Server
Client
Shared
Menu
Server and Menu
Client and Menu
Shared and Menu
Gmod comes with a number of builtin libraries such as the string library. For example:
// JavaScript version
print("haystack".endsWith("needle"))
// Gmod version
print(string.EndsWith("haystack","needle"))
Both of these functions will accomplish the same goal, however if using the JS version, the transpiler will need to add
local function __TS__StringEndsWith(self, searchString, endPosition)
if endPosition == nil or endPosition > #self then
endPosition = #self
end
return string.sub(self, endPosition - #searchString + 1, endPosition) == searchString
end
to the transpiled output. This will result in a lot of redundant code.
Is there a way we can make TypeScriptToLua use the built-in gmod libraries?
I'd be happy to write the logic to do so, just not sure if it is possible or where to start.
There is no way to control script workflow with a SERVER/CLIENT realms. Its not exists in TypeScript.
https://wiki.facepunch.com/gmod/States
There are also some global variables, such as ENT, SWEP, GM. However, I have no idea how to implement them correctly, because they will be visible in the entire TypeScript code. TypeScriptToLua uses globalThis
for _G alias. globalThis
implemented by TypeScript itself.
https://wiki.facepunch.com/gmod/Global_Variables
Minor. Garry's mod also have some constants (vector_origin, vector_up) at the bottom of the page.
I don't like their names, I'd rather make my own like ZeroVector, ForwardVector and other. But perhaps it can be useful to someone. I think you will decide for yourself whether you need to spend time on this.
Read the TypeScriptToLua docs wrong, never mind.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.