User:JulienDethurens/Guide/Chapter 4: Difference between revisions
m Text replacement - "</code>" to "</SyntaxHighlight>" |
m Text replacement - "<SyntaxHighlight code="lua">" to "<syntaxhighlight lang="lua">" |
||
Line 20: | Line 20: | ||
And here is the same code, without indentation: | And here is the same code, without indentation: | ||
< | <syntaxhighlight lang="lua"> | ||
for i = 1, 50 do | for i = 1, 50 do | ||
if math.random(5) == 3 then | if math.random(5) == 3 then |
Revision as of 03:55, 27 April 2023
This page talks about some subjects that could be useful to know about. You can read these at any time, there is no specific moment to read them, so you don't need to read them completely at the end. However, some of these assume you have some prerequisite knowledge, so you might only be able to understand some of these after reading a certain part of the guide.
Indentation
Indenting code means using tabs (or spaces, but I prefer tabs) to indent code, to make it more organized.
Usually, a new level of indentation is added on every new block of code, or every scope, if you prefer. Some also do it for tables and in long comments.
Here is an example of indented code:
for i = 1, 50 do
if math.random(5) == 3 then
print(string.format("The number is %d.", i))
elseif math.random(15) == math.random(10) then
print(math.random(5) + i)
break
end
end
And here is the same code, without indentation:
for i = 1, 50 do
if math.random(5) == 3 then
print(string.format("The number is %d.", i))
elseif math.random(15) == math.random(10) then
print(math.random(5) + i)
break
end
end
Indented code looks much better than non-indented code. Indenting it will make it much more organized and will be extremly helpful later on, when you'll want to edit your code and understand it faster. Trust me, you should always indent code, even if you think it's not worth it.
ReflectionMetadata.xml
In your ROBLOX folder, there is a file called ReflectionMetadata.xml. That file controls what objects are shown in the explorer, what is their icon, what order they are shown in, as well as what objects are shown in the object browser, whether they are deprecated, their summary, etc.
The thing is, not all objects are shown in the explorer. I'm sure you've already used the InsertService, or perhaps heard about the CoreGui, or any other service like that. Yet, these don't show in the explorer, unlike the Workspace, the StarterGui, and the other basic services. And not only services, there are many other hidden objects that you do not see normally in the explorer.
You could manually edit your ReflectionMetadata.xml file to show these files. Or, you could just use a premade one. Well, you're lucky, because there's an user, Anaminus, that made his own edited ReflectionMetadata.xml file and he publishes it on an alt, he updates it often enough. Here is the alt: ReflectionMetadata.
The latest model should be the one you want. Just take it and replace your ReflectionMetadata.xml file by its contents. You can find instructions in the model.
Now, I could explain how to edit the file, but it's kind of simple, anyways, so you can figure it yourself just by looking at it. I recommend using Anaminus's ReflectionMetadata, as it allows you to see every service.
Now, you might wonder why I am talking about this even though this is a scripting tutorial. Well, in fact, when you make them show in the explorer, they also show in the object browser. What that means is that you will then see them in the object browser and you'll be able to see their properties, methods and everything, which could be helpful if you want to know about a certain property of this or that object without looking at the wiki.
Coercion
Quote from Wikipedia:
In computer science, type conversion, typecasting, and coercion are different ways of, implicitly or explicitly, changing an entity of one data type into another. This is done to take advantage of certain features of type hierarchies or type representations.
Basically, coercion is the action of converting a value of a certain type to another type.
The Lua 5.1 Reference Manual says this about coercion:
Lua provides automatic conversion between string and number values at run time. Any arithmetic operation applied to a string tries to convert this string to a number, following the usual conversion rules. Conversely, whenever a number is used where a string is expected, the number is converted to a string, in a reasonable format.
What that means is that you can use strings in arithmetic operations and numbers in string manipulation.
Examples
Here are some examples of coercion:
Example
This example does arithmetic operations on strings and numbers. Strings are automatically coerced to numbers, to allow for arithmetics to be performed on them.
As you can see, the strings were converted to numbers and then the expression was evaluated to be equal to 8.15. Note: incase you don't know what 1e2 is, just know that it is equal to 100.
Example
This example demonstrates how numbers are automatically coerced to enums when an enum is expected.
local part = Instance.new('Part', Workspace)
part.Material = 1280
This will make the part's material become grass, because the Grass material corresponds to the number 1280. You can know what number corresponds to what enum by looking at the object browser.