How to embed python interpreter in UE4 via C++?

I am thinking of embedding a python interpreter in UE4 using C++. It seems like this would be very useful in writing game logic that is a bit too complicated for blueprint without having to go through the full C++ experience. Does anyone have experience or suggestions on this? Will it cause problems if/when I try to package my project for a smart phone or tablet? Will the python interpreter be too big? Will module loading fail due to lack of a true filesystem? The reason I ask is because using python with UE4 seems so obvious and useful that the fact that I can’t find any discussion of it makes me worry that there is such an obvious problem that I’m missing with doing that. Thanks.

1 Like

This is a very interesting question. Python could be the middle-ground from blueprint and c++. I’d like to see it on UE4 (as python programmer). But i think that this required a huge amount of coding and implementing and I think we need the support of the epic team.

It’s 100% possible, engine is writen in C++ and you modules are part of the engine, so you can implement anything you can implement using C++. So quastion how to implment Python scripting and other quastions you asked is same as asking how to implement Python scripting in any C++ application and if it good. Here you have instruction how to add external libraries in to UE4

I think best way to implement any scripting language in UE4, is by pluging it to reflection system so you don’t need to bind invidual functions manually and have everything (except K2Nodes) that blueprint.has and more actully as there reflected functions but they are not blueprint binded. You probably will need to do type conversions and such too from python string to FString and such. There is ScriptPlugin, i don’t know it current status but it’s implementing Lua also using reflection system, so that might be good starting point for you (ofcorse it copyrighted by Epic so don’t copy paste it too much):

https://github.com/EpicGames/UnrealEngine/tree/release/Engine/Plugins/ScriptPlugin

Then try to study first how to build python to iOS first and try to place python source in to ThirdParty same as other libraries are placed there (not sure how exacly that works), usally source of cross-platfrom software compiles to specific platfrom by there make scripts detecting for which platfrom they are being compiled to, so usally you don’t need to do anything.

Thanks for the response.

Actually, I was able to create a static library and link it to the engine so that I could run python code from UE4. Basically, I provided an “ExecInPython” command which would take in a string, run it in python, and return the result. That was helpful but didn’t achieve the holy grail of being able to easily call python from UE4 or have python code reference the UE4 API.

The reason I didn’t post more about this or go further is because I wasn’t confident about what would happen if/when I tried to package my project for iOS or other non-PC platforms. I’m reasonably confident that with static linking, the python integration would work on PCs. But since I don’t know how the cross-compilation works and since I suspect python relies fairly heavily on OS-specific things like the file-system I was concerned that it would be a mess.

Basically, it’s not just like implementing anything in C++ because I am developing on a PC but intend to run on iOS. So it’s more like just implementing anything in C++ with a 3rd party cross-compiler. Not a simple thing.

This thread (Scripting Language extensions via plugins - C++ - Unreal Engine Forums) has lots of discussions on trying to get Lua working with UE4. Lots of people are excited about it but also having trouble.

I know Epic is busy so I’m not complaining. It would be really awesome if Epic could provide a little support on getting python or lua or tcl or something working, able to be packaged on non-PC/Mac/Linux (i.e., using cross-compiler) and documented.

Thanks,
-X

Hi, we have just released a plugin for embedding python in Unreal Engine 4:

Wow, your plugin looks awesome! Thanks!

HOORAY! How does it compare feature wise to UnrealJS? Would much rather write Python than JS.

well unreal.js is out by months, i suppose they have a way more polished code :slight_smile: By the way the main difference is that we allow to script the editor too, and that we are exposing ad-hoc functions that are more ‘pythonic’ for a python developer.

Very good work!

A question: this is an unofficial release or what?

I see now that it support only python 3.5 and not 2.x

:frowning: