Lua Questions

Post Reply
User avatar
Andrey01
Member
Posts: 2574
Joined: Wed Oct 19, 2016 15:18
GitHub: Andrey2470T
In-game: Andrey01
Location: Russia, Moscow

Thread data type in Lua.

by Andrey01 » Post

Please, explain what "thread" data type is and what it needs for.

hajo
Member
Posts: 606
Joined: Thu Oct 13, 2016 10:45
Location: DE
Contact:

Re: Thread data type in Lua.

by hajo » Post

Andrey01 wrote:Please, explain what "thread" data type is and what it needs for.
You have read the http://lua-users.org/wiki/ThreadsTutorial ?

User avatar
Andrey01
Member
Posts: 2574
Joined: Wed Oct 19, 2016 15:18
GitHub: Andrey2470T
In-game: Andrey01
Location: Russia, Moscow

Re: Thread data type in Lua.

by Andrey01 » Post

hajo wrote:
Andrey01 wrote:Please, explain what "thread" data type is and what it needs for.
You have read the http://lua-users.org/wiki/ThreadsTutorial ?
No, didn`t read there. I read on lua.org manual (on Russian language). I`ll read there, too, thanks.

User avatar
Andrey01
Member
Posts: 2574
Joined: Wed Oct 19, 2016 15:18
GitHub: Andrey2470T
In-game: Andrey01
Location: Russia, Moscow

Lua Questions

by Andrey01 » Post

I read Lua manual and i would like to check my knowledge that i got while i was reading that. Where can i find a book that i could solve problem in on Lua?

User avatar
rubenwardy
Moderator
Posts: 6972
Joined: Tue Jun 12, 2012 18:11
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy
Location: Bristol, United Kingdom
Contact:

Re: A book with tasks in Lua

by rubenwardy » Post

There's Minetest-specific tasks at the end of most chapters here: http://rubenwardy.com/minetest_modding_book/
Renewed Tab (my browser add-on) | Donate | Mods | Minetest Modding Book

Hello profile reader

hajo
Member
Posts: 606
Joined: Thu Oct 13, 2016 10:45
Location: DE
Contact:

Re: A book with tasks in Lua

by hajo » Post

Andrey01 wrote:Where can i find a book that i could solve problem in on Lua?
Rosettacode is a wiki with a lot of programming-tasks,
solved in many programming-languages, and lua is among them.

You can look at existing solutions, and contribute your own.

Also, Trainsported is a game/simulation where you have to write programs in lua.

User avatar
Andrey01
Member
Posts: 2574
Joined: Wed Oct 19, 2016 15:18
GitHub: Andrey2470T
In-game: Andrey01
Location: Russia, Moscow

Re: A book with tasks in Lua

by Andrey01 » Post

rubenwardy wrote:There's Minetest-specific tasks at the end of most chapters here: http://rubenwardy.com/minetest_modding_book/
Thanks, but there are Minetest tasks that need the knowledge of Minetest API to solve them. But i need just Lua tasks with various themes as if... elif... else... construction, working with varyables, massives, tables and etc... also some cycles for, while, repeat...
hajo wrote:
Andrey01 wrote:Where can i find a book that i could solve problem in on Lua?
Rosettacode is a wiki with a lot of programming-tasks,
solved in many programming-languages, and lua is among them.

You can look at existing solutions, and contribute your own.

Also, Trainsported is a game/simulation where you have to write programs in lua.
Thanks, but if honestly i`m confused in that wiki and i can not find good tasks. And i would like tasks to be distributed themes and if it would be possible to solve online.

User avatar
v-rob
Developer
Posts: 970
Joined: Thu Mar 24, 2016 03:19
GitHub: v-rob
IRC: v-rob
Location: Right behind you.

Re: A book with tasks in Lua

by v-rob » Post

Andrey01 wrote:... elif...
Just wanted to point out that that is Python syntax (And maybe the syntax of other languages as well). Lua uses elseif, not elif.
Core Developer | My Best Mods: Bridger - Slats - Stained Glass

hajo
Member
Posts: 606
Joined: Thu Oct 13, 2016 10:45
Location: DE
Contact:

Re: A book with tasks in Lua

by hajo » Post

Andrey01 wrote:i need just Lua tasks with various themes as if... for, while, repeat...
hajo wrote:Rosettacode is a wiki with a lot of programming-tasks
i`m confused in that wiki and i can not find good tasks.
Start looking at simple tasks, eg. Conditional_structures.
There, look at the section about that theme/task/problem, done in lua.
You can compare that section with other languages, eg. C, Python, etc.

Do the same for Loops/For, etc.

Currently, there are 872 Programming_Tasks,
and of these 563 have a section/solution.in lua.

So, there are 186 unsolved task left for you to do :)
Maybe look at Angle_difference_between_two_bearings,
or Averages/Mean_time_of_day.

But I recommand to just read & understand existing solutions,
and try to do some of them on your own.
i would like tasks .. possible to solve online.
Some online-IDEs that support lua:
* http://www.lua.org/demo.html
* http://codepad.org/
* https://ideone.com/

