Extending the build in modules? (SOLVED)

Is there any method of adding functions to the built in defold modules such as the Vector math library? In C# you can do extension methods and such but I’m not sure if that sort of thing exists in Lua?

I’m really missing some of the Vector functions that are in the Unity Vector3 library such as MoveTowards and it would be nice to be able to add them in some way without making them in their own separate module

I would recommend that you add them to your own module since it is much more obvious to anyone looking at your code that they are indeed extensions and not part of the official API. But if you do insist to extend vmath then it is super-easy to do so using Lua. All Defold and Lua modules or “namespaces” are plain old Lua tables with the functions being values on the table, keyed by function name. This means that you can add any functionality you want to existing modules (you can even replace or remove existing functions):

-- add a move_towards function
function vmath.move_towards(from, to)
	--- do stuff
end

-- replace an existing function 
function vmath.length(v)
	--- do stuff
end

-- remove a function
vmath.normalize = nil
4 Likes

Ah ok very cool thanks for the explanation :+1:

1 Like