There was some attempts of combining Garry's Mod and Python previously, for example, gm_python. I thought "What if it was possible to code entire addons on Python instead of Lua?", and then I started developing this thing.
Theoretically, this should give the ability to use Python for addon development with or instead of Lua. Python may be used for something especially great which can't be done with Garry's Mod Lua.
The project is in the very early development stage, so there is not much to test.
I think that code with PyGmod will look like this:
-- Lua version
hook.Add('Initialize', 'init', function()
Msg('Hello world')
end)
hook.Add('PlayerSay', 'logChatToConsole', function(sender, text, teamChat)
Msg(sender:Nick() .. ' says: ' .. text)
end)
# Python version
from gmod import hooks
@hooks.hook('Initialized')
def init():
print('Hello world')
@hooks.hook('PlayerSay')
def log_chat_to_console(sender, text, team_chat):
print(sender.nick, 'says:', str(text))
If you are interested, take a look at the Project Repository and Documentation (not quite ready yet).
Here you can download it and test what is already done.
Examples of what has been done so far:
You can use eval() and exec() functions in gmod.lua module to run Lua code, and there is "py.Exec" function in Lua to run Python code.
Printing in Python will print to the game console.
gmod.net module contains prototypes of send() and receive() functions which are similar to correspoding net.Send(), net.SendToServer() (Python send() combines the functionality of both) and net.Receive().
See the documentation for more.
Feedback is welcome!
I created a Discord server for PyGmod discussion. Feel free to join!
Wow this is amazing, I'm excited to be able to use Python in Gmod.
I just realized that I don't have to make Python wrappers for every single GLua function, as I planned before.
I am going to make a fundamental design change that will make PyGmod code to look different.
# PyGmod 0.9-alpha code, before the redesign
from gmod import hooks
@hooks.hook('Initialized')
def init():
print('Hello world')
@hooks.hook('PlayerSay')
def log_chat_to_console(sender, text, team_chat):
print(sender.nick, 'says:', str(text))
# PyGmod 0.10-alpha code, after the redesign
from gmod.lua import *
def init():
print('Hello world')
hook.Add('Initialized', 'init', luafunction(init))
def log_chat_to_console(sender, text, team_chat):
print(sender._.Nick(), 'says:', text)
hook.Add('PlayerSay', 'logChatToConsole', luafunction(init))
As you can see, the code after redesign looks more Lua-ish. With this approach, I won't have to implement countless wrappers and developers won't have to learn yet another API.
Sorry, you need to Log In to post a reply to this thread.