Scripting tutorial in Leadwerks development environment
I do not pretend that this tutorial is an absolute truth, it is just how I have learned to do things, very possibly I am wrong, but always willing to learn.
When programming, it is extremely necessary to plan what you want to do, a coding project should be divided into blocks, function files.
It is like writing a book, by pages, by chapters, I speak of a book of modern times, those books that you can touch and pass its pages with a finger wet in saliva, of course because of the pandemic I think that is a danger to do.
Well, it is another thing to make a codification as if we were in the first century of our era writing the history of Egypt on a parchment.
What I mean by these two examples of the book and the papyrus is that it is good to break the whole code into parts, as when you create a wall, which is made up of much smaller blocks. Unless you are a machine, you will surely put all the code of the player entity in a single script (scroll), but as I have found it easier and more comfortable to simulate a development environment focused on object programming in Lua Script.
And how do I do this, through the use of tables.
function Script:Start()
end
This is the Start function of a file attached to a player entity. Inside it I can put everything I can think of for the player entity, ragdoll effect, movement, hud system for life indicators, oxygen, animations, etc. But I feel more comfortable splitting this into files, let's see an example.
import("Scripts/Objects/Player/Classes/CPlayer.lua") function Script:Start() self.player = CPlayer:New() end
With the Import command, I link another file called CPlayer ( Player Class ), remember this is a kind of simulation of classes and objects.
And in that file I have the following.
import("Scripts/Objects/Player/Classes/CRagDoll.lua") CPlayer={} function CPlayer:New() local this={} this.ragDoll = CRagDoll:Start() function this:Start() end this:Start() return( this ) end
As you can see, inside that CPlayer file, I import another file called CRagdoll, and the principle is totally the same, I repeat this is the way I have found this is more comfortable for me, possibly you feel better putting everything in one file.
What is a class?
The best way to understand what a class is, is to think of a cookie mold, from which many cookie objects come out, these objects differ from each other in the flavor of the cookie, its color, its texture. That is to say, from a House class, we can create many houses, where their properties such as color and materials vary a little, making each house different.
So let's create a Globe class in Lua Script.
CGlobe={} function CGlobe:Start() end
That's how simple we create our mock class in Lua, a balloon class, where we create balloon objects. The next step is to define the properties of the balloon object, for example its color.
CGlobe={} function CGlobe:New() local this={} this.color = "Red" function this:Create() System:Print("Create Globe... OK!) end this:Create() function this:SetColor(color) this.color = color end function this:GetColor() return ( this.color ) end return ( this ) end
At this point I create the constructor of the object, it is simply the New function, which returns me the table this, and this calls the start function, creating a default object of red color.
We have two methods or functions, SetColor, to give a new color to the balloon, and one called GetColor to retrieve the color of the balloon
It should be noted that by handling this file system, you can have a large number of well-organized "objects" on your project that do something. In this case you can create different balloons of different colors.
import("Scripts/Objects/Player/Classes/CGlobe.lua") function Script:Start() self.myGlobeRed = CGlobe:Create() System:Print(self.myGlobeRed:GetColor()) self.myGlobeOrange = CGlobe:Create() self.myGlobeOrange:SetColor("Orange") end
This is the idea I want to convey, dividing a project into smaller parts will make your life much easier.
- 2
1 Comment
Recommended Comments