User avatar
Andrey01
Member
Posts: 2574
Joined: Wed Oct 19, 2016 15:18
GitHub: Andrey2470T
In-game: Andrey01
Location: Russia, Moscow

Problem with massive printing in Lua.

by Andrey01 » Post

I wrote a code:

Code: Select all

mas = {"dungeon_master", "tree", "sand", "oerkki"}
if #mas  == 4 then
     mas[5]  = "stone"
          
elseif  #mas == 5 then
     mas[6], mas[7] = "cow", "sheep"

end

print(mas)
and tried to print the massive after some calculations, but i got it:

Code: Select all

table: 0x11e7e10
But i wanted to print the massive with all elements that it has currently! Why does it happen and what does this mean?
And the same thing happens with tables when they are been printing.

User avatar
Krock
Developer
Posts: 4650
Joined: Thu Oct 03, 2013 07:48
GitHub: SmallJoker
Location: Switzerland
Contact:

Re: Problem with massive printing in Lua.

by Krock » Post

This means that converting a table to string returns some kind of memory address where the table is saved in Lua. Use " dump() " instead:

Code: Select all

print(dump(mas))
Look, I programmed a bug for you. >> Mod Search Engine << - Mods by Krock - DuckDuckGo mod search bang: !mtmod <keyword here>

User avatar
Andrey01
Member
Posts: 2574
Joined: Wed Oct 19, 2016 15:18
GitHub: Andrey2470T
In-game: Andrey01
Location: Russia, Moscow

Ways for input of data.

by Andrey01 » Post

I tried to solve one task. There are two numbers (A and B). I need to print them all with using for loop. For example, in oprder to input random numbers (in this case A and B) there are function input() in Python. But how to do it in Lua? Any like functions or else to input those numbers?

And by the way i`ve noticed there is input() function in Lua, but it is intended for files.

Here is a example:

Code: Select all

number1 = 1 -- [[i need to give a value directly in the code itself. 
In Python i could instead it to write just: number1 = input()]]
number2 = 10

for i = number1, number2 do -- i need to look over all numbers from 1 to 10 here
       print(i) -- and then to print each of them
end

sofar
Developer
Posts: 2146
Joined: Fri Jan 16, 2015 07:31
GitHub: sofar
IRC: sofar
In-game: sofar

Re: Lua Questions

by sofar » Post

Do something like:

Code: Select all

input = {"first", "second", "third" }
 
for _, v in pairs(input) do
    print(v)
end

User avatar
rubenwardy
Moderator
Posts: 6972
Joined: Tue Jun 12, 2012 18:11
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy
Location: Bristol, United Kingdom
Contact:

Re: Lua Questions

by rubenwardy » Post

Merged this all into one thread, as a topic every day with a single reply was a bit much.
Andrey01 wrote:there is the function input() in Python. But how to do it in Lua? Any like functions or else to input those numbers?

Here's the documentation for reading standard in: https://www.lua.org/pil/21.1.html
Use io.read():

Code: Select all

local number1 = io.read("*number")
Or for multiple:

Code: Select all

local number1, number2 = io.read("*number", "*number")
Renewed Tab (my browser add-on) | Donate | Mods | Minetest Modding Book

Hello profile reader

User avatar
Andrey01
Member
Posts: 2574
Joined: Wed Oct 19, 2016 15:18
GitHub: Andrey2470T
In-game: Andrey01
Location: Russia, Moscow

Re: Lua Questions

by Andrey01 » Post

Thanks.

User avatar
Andrey01
Member
Posts: 2574
Joined: Wed Oct 19, 2016 15:18
GitHub: Andrey2470T
In-game: Andrey01
Location: Russia, Moscow

Re: Lua Questions

by Andrey01 » Post

I was trying to rewrite my cube_nodes mod. I wanted to write registration of blocks simpler than huge code was earlier. But here`s an error. It says on line:79 table index is nil. Why so? Why is it nil? See my code below.
cube_nodes mod.zip
(908 Bytes) Downloaded 42 times

User avatar
Andrey01
Member
Posts: 2574
Joined: Wed Oct 19, 2016 15:18
GitHub: Andrey2470T
In-game: Andrey01
Location: Russia, Moscow

Re: Lua Questions

by Andrey01 » Post

Please help me somebody, and here is a error:

Code: Select all

2018-04-12 21:39:45: ERROR[Main]: ModError: Failed to load and run script from /home/user/.minetest/mods/cube_nodes/init.lua:
2018-04-12 21:39:45: ERROR[Main]: /home/user/.minetest/mods/cube_nodes/nodes.lua:79: table index is nil
2018-04-12 21:39:45: ERROR[Main]: stack traceback:
2018-04-12 21:39:45: ERROR[Main]: 	/home/user/.minetest/mods/cube_nodes/nodes.lua:79: in main chunk
2018-04-12 21:39:45: ERROR[Main]: 	[C]: in function 'dofile'
2018-04-12 21:39:45: ERROR[Main]: 	/home/user/.minetest/mods/cube_nodes/init.lua:5: in main chunk
2018-04-12 21:39:45: ERROR[Main]: Подробная информация в debug.txt.
Download my mod:
cube_nodes.zip
(20.62 KiB) Downloaded 21 times

Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests