Page 1 of 1

Is C compatible with Love2D?

Posted: Thu Sep 27, 2018 12:55 pm
by noahfeazell
I'm curious if we can use C in Love2D. Maybe it has said it somewhere but I missed it.

Re: Is C compatible with Love2D?

Posted: Fri Sep 28, 2018 12:21 am
by pgimeno
Not sure if this covers what you mean, but you can use many languages, including C, to write a dynamic library, and make bindings to it via LuaJIT's FFI library. Then you can use LÖVE's Lua to call any functions the library exports.

However, that means the library will only work for the processor it's compiled for. If you manage to include binary dynamic libraries for all systems you intend to support, you will need special code to make your LÖVE program load the right library for the running system.

Re: Is C compatible with Love2D?

Posted: Fri Sep 28, 2018 12:48 pm
by Kindermaus
Yes.

I wrote a behavior tree library and a "game database" library in C++ with C wrappers and I use them from Love on both Android and Windows (Linux, etc would be just as easy). You build a normal Lua-compatible DLL (either using FFI/LuaJit or traditional luaopen_xxx shared symbol where xxx is require 'xxx'). I use an ext/ directory off the main Love directory on desktop platforms and modify LUA_CPATH to search there first:

Code: Select all

do
	if love.system.getOS() == "Android" then
		local sourceDirectory = love.filesystem.getSourceBaseDirectory()

		local cpath = package.cpath
		package.cpath = string.format(
			"%s/lib/lib?.so;%s/lib?.so;%s",
			sourceDirectory,
			sourceDirectory,
			cpath)
	else
		local sourceDirectory = love.filesystem.getSourceBaseDirectory()

		local cpath = package.cpath
		package.cpath = string.format(
			"%s/ext/?.dll;%s/ext/?.so;%s",
			sourceDirectory,
			sourceDirectory,
			cpath)

		local path = package.path
		package.path = string.format(
			"%s/ext/?.lua;%s/ext/?/init.lua;%s",
			sourceDirectory,
			sourceDirectory,
			cpath)
	end
end
Of course, you need to distribute the right binaries for your platform. This makes your game less portable than vanilla Love2D.

Re: Is C compatible with Love2D?

Posted: Mon Oct 01, 2018 10:25 am
by Chris951
How to reload a plugin that actually works???...

I have tried several times to reload a existing pluging on lovedebug but it's not working very well... When there an error on reload it, It can never reload again by forcing a manual relaunch, Please tell me how to do it exactly?

Re: Is C compatible with Love2D?

Posted: Mon Oct 01, 2018 10:37 pm
by Kindermaus
Reloading a shared library generally requires loading the DLL with a different path.

For example, Unreal Engine generates libraryXXX.dll where XXX is unique per build.