I'm new to Lua, so I'm confused

Hello, I’m not a very experienced programmer, my past experience was writing couple of ios apps and games using Objective-C&SWIFT, I’ve been hearing that LUA is supposed to be a simple and a quick scripting language, I tried the side-scroller tutorial, and the very first tutorial with the frog, but I’m very confused and the lack of auto-complete in the editor certainly is not helping, I don’t know the vmath functions for example, or what parameters they should take, also I can’t distinguish between strings that belong to the system or ones we define ourselves. I’m also finding this “message” system to be confusing, I’m used to simple, classic OOP in communicating between classes, components. So is there a proper website or tutorial that could help me understand Lua better before I dive in here, I also find this function from the first tutorial extremely confusing:

local function handle_geometry_contact(self, normal, distance)
    -- project the correction vector onto the contact normal
    -- (the correction vector is the 0-vector for the first contact point)
local proj = vmath.dot(self.correction, normal)
    -- calculate the compensation we need to make for this contact point
    local comp = (distance - proj) * normal
    -- add it to the correction vector
    self.correction = self.correction + comp
    -- apply the compensation to the player character
    go.set_position(go.get_position() + comp)
    -- check if the normal points enough up to consider the player standing on the ground
    -- (0.7 is roughly equal to 45 degrees deviation from pure vertical direction)
    if normal.y > 0.7 then
        self.ground_contact = true
    end
    -- project the velocity onto the normal
    proj = vmath.dot(self.velocity, normal)
    -- if the projection is negative, it means that some of the velocity points towards the contact point
    if proj < 0 then
        -- remove that component in that case
        self.velocity = self.velocity - proj * normal
    end

Why are we making all these physics calculation, isn’t there supposed to be a built-in physics engine?!

I hope I’m not being too much of a burden :blush:

Hello @Abdou23!

I can understand your confusion, The snippet of code you posted is probably the most complicated there is in the tutorials. There’s a bit of physics and math going on that might overwhelm a new user of Defold. There is a physics engine (Box2D) in Defold and you can let the physics engine resolve all collisions by setting the type of collision objects to Dynamic instead of Kinematic. If you use Dynamic objects you can only control them by applying forces to them while a Kinematic object can be moved around simply by programmatically changing it’s position. The platformer example uses Kinematic objects since you get absolute control over position and movement, which makes sense in a platformer. If you were to create a game similar to Angry Birds then you’d probably opt for Dynamic objects instead and let the physics system resolve collisions.

When it comes to auto-complete the editor should be able to provide you with auto complete for the built in Defold modules such as go, gui, vmath etc. You can also enable a kind of fuzzy auto complete in the key bindings of the editor that allows you to auto complete based on words that exist in open script files.

You will find that message passing takes a while to get used to if you come from classic OOP programming, but once you do you will be able to write very nice decoupled and reusable scripts and components that communicate via message passing. You should however take note of the fact that message passing is not to be confused with function calls.. And there is nothing stopping you from using Lua modules to share code between scripts. And you can also add a layer of OOP on top of it all using any of the popular OOP modules for Lua (although I would mostly recommend against it).

Finally, I can recommend that you take a look at Programming in Lua, available as a book or online. Remember that Defold uses Lua 5.1 and not the more recent 5.2 or 5.3.

And please don’t hesitate to ask your questions here! We’re here to help!

4 Likes

Welcome to the forum!

The editor support for Lua is a bit lacking, but there auto-completion in the editor for all Defold specific Lua functions, like the vmath.* library. It should pop up automatically with information on parameters and basic documentation. You can also hit ctrl-space to bring it up.

The in editor documentation corresponds to the on-line reference documentation that you find here: http://www.defold.com/ref

There are many great resources on Lua. The official reference manual can be found here: http://www.lua.org/manual/5.1/

