Advertisement
realmaster42

Block Universal Lua API guide

Apr 6th, 2018
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.64 KB | None | 0 0
  1. discord.gg/fwKEAJF
  2. Block Universal
  3. don't mind the format because it's supposed to be in Unity and looks better there, but since it's in build 5 and specifically in the Lobby I can't share, sorry
  4.  
  5. if you want to test your scripts, DM me in discord and I'll test them for you
  6. ok now have fun reading this wonderful guide
  7.  
  8. (===) means new chapter
  9.  
  10. (===) Introduction
  11. <size=18>Introduction</size>
  12. Welcome to Block Universal's official Lua API guide.
  13.  
  14. In this guide the fundamentals of Lua and BU's integration for it are explained and worked on.
  15.  
  16. Any word or sentence in this guide under Italics represent Lua code.
  17.  
  18.  
  19. If you'd like to skip to BU API, please move on to the chapter <i>BU and Lua</i>.
  20.  
  21. This chapter is only recommended for Lua beginners or people who aren't familiar with Lua.
  22.  
  23. <size=18>Starting off easy</size>
  24. To begin with Lua, you must understand its syntax and logics. This isn't a hard thing to do, seen by how Lua is so easy and easily learnt.
  25.  
  26. One key function in BU Lua is <i>print()</i>.
  27. This function adds text into the output. (This will be explained shortly)
  28.  
  29. If you do <i>print('Hello World!')</i>, Hello World! will appear under the output.
  30.  
  31. <size=18>Output</size>
  32. The output is a list of messages that come from the scripts. As the word indicates, 'output', these are messages who come out of the script.
  33.  
  34. Remember the <i>print('Hello World!')</i> from earlier on? That is going to output 'Hello World!'.
  35.  
  36. Any print will, infact, output. This is a special function that only outputs.
  37.  
  38. <size=18>Strings</size>
  39. Strings are another way of saying 'sentence' or 'line'. A string is basically text.
  40.  
  41. Anything that represents text is a string; for example, 'I am not a string', is a string.
  42.  
  43. You can form strings with two characters, as long as it starts and ends with the same character. These characters are <i>'double</i> and <i>'</i>.
  44.  
  45. This means 'doubleHey I am beautiful'double and 'Hey I am beautiful' represent no difference, meaning both are strings.
  46.  
  47. <size=18>Math</size>
  48. Mathematics are used in Lua, of course.
  49. Don't worry though, you don't need to go all advanced with Maths!
  50.  
  51. In Lua there are basic math operators, such as sum or subtract.
  52. Respectively,
  53. + -> Sum
  54. - -> Subtract
  55. / -> Divide
  56. * -> Multiply
  57. % -> Modulus
  58. ^ -> Exponent
  59.  
  60. Surprisingly enough, you can use any type of data in <i>print()</i>!
  61.  
  62. This means doing <i>print(5 * 5 / 25 + 5 - 2.5 * 2)</i> is possible!
  63.  
  64. <size=18>Data types</size>
  65. There are multiple data types in Lua, but they don't require specification!
  66.  
  67. Strings were specified earlier on this guide;
  68. Integers are whole numbers, such as 1, 2, 3...;
  69. Decimals are numbers that aren't whole, as for example 1.23, 6.74, 9.99...;
  70. Booleans represent true or false values, literally. The only booleans that exist are <i>true</i> and <i>false</i>;
  71. Nil is a special data type which represents no value. This data type also only exists under the keyword <i>nil</i>;
  72. Arrays are lists of stuff together, for example, {1, 2, 3}.
  73.  
  74. There are strings, integers, decimals, arrays and some couple others.
  75. * Arrays are named 'tables' in Lua.
  76.  
  77. (===) Basics
  78. <size=18>Variables</size>
  79. Ah, finally the good stuff.
  80.  
  81. Variables can be any data type, and you can name them.
  82.  
  83. For example, if you do a script <i>i = 'Hello World!'</i>, <i>i</i> will represent 'Hello World!' anywhere in the script.
  84.  
  85. Of course, you can use variables in functions.
  86. Example script:
  87. <i>i = 'I hate variables, eh'
  88. print(i)</i>
  89. (Output would be <i>I hate variables, eh</i>)
  90.  
  91. <size=18>If Statements</size>
  92. Now, on to more serious stuff.
  93.  
  94. If Statements are the most common logical condition found on Lua scripts, and they're essential to scripting.
  95.  
  96. With these, you can do predictions, checks and other stuff.
  97.  
  98. They start with an <i>if</i>, that must be followed by the condition and the target result.
  99.  
  100. Seems confusing?
  101. Here's an example:
  102.  
  103. Applying everything in this guide so far, you could make a calculator, and a prediction for it:
  104. <i>i = 5 * 5
  105. x = i / 2.5
  106.  
  107. if x == i * 2.5 then
  108. print('I had a F on maths.')
  109. end
  110.  
  111. if x == i / 2.5 then
  112. print('I had a A on maths.')
  113. end</i>
  114.  
  115. If it seems a bit confusing, don't worry! This is normal for anybody who isn't familiar with Lua.
  116.  
  117. If Statements must be specified on what you want to check.
  118. You've got multiple logical conditions:
  119. == -> Equal to
  120. ~= -> Not equal to
  121. >= -> Greater or equal to
  122. <= -> Smaller or equal to
  123. > -> Greater than
  124. < -> Smaller than
  125.  
  126. The If Statement model is usually this: <i>if condition then</i>
  127. They also require a keyword to note that the condition code is over: <i>end</i>
  128.  
  129. Let's say you want to check if a certain formula results in a higher value than another, you can do this:
  130. <i>x = 7 * 9 / 3.7
  131. y = 9 * 4 / 1.1
  132.  
  133. if x > y then
  134. print('seems like x is greater')
  135. end
  136. if x < y then
  137. print('seems like x is smaller')
  138. end
  139. if x == y then
  140. print('seems like x equals to y (how)')
  141. end</i>
  142.  
  143. Of course, typing all these would be a bit boring and tiring, but don't worry! Lua has got the solution for you.
  144.  
  145. Instead of only doing <i>if</i>, there are also two other If Statement beginning keywords: <i>elseif</i> and <i>else</i>.
  146. These different keywords require an initial <i>if</i> to exist, which means without one they can't be there, or the script will result in an error.
  147.  
  148. The <i>elseif</i> keyword checks if another condition applies, and the <i>else</i> keyword is for when none of the conditions specified before apply.
  149.  
  150. Here's the previous code, but with the new keywords:
  151. <i>x = 7 * 9 / 3.7
  152. y = 9 * 4 / 1.1
  153.  
  154. if x > y then
  155. print('seems like x is greater')
  156. elseif x < y then
  157. print('seems like x is smaller')
  158. else
  159. print('seems like x equals to y (how)')
  160. end</i>
  161.  
  162. As you can see, the code is a whole lot cleaner and nicer!
  163.  
  164. <size=18>Functions</size>
  165. Uh oh, even more serious stuff.
  166.  
  167. Functions are basically sets of code that run when you call them.
  168.  
  169. Our good ol' friend <i>print()</i> is a function itself.
  170.  
  171.  
  172. When making a function, you must define a name.
  173.  
  174. Here's an empty function's code:
  175. <i>function empty()
  176. end</i>
  177.  
  178. Note the keyword <i>function</i>. This keyword is necessary for any function that is created.
  179. <i>empty</i> is, in this case, the function's name.
  180. And yes, functions require an <i>end</i>.
  181.  
  182. Now, you may be curious about the <i>()</i>.
  183. These are the function's arguments. When calling a function, you may or may not send arguments.
  184.  
  185. As an example, when calling <i>print('Hello World!')</i> you are passing the argument 'Hello World!'.
  186.  
  187. You may be wondering how to initialize arguments in a function.
  188. Well, it's pretty simple!
  189.  
  190. All you have to do is set a name for the argument in the function's header.
  191. Oh yes, function header. It's this code here: <i>function empty()</i>.
  192.  
  193. Now, let's rewrite our function but with an argument!
  194. <i>function empty(arg)
  195. end</i>
  196.  
  197. This means that every time you call <i>empty</i>, <i>arg</i> will be there.
  198.  
  199. To call a function, you use the name followed by the argument.
  200.  
  201. <i>function empty(arg)
  202. print(arg)
  203. end</i>
  204.  
  205. If you were to call <i>empty('Hello World!')</i> now, it'd output <i>Hello World!</i>.
  206.  
  207.  
  208. But what if you want to call multiple of them...?
  209.  
  210. Well, there is yet another solution.
  211.  
  212. <i>function empty(arg1, arg2)
  213. print(arg)
  214. print(arg2)
  215. end</i>
  216.  
  217. Yep. All you need to do is literally add a comma after the first argument and then name the second one.
  218.  
  219. Pretty simple, isn't it?
  220.  
  221. Here's our total script:
  222. <i>function empty(arg1, arg2)
  223. print(arg)
  224. print(arg2)
  225. end
  226.  
  227. empty('Hello World!', 'You are very big.')</i>
  228.  
  229. <size=18>String Concatenation</size>
  230. Yep, it is possible to concatenate strings!
  231.  
  232. It is fairly easy to do so.
  233. All you need to do is add <i>..</i> after the last character of a string and then add the next string after that.
  234.  
  235. Here's an example:
  236. <i>print('Hello '.. 'World!')</i>
  237.  
  238. Of course, you can use variables in this.
  239.  
  240. <i>x = 'World!'
  241. print('Hello '.. x)
  242.  
  243. y = 'Hello '
  244. print(y..x)</i>
  245.  
  246. (===) Loops
  247. <size=18>Loops?</size>
  248. Oh boy, now it's getting serious.
  249.  
  250. Although loops aren't advanced, they are required for most scripts to function properly.
  251.  
  252. Loops can be used for a variety of things and are extremely useful.
  253.  
  254. There are different types of loops; for loops, while loops (...)
  255. Let's start easy...
  256.  
  257. <size=18>For Loops</size>
  258. For Loops are mostly 'counters', as they depend on a numeric value.
  259.  
  260. Here's an example for loop:
  261. <i>for i = 1, 10 do
  262. end</i>
  263.  
  264. Yes, for loops also need the <i>end</i> keyword. Infact, all loops need it, except <i>repeat until</i>.
  265.  
  266. This kind of loops increment the value by 1 (by default), and stop when the value reaches the specified number.
  267.  
  268. Here's an example output of a for loop script:
  269. <i>for i = 1, 3 do
  270. print(i)
  271. end</i>
  272. -> 1, 2, 3
  273.  
  274. When a for loop is made, a new variable with the name you specify is assigned locally in the loop area.
  275.  
  276. Confused? Here's an example:
  277. <i>i = 5
  278. for i = 1, 3 do
  279. print(i)
  280. end
  281. print('real value of i is '.. tostring(i))</i>
  282. -> 1, 2, 3, real value of i is 5
  283.  
  284. Using these loops is very useful when scripting.
  285.  
  286. Here's an example application of a for loop in a script:
  287. <i>function math(number)
  288. for i = 1, number do
  289. number = number * number
  290. end
  291.  
  292. print(number)
  293. end</i>
  294.  
  295. math(2) -> 8
  296.  
  297. You can also increment the variable differently from the default value, 1.
  298. It's very simple to do so:
  299. <i>for i = 3, 1, -1 do
  300. print(i)
  301. end</i>
  302. -> 3, 2, 1
  303.  
  304. <size=18>While Loops</size>
  305. While Loops are loops that never stop unless a condition is no longer met.
  306.  
  307. They're fairly simple to work with, and are extremely useful when scripting.
  308.  
  309. Here's an example of a While Loop:
  310. <i>x = 0.1
  311. while x < 10 do
  312. x = x + x / 2
  313. end</i>
  314.  
  315. As you can see, it starts with the <i>while</i> keyword and requires a condition right next to it, followed by the <i>do</i> keyword.
  316.  
  317. You can use While Loops for multiple purposes, including waiting until something is done.
  318.  
  319. <size=18>Repeat Until</size>
  320. Repeat Until is a special loop, and acts as a While Loop does, except it has the inverse role.
  321.  
  322. Basically, it repeats the code inside it until a condition is met.
  323.  
  324. Here's an example:
  325. <i>x = 1
  326. repeat
  327. x = x + 1
  328. until(x == 10)</i>
  329.  
  330. <size=18>Stopping Loops</size>
  331. In Lua, it's possible to stop loops while they are running.
  332.  
  333. This is done with the <i>break</i> keyword.
  334.  
  335. Here's this keyword in action:
  336. <i>target = 7
  337. for i = 1, target do
  338. if i == target then
  339. break
  340. end
  341. end</i>
  342.  
  343. With that script, when the For Loop <i>i</i> variable reaches the target value, it stops.
  344.  
  345. <size=18>tostring() and tonumber()</size>
  346. They've been used here and there before in this guide.
  347. But what do they really mean?
  348.  
  349. Well, it's actually pretty easy to understand.
  350.  
  351. As their names indicate, they convert the value to a different data type.
  352.  
  353. This means <i>tostring(5)</i> will convert <i>5</i> to <i>'double5'double</i>.
  354. The opposite also applies; <i>tonumber('double5'double)</i> results in <i>5</i>.
  355.  
  356. Of course, converting a data type to another one that is impossible will result in error.
  357. So remember, never convert a string that doesn't represent a numeric value into a integer/decimal. Never. Ever.
  358.  
  359. (===) Lua Basics End
  360. <size=18>Return keyword</size>
  361. The <i>return</i> keyword is yet another keyword belonging to functions.
  362. It is used for a variety of things, but the most common cause of its use is returning a value.
  363.  
  364. To make an example for this, you can do:
  365. <i>function test(a)
  366. return a * 2
  367. end</i>
  368.  
  369. This function will multiply <i>a</i> by 2 and return the new value.
  370.  
  371. Here's an example use for this purpose:
  372. <i>function double(a)
  373. return a * 2
  374. end
  375.  
  376. print(double(5))
  377. print(double(10))</i>
  378. -> 10, 20
  379.  
  380. However, <i>return</i> can also be used to stop functions from running.
  381.  
  382. <i>function test(a)
  383. if a < 0 then
  384. return
  385. end
  386.  
  387. print(tostring(a).. ' is positive!')
  388. end</i>
  389.  
  390. <size=18>Conditional keywords</size>
  391. There are three very important keywords for conditions: <i>and</i>, <i>or</i> and <i>not</i>.
  392.  
  393. The <i>and</i> and <i>or</i> are used very often in Lua for conditions, and these alongside <i>not</i> can be used in If Statements and in Loops, alongside another special use for them.
  394.  
  395. The <i>and</i> and <i>or</i> as their name indicates represent, respectively, the need for another condition to be met or for the other condition to be met.
  396.  
  397. Seems confusing? Here's an example:
  398. <i>function test(number)
  399. if number * number / 10 == 1 or number / 5 == 1 then
  400. print('my random equation is met')
  401. end
  402. end
  403.  
  404. function test2(number)
  405. if number - 5 == 0 and number + 5 == 10 then
  406. print('I think the number is 5')
  407. end
  408. end
  409.  
  410. test(10)
  411. test(5)
  412. test(1)
  413. test2(5)</i>
  414. -> my random equation is met, my random equation is met, I think the number is 5
  415.  
  416. Note how <i>test(1)</i> had no response. This is because none of the conditions matched.
  417.  
  418. Then, we have the <i>not</i> keyword.
  419. This keyword acts as a negator, that inverts the condition's expected result.
  420.  
  421. For example, if you do <i>if x == 5</i> and <i>if not (x == 5)</i>, you are doing inverse conditions.
  422.  
  423. Here's a script using <i>not</i>:
  424. <i>x = 1
  425. if not (x == 1) then
  426. print('the cake is a lie')
  427. else
  428. print('welp')
  429. end</i>
  430. -> welp
  431.  
  432. However, this keyword can also be used with booleans.
  433.  
  434. These haven't been spoke about in this guide yet, so here is a quick tutorial on them:
  435. You've got either <i>true</i> or <i>false</i> as the value. Nothing else.
  436.  
  437. Of course, these represent conditional values.
  438. This means doing <i>if true then</i> will just result in the code in that statement to be ran, because the condition is <i>true</i>.
  439.  
  440. You can negate them using the <i>not</i> keyword:
  441. <i>b = false
  442. if not b then
  443. print('I am always right')
  444. end</i>
  445. -> I am always right
  446.  
  447. <i>b = (5 * 5 == 20)
  448. if not b then
  449. print('5 * 5 actually equals '.. tostring(5 * 5))
  450. end</i>
  451. -> 5 * 5 actually equals 25
  452.  
  453. As it can be deducted from this quick tutorial, booleans are extremely easy.
  454.  
  455. <size=18>Comments</size>
  456. Comments are pieces of code or text that don't run. Basically, they're notes.
  457.  
  458. It is fairly easy to do a note, just add two of the <i>-</i> character and you've got yourself a comment.
  459.  
  460. Here's an example:
  461. <i>-- I am a note, so I don't run in the code
  462. x = 1 -- 3 (old value changed)</i>
  463.  
  464. <size=18>Local keyword</size>
  465. The keyword <i>local</i> is used quite a lot in Lua, and is one of the most important ones to exist in this scripting language.
  466.  
  467. This keyword makes a variable limited to the scope it is in.
  468. Seems confusing? Don't worry. This concept is extremely confusing at first.
  469.  
  470. But here's a quick example that might help:
  471. <i>local x = 5 -- This variable is still global, because the 'doublelocal'double keyword is used in the global scope.
  472.  
  473. function z(y)
  474. local x = y * 2
  475. print(x)
  476. end
  477.  
  478. print(x)
  479. z(x)
  480. print(x)</i>
  481. -> 5, 10, 5
  482.  
  483. As you can see, even though x was changed inside the function, it's value remained equal in the global scope.
  484.  
  485. <size=18>Tables</size>
  486. Tables are, in other words, arrays.
  487. For those unfamiliar with what arrays are, they're lists.
  488.  
  489. Basically, a group of data.
  490.  
  491. Making tables in Lua is extremely easy, but also useful.
  492.  
  493. Here's an example table:
  494. <i>x = { 'I am a string', 5, 'I am another string' }</i>
  495.  
  496. You can access the table's content using indexers. Indexers are also easy to comprehend and use; simply use <i>[number]</i>. Remember to replace number with the index.
  497. Tables in Lua start at the index 1.
  498.  
  499. You can also obtain the table's content count using the <i>#</i> character.
  500.  
  501. Here's an example:
  502. <i>x = { 'I am a string', 5, true }
  503. print(x[0])
  504. print(x[1])
  505. print(x[2])
  506. print(x[3])
  507. print(#x)</i>
  508. -> nil, I am a string, 5, true, 3
  509.  
  510. There're also other fun ways to use tables in Lua.
  511.  
  512. Here's a common way of using tables in Lua:
  513. <i>x = { }
  514. x.testing = 'hello'
  515. print(x.testing)</i>
  516. -> hello
  517.  
  518. There are, also, multi-dimensional tables. Basically, tables that contain tables inside themselves.
  519.  
  520. <i>x = { 'hello', { 'hello' } }
  521. print(#x)
  522. print(#x[2])</i>
  523. -> 2, 1
  524.  
  525. (===) BU and Lua
  526. <size=18>BU Lua API</size>
  527. Block Universal's Lua API's full list of additions and changes can be seen on the Discord server (https://www.discord.gg/fwKEAJF)
  528. These can be seen in the channel #api.
  529.  
  530. Using Lua in Block Universal is quite easy, but requires attention and some understandment of how it works beforehand.
  531.  
  532.  
  533. To begin with scripting with Lua in Block Universal, it is necessary to understand how Lua is applied in Block Universal first.
  534.  
  535. In Block Universal, there is only one script, which the universe's owner must set/script.
  536. That script is then ran clientsided when a player joins the universe.
  537.  
  538. * Extremely important note: The script isn't serversided, which means you must use workarounds to make it act as a server script.
  539.  
  540. <size=18>Timed infinite loops</size>
  541. With Block Universal's Lua API, it's possible to make infinite loops that have a delay between being ran again.
  542.  
  543. This can be done with the <i>wait(seconds)</i> function.
  544. This function stops the script from being executed for a specific seconds amount.
  545.  
  546. Here is a normal while loop, with a 1 second delay per run:
  547. <i>while true do
  548. wait(1)
  549. end</i>
  550.  
  551. However, that doesn't seem too pratical and clean.
  552. Fortunately for you, BU Lua API supports a conditional <i>wait(seconds)</i> function.
  553.  
  554. Here is the same code, but with <i>wait(seconds)</i> as a condition:
  555. <i>while wait(1) do
  556. end</i>
  557.  
  558. As you can see, the loop seems cleaner now.
  559.  
  560. You can also use decimals as an argument, which means you can do half seconds or even lower amounts of time.
  561.  
  562. BU Lua API supports a decimal down to <i>0.001</i>, which is 1 millisecond.
  563.  
  564. The fastest delayed loop would be this:
  565. <i>while wait(0.001) do
  566. end</i>
  567.  
  568. <size=18>[GAME] messages</size>
  569. It is possible to make the script add [GAME] messages to the chat.
  570. However, only the person on the script's end can see it.
  571. Remember; scripts run clientsided.
  572.  
  573. The function for this is <i>chat(message)</i>, which will add to the chat a [GAME] message.
  574.  
  575. Example welcome script:
  576. <i>chat('Welcome to my universe!')</i>
  577. -> [GAME]: Welcome to my universe!
  578.  
  579. However, it'd be nice to be able to welcome the player by their name.
  580. This is also possible, using <i>localname()</i>.
  581.  
  582. This function returns the local player's name.
  583.  
  584. Here is the same welcome script, but calling the player by their name:
  585. <i>chat('Welcome to my universe, '.. localname() ..'!')</i>
  586.  
  587. <size=18>Block Class</size>
  588. The <i>block</i> class is a small but useful class that allows block manipulation.
  589.  
  590. Currently, it consists of two functions: <i>id</i> and <i>place</i>.
  591.  
  592. Using these, it is possible to do a variety of things.
  593. * Remember that to place a block, the local player requires edit access.
  594.  
  595. A possible application of the <i>id(layer, x, y)</i> function is telling the player every used block id in the universe.
  596.  
  597. Of course, for this, you'd need the universe's width and height. This is possible to obtain using <i>universe_width</i> and <i>universe_height</i>.
  598.  
  599. Here's a script that tells the local player every used block id in the universe:
  600. <i>local blocks = { }
  601.  
  602. function table_contains(table, id)
  603. if #table == 0 then return false end
  604. for i = 1, #table do
  605. if table[i] == id then return true end
  606. end
  607. return false
  608. end
  609.  
  610. for l = 0, 1 do
  611. for x = 0, universe_width - 1 do
  612. for y = 0, universe_height - 1 do
  613. if not table_contains(blocks, block:id(l, x, y)) then
  614. table.insert(blocks, block:id(l, x, y))
  615. end
  616. end
  617. end
  618. end
  619.  
  620. local block = ''
  621.  
  622. for i = 1, #blocks do
  623. block = block.. tostring(blocks[i]).. ', '
  624. end
  625.  
  626. chat(block)</i>
  627.  
  628. Quite a long script, but returns exactly every used block id in the universe, non-repeating.
  629. * For lua beginners, <i>table.insert(table, object)</i> adds the specified value into the specified table.
  630. * You can also use <i>table.remove(table, index)</i> to remove the value at the specified index from the specified table.
  631.  
  632. (===) Digging further
  633. <size=18>Optimization</size>
  634. Optimizing your scripts is a huge deal, since it increases efficiency and performance.
  635.  
  636. This is a step that is optional, but if done can be very useful.
  637.  
  638. The script wrote to return every used block id in the universe isn't optimized, because it's constantly checking the same table for the id.
  639.  
  640. To fix this, it is possible to use string indexers, which helps a ton.
  641.  
  642. Here's the same script, but optimized:
  643. <i>local blocks = { }
  644.  
  645. for l = 0, 1 do
  646. for x = 0, universe_width - 1 do
  647. for y = 0, universe_height - 1 do
  648. if blocks[block:id(l, x, y)] == nil then
  649. blocks[block:id(l, x, y)] = block:id(l, x, y)
  650. end
  651. end
  652. end
  653. end
  654.  
  655. local block = ''
  656.  
  657. for i = 1, #blocks do
  658. block = block.. tostring(blocks[i]).. ', '
  659. end
  660.  
  661. chat(block)</i>
  662.  
  663. The result is a lot of less time to compile the same script.
  664. This is, of course, useful and good for the script.
  665.  
  666. Plus, the resulting string will now have the ids ordered, because we are now using indexes to store the ids.
  667.  
  668. Note how the script also reduced in size, because the function was no longer needed.
  669.  
  670. <size=18>Placing blocks</size>
  671. There is also another function in <i>block</i>. <i>place(layer, x, y, id)</i>.
  672.  
  673. This function is used to place blocks, as deducted from it's name.
  674. * Note that since scripts are clientsided, the blocks per second limitation still applies.
  675.  
  676. <size=18>Events</size>
  677. Events are functions that are called every time a specific event occurs.
  678.  
  679. To use functions, you must type their names in your script, so that they can be called.
  680.  
  681. Here's an example of a simple snake mechanic, but in Lua:
  682. <i>-- Green block is id 7 and red block is id 4
  683.  
  684. function onBlockPlaced(layer, x, y, id, placer)
  685. local newId = 0
  686. if id ~= 4 and id ~= 7 then return end
  687.  
  688. if id == 7 then newId = 4 end
  689.  
  690. block:place(layer, x, y, newId)
  691. wait(18 / 1000) -- 18ms delay
  692. end</i>
  693.  
  694. <size=18>More into events</size>
  695. There are also more events, and it will eventually come in handy to get the player's name and id.
  696.  
  697. This is, of course, possible.
  698.  
  699. Here's a sample welcome/goodbye script:
  700. <i>function onJoin(name, id)
  701. chat('Welcome, '.. name..'!')
  702. end
  703.  
  704. function onLeave(player)
  705. chat('Goodbye, '.. player.name..'!')
  706. end</i>
  707.  
  708. Now, what if your script required a list of every player in the game?
  709. Fortunately for you, that's also possible to obtain!
  710. And surprisingly enough, it is relatively easy to do so.
  711.  
  712. Here's an example script that returns the name of every player in the game and their id:
  713. <i>names = ''
  714. for i = 1, #players() do
  715. names = names.. players()[i].name.. ' ('.. tostring(players()[i].id).. '), '
  716. end
  717.  
  718. chat(names)</i>
  719.  
  720.  
  721. But what if you only have the player's name, and need their id?
  722. It is also possible to get the player's id with only their name, even without a list of players.
  723.  
  724. Here is the function that collects a player's id from their name:
  725. <i>getplayer(name).id</i>
  726.  
  727. Events are, infact, surprisingly easy once the basics are known.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement