Is C compatible with Love2D?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
noahfeazell
Prole
Posts: 1
Joined: Tue Sep 25, 2018 7:34 pm

Is C compatible with Love2D?

Post by noahfeazell »

I'm curious if we can use C in Love2D. Maybe it has said it somewhere but I missed it.
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: Is C compatible with Love2D?

Post 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.
User avatar
Kindermaus
Prole
Posts: 33
Joined: Sun Sep 23, 2018 5:08 am

Re: Is C compatible with Love2D?

Post 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.
ItsyRealm, a quirky 2D/3D RPG where you fight, skill, and explore in a medieval world with horrors unimaginable.
:emo: they / she
Chris951
Prole
Posts: 1
Joined: Tue Sep 11, 2018 6:00 am

Re: Is C compatible with Love2D?

Post 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?
Last edited by Chris951 on Tue Oct 02, 2018 6:00 am, edited 3 times in total.
User avatar
Kindermaus
Prole
Posts: 33
Joined: Sun Sep 23, 2018 5:08 am

Re: Is C compatible with Love2D?

Post 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.
ItsyRealm, a quirky 2D/3D RPG where you fight, skill, and explore in a medieval world with horrors unimaginable.
:emo: they / she
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 23 guests