Asynchronous message passing is an addition that Defold makes on top of Lua. We believe that OOP with inheritance, methods etc is not the best way to build a game. If you are used to program in C#, Java or similar, the transition to message passing takes some getting used to. Instead of mixing data and logic into many classes of objects, think about your data and how to transform and represent it, with objects that isolate their state. Messages are intended to be a higher abstraction than method calls. Methods often directly access and modify the internal state of objects (getters and setters are the most obvious example), creating coupling between object which often leads to intricate dependencies and bugs. Messages are typically used for objects to send requests to other objects to perform actions. All handling of internal state happens inside the receiving object.

Sure, Defold includes the Box 2D physics engine for 2D physics (and Bullet for 3D). You can definitely use the physics engine’s solver to create your player character but I would not recommend it. The physics manual at http://www.defold.com/doc/physics describes how to use the engine and also includes some discussion about when to use kinematic vs dynamic objects.

3 Likes

Thank you so much guys for clearing some stuff up, indeed, the code editor needs some basic additions, the “Message” system will need some used to, I understand the concept behind that that you want to remove any exposure between classes, to minimize bugs or whatever, but that could be limiting in certain situations, for example in “unity” if you want to access another game object component you can do something like this:
gameObject.GetComponent<RigidBody2D>
I think that could have been more practical. I would also suggest making some video tutorials because sometimes the written tutorials are not clear or accurate enough and in certain situations they leave some stuff out.

I understand that Kinematic objects need to be moved manually, but isn’t there some kind of “Action” system like in “Cocos” or “SpriteKit” or a helper method like in “unity” “transform.forward”?

You can still access position, size, scale and other properties of other game objects through the functions on the go module. Most (all?) functions take an optional id which if specified will make the function operate on the game object with that specific id and if omitted operate on the game object the scripts is attached to. Example:

– get position of another game object
local other_pos = go.get_position(“some_game_object”)

– get position of "self"
local my_pos = go.get_position()

As for video tutorials there’s a bunch of video tutorials on the Defold YouTube channel: https://www.youtube.com/channel/UCrEYXG15n3V0Y8eK0RbCaLQ

And @Andreas_Jirenius is livecoding here: https://www.livecoding.tv/setthebet/

1 Like

@Abdou23
the stream is awesome for learning how to use Lua in Defold.
In the beginning you don’t really know how to structure your game because of the non-OOP paradigm and watching a professional do it helps a lot.
I went back and rewrote parts of my game after watching the stream.

5 Likes

Excellent! I’m sure @Andreas_Jirenius will be thrilled to hear that!

1 Like

Thank you guys for your help, the engine itself is quite similar to unity in some parts, but the language definitely needs some getting used to, especially the metatables stuff, and the use of under_scores instead of camelCases, I also hope the code editor gets a massive reshape very quickly, it needs to at least auto complete brackets, end, and the properties&functions I create to even be considered basic, also, a dark theme for the entire thing would be a very welcomed addition.

1 Like

There’s a new editor on the works and I’m pretty sure the script editor will get it’s fair share of improvements (and yes, there will be a dark theme!).

When it comes to coding conventions, naming and things like that there is no 100% agreed upon convention for Lua code. Some of the major 3rd party libraries use camel_case while Lua libraries uses all lowercase and no underscores and Defold uses snake_case. One style guide that I do like and we use more or less as it is here at King is this one: https://github.com/Olivine-Labs/lua-style-guide

Lua metatables are very powerful and can be put to both good and bad use. Use them with care!

Just to clarify some global things. Are you really think that supporting and developing of builtin editor is good decision? Why not to give this work, I mean code editing, to other players on the market and concentrate on more important engine development tasks?
I use sublime text, it does it’s job on very good level. And, with all due respect, I don’t think builtin editor will bring features for me to move on it in the nearest future.
Thanks for your work btw! :slight_smile:

The goal is to be able to work “good enough” directly in the editor once you’ve downloaded it. As a means to learn what the engine exposes in terms of an API, that means auto completion. Also we wish to support the standard text editing features. The goal is not to replace all editors out there. Note that there are many users that write code in a separate text editor, using their own tools/features in order to work faster (e.g. a custom build process). Here is one example.

4 Likes