Lua Scripting: Update chapter
This commit is contained in:
parent
e88b892551
commit
b5fcac7ed5
@ -9,8 +9,8 @@ redirect_from: /en/chapters/lua.html
|
|||||||
|
|
||||||
## Introduction <!-- omit in toc -->
|
## Introduction <!-- omit in toc -->
|
||||||
|
|
||||||
In this chapter we'll talk about scripting in Lua, the tools required
|
In this chapter, you'll learn about scripting in Lua, the tools required
|
||||||
to assist with this, and some techniques which you may find useful.
|
to help with this, and some techniques that you may find useful.
|
||||||
|
|
||||||
- [Programming](#programming)
|
- [Programming](#programming)
|
||||||
- [Coding in Lua](#coding-in-lua)
|
- [Coding in Lua](#coding-in-lua)
|
||||||
@ -24,23 +24,21 @@ to assist with this, and some techniques which you may find useful.
|
|||||||
|
|
||||||
Programming is the action of taking a problem, such as sorting a list
|
Programming is the action of taking a problem, such as sorting a list
|
||||||
of items, and turning it into steps that a computer can understand.
|
of items, and turning it into steps that a computer can understand.
|
||||||
|
|
||||||
Teaching you the logical process of programming is beyond the scope of this book;
|
Teaching you the logical process of programming is beyond the scope of this book;
|
||||||
however, the following websites are quite useful in developing this:
|
however, the following websites are quite useful in developing this:
|
||||||
|
|
||||||
* [Codecademy](http://www.codecademy.com/) is one of the best resources for
|
* [Codecademy](http://www.codecademy.com/) is one of the best resources for
|
||||||
learning to write code. It provides an interactive tutorial experience.
|
learning to write code. It provides an interactive tutorial experience.
|
||||||
* [Scratch](https://scratch.mit.edu) is a good resource for starting from
|
* [Scratch](https://scratch.mit.edu) is a good resource for starting from
|
||||||
absolute basics, and learning the problem-solving techniques required to program.\\
|
absolute basics, and learning the problem-solving techniques required to program.
|
||||||
Scratch is *designed to teach children* how to program and isn't a serious
|
It's great for children and teenagers.
|
||||||
programming language.
|
|
||||||
* [Programming with Mosh](https://www.youtube.com/user/programmingwithmosh) is
|
* [Programming with Mosh](https://www.youtube.com/user/programmingwithmosh) is
|
||||||
a good YouTube series to learn programming.
|
a good YouTube series to learn programming.
|
||||||
|
|
||||||
### Coding in Lua
|
### Coding in Lua
|
||||||
|
|
||||||
It's also beyond the scope of this book to teach Lua coding.
|
It's also beyond the scope of this book to teach Lua coding.
|
||||||
The [Programming in Lua](https://www.lua.org/pil/contents.html) book is an
|
The [Programming in Lua (PiL)](https://www.lua.org/pil/contents.html) book is an
|
||||||
excellent introduction to Lua programming.
|
excellent introduction to Lua programming.
|
||||||
|
|
||||||
|
|
||||||
@ -74,10 +72,10 @@ Functions which come with Lua by default, such as `table.insert`, are also highl
|
|||||||
|
|
||||||
Commonly used editors which are well-suited for Lua include:
|
Commonly used editors which are well-suited for Lua include:
|
||||||
|
|
||||||
* [VSCode](https://code.visualstudio.com/) -
|
* [VSCode](https://code.visualstudio.com/):
|
||||||
open source (as Code-OSS or VSCodium), popular, and has
|
open source (as Code-OSS or VSCodium), popular, and has
|
||||||
[plugins for Minetest modding](https://marketplace.visualstudio.com/items?itemName=GreenXenith.minetest-tools).
|
[plugins for Minetest](https://marketplace.visualstudio.com/items?itemName=GreenXenith.minetest-tools).
|
||||||
* [Notepad++](http://notepad-plus-plus.org/) - Windows-only
|
* [Notepad++](http://notepad-plus-plus.org/): simple, Windows-only
|
||||||
|
|
||||||
Other suitable editors are also available.
|
Other suitable editors are also available.
|
||||||
|
|
||||||
@ -85,8 +83,25 @@ Other suitable editors are also available.
|
|||||||
## Local and Global Scope
|
## Local and Global Scope
|
||||||
|
|
||||||
Whether a variable is local or global determines where it can be written to or
|
Whether a variable is local or global determines where it can be written to or
|
||||||
read from. A local variable is only accessible from where it is defined. Here
|
read from. Global variables can be accessed from anywhere in the script file,
|
||||||
are some examples:
|
and from any other mod:
|
||||||
|
|
||||||
|
```lua
|
||||||
|
function one()
|
||||||
|
foo = "bar"
|
||||||
|
end
|
||||||
|
|
||||||
|
function two()
|
||||||
|
print(dump(foo)) -- Output: "bar"
|
||||||
|
end
|
||||||
|
|
||||||
|
one()
|
||||||
|
two()
|
||||||
|
```
|
||||||
|
|
||||||
|
In constrast, a local variable is only accessible from where it is defined.
|
||||||
|
Lua defaults to variables being global, so you need to explicitly use the
|
||||||
|
`local` keyword:
|
||||||
|
|
||||||
```lua
|
```lua
|
||||||
-- Accessible from within this script file
|
-- Accessible from within this script file
|
||||||
@ -103,22 +118,6 @@ function myfunc()
|
|||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
In contrast, global variables can be accessed from anywhere in the script file, and
|
|
||||||
from any other mod.
|
|
||||||
|
|
||||||
```lua
|
|
||||||
function one()
|
|
||||||
foo = "bar"
|
|
||||||
end
|
|
||||||
|
|
||||||
function two()
|
|
||||||
print(dump(foo)) -- Output: "bar"
|
|
||||||
end
|
|
||||||
|
|
||||||
one()
|
|
||||||
two()
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
||||||
### Locals should be used as much as possible
|
### Locals should be used as much as possible
|
||||||
|
|
||||||
@ -170,6 +169,9 @@ end
|
|||||||
mymod.foo("foobar")
|
mymod.foo("foobar")
|
||||||
```
|
```
|
||||||
|
|
||||||
|
`function mymod.foo()` is equivalent to `mymod.foo = function()`, it's just a
|
||||||
|
nicer way to write it.
|
||||||
|
|
||||||
## Including other Lua Scripts
|
## Including other Lua Scripts
|
||||||
|
|
||||||
The recommended way to include other Lua scripts in a mod is to use *dofile*.
|
The recommended way to include other Lua scripts in a mod is to use *dofile*.
|
||||||
@ -182,11 +184,13 @@ A script can return a value, which is useful for sharing private locals:
|
|||||||
|
|
||||||
```lua
|
```lua
|
||||||
-- script.lua
|
-- script.lua
|
||||||
return "Hello world!"
|
local module = {}
|
||||||
|
module.message = "Hello World!"
|
||||||
|
return module
|
||||||
|
|
||||||
-- init.lua
|
-- init.lua
|
||||||
local ret = dofile(minetest.get_modpath("modname") .. "/script.lua")
|
local ret = dofile(minetest.get_modpath("modname") .. "/script.lua")
|
||||||
print(ret) -- Hello world!
|
print(ret.message) -- Hello world!
|
||||||
```
|
```
|
||||||
|
|
||||||
[Later chapters](../quality/clean_arch.html) will discuss how best to split up
|
[Later chapters](../quality/clean_arch.html) will discuss how best to split up
|
||||||
|
Loading…
Reference in New Issue
Block a user