Lua Questions

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

Thread data type in Lua.

by Andrey01 » Fri Mar 23, 2018 17:05

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

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

Re: Thread data type in Lua.

by hajo » Fri Mar 23, 2018 20:02

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: 2128
Joined: Wed Oct 19, 2016 15:18
Location: Russia, Moscow
GitHub: Andrey2470T
In-game: Andrey01

Re: Thread data type in Lua.

by Andrey01 » Sat Mar 24, 2018 06:50

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.
DL9Eh7xVBw7DXwLAMX1lStE21bSD2XUQ
 

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

Lua Questions

by Andrey01 » Sat Apr 07, 2018 17:13

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?
DL9Eh7xVBw7DXwLAMX1lStE21bSD2XUQ
 

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

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

Re: A book with tasks in Lua

by hajo » Sat Apr 07, 2018 20:17

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: 2128
Joined: Wed Oct 19, 2016 15:18
Location: Russia, Moscow
GitHub: Andrey2470T
In-game: Andrey01

Re: A book with tasks in Lua

by Andrey01 » Sun Apr 08, 2018 18:54

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.
DL9Eh7xVBw7DXwLAMX1lStE21bSD2XUQ
 

User avatar
v-rob
Member
 
Posts: 553
Joined: Thu Mar 24, 2016 03:19
GitHub: v-rob

Re: A book with tasks in Lua

by v-rob » Sun Apr 08, 2018 20:56

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.
 

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

Re: A book with tasks in Lua

by hajo » Sun Apr 08, 2018 21:43

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: 2128
Joined: Wed Oct 19, 2016 15:18
Location: Russia, Moscow
GitHub: Andrey2470T
In-game: Andrey01

Problem with massive printing in Lua.

by Andrey01 » Mon Apr 09, 2018 14:43

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.
DL9Eh7xVBw7DXwLAMX1lStE21bSD2XUQ
 

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

Re: Problem with massive printing in Lua.

by Krock » Mon Apr 09, 2018 15:16

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))
Mod Search Engine - Mods by Krock - DuckDuckGo mod search bang: !mtmod <keyword here>
 

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

Ways for input of data.

by Andrey01 » Wed Apr 11, 2018 17:41

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
DL9Eh7xVBw7DXwLAMX1lStE21bSD2XUQ
 

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

Re: Lua Questions

by sofar » Wed Apr 11, 2018 17:47

Do something like:
Code: Select all
input = {"first", "second", "third" }
 
for _, v in pairs(input) do
    print(v)
end
 

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

Re: Lua Questions

by rubenwardy » Wed Apr 11, 2018 17:47

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")
 

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

Re: Lua Questions

by Andrey01 » Wed Apr 11, 2018 17:55

Thanks.
DL9Eh7xVBw7DXwLAMX1lStE21bSD2XUQ
 

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

Re: Lua Questions

by Andrey01 » Wed Apr 11, 2018 18:59

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 7 times
DL9Eh7xVBw7DXwLAMX1lStE21bSD2XUQ
 

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

Re: Lua Questions

by Andrey01 » Thu Apr 12, 2018 18:41

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 1 time
DL9Eh7xVBw7DXwLAMX1lStE21bSD2XUQ
 


Return to Modding Discussion



Who is online

Users browsing this forum: No registered users and 2 guests