×
Create a new article
Write your page title here:
We currently have 277 articles on Waste Of Space Wiki. Type your article name above or create one of the articles listed here!



    Waste Of Space Wiki

    Programming: Difference between revisions

    Content deleted Content added
    m Nvm found the new link (Undo revision 4048)
    Added 2 more tutorials, including events and modems. Please polish/revise since i don't have good grammar
    Line 74: Line 74:
    ==Small Tutorials==
    ==Small Tutorials==
    These tutorials are assuming you know a thing or two about coding already. More are yet to come.
    These tutorials are assuming you know a thing or two about coding already. More are yet to come.
    ===Events===
    One of the most important parts about programming in WoS is events. Events allow you to get player input, detect when something happened, and more. For example, if you are using the [[Microphone]], in order to get when a player has talked, you need to use the chatted event. A full list of events can be found [https://github.com/iimurpyh/pilot-lua/wiki here]. To get an event, you need to first do the connect method on the part, like this; microphone:Connect(). Within the parenthesis, you need to put two parameters. The first one is simple, just put the name of the event within a string (example: "Chatted"). As the second parameter, we put the function we want it to run when the event happens. This is known as a [https://en.wikipedia.org/wiki/Callback_(computer_programming) callback]. Basically, its when you pass a function as a parameter to another function. In this example, we want the :Connect() method to run a function whenever the "Chatted" event happens. In that second parameter, we can reference an already existing function, or make a function within the parameter. Here is our code so far:
    <syntaxhighlight lang="lua" line="1">
    local microphone = GetPartFromPort(1, "Microphone") --this gets the microphone object
    microphone:Connect("Chatted", function() --so we call the connect method, then we tell it to run the function whenever it detects the event
    print("Someone talked!!!")
    end)--we need to end the function, and close the open parenthesis
    </syntaxhighlight>
    Now, this isn't very useful on its own. We have no way of telling what the person even said! All we know is when someone talked. However, we are in luck. When the connect method calls the function, it also passes the parameters along with the function (once again, all the events and what they return can be found [https://github.com/iimurpyh/pilot-lua/wiki here]). In the case of the chatted event, it returns the players name, and the message they sent. Here is the example code:
    <syntaxhighlight lang="lua" line="1">
    local microphone = GetPartFromPort(1, "Microphone") --this gets the microphone object
    microphone:Connect("Chatted", function(player, message) --so we call the connect method, then we tell it to run the function whenever it detects the event
    print(player.. " Said: ".. message) --print what was said and who said it (eg. Playername Said: Hello!)
    end)--we need to end the function, and close the open parenthesis
    </syntaxhighlight>
    ===Screen===
    ===Screen===
    The [[Screen]] is a confusing part for beginner programmers, because of all the confusing stuff like UDim2. So, here's an explanation. [[Screen|Screens]] are a very useful item for displaying information, allowing for much more possibilities then just simply configuring [[Sign|signs]]. While [[Sign|signs]] will work for displaying text, it doesn't have nearly as much possibility as [[Screen|screens]]. [[Screen|Screens]] can display multiple elements, and have custom size/position. However, when it comes to creating [[Screen]] elements, it can be confusing because it uses a whole new data type called [https://create.roblox.com/docs/reference/engine/datatypes/UDim2 UDim2]. UDim2 is a data type consisting of 4 values. The order goes XScale, XOffset, YScale YOffset,. ''What the heck does all that mean??'' Well, simply put, Offset is the X or Y size/position of the element in pixels. Scale is the X or Y size/position in screen sizes. ''What do you mean, "screen sizes"???'' 1 scale is equal to the entire size of the screen. 1 XScale is equal to the width of the screen, while 1 YScale is equal to the height of the screen. Scale is very important, as it allows for the ScreenElement to look the same no matter the size of it. In order to make a screen element cover the entire screen, you would put the scale at 1 for both. So what you can do is make the screen size equal to UDim2.new(1, 0, 1, 0). ''Whats the point of putting the zeros? Is there an easier way to do it?'' There is an easier way to do it. instead of using UDim2.new, you can use UDim2.fromScale, which is where you only need to put 2 values instead of 4, UDim2.fromScale(1, 1). Before I continue, if you need further help understanding UDim2, go visit the [https://create.roblox.com/docs/reference/engine/datatypes/UDim2 Creator documentation page]. Now comes the part where I actually explain how to make a new element. So first to create the element, you need to get the screen from the port. <syntaxhighlight lang="lua" line="1">
    The [[Screen]] is a confusing part for beginner programmers, because of all the confusing stuff like UDim2. So, here's an explanation. [[Screen|Screens]] are a very useful item for displaying information, allowing for much more possibilities then just simply configuring [[Sign|signs]]. While [[Sign|signs]] will work for displaying text, it doesn't have nearly as much possibility as [[Screen|screens]]. [[Screen|Screens]] can display multiple elements, and have custom size/position. However, when it comes to creating [[Screen]] elements, it can be confusing because it uses a whole new data type called [https://create.roblox.com/docs/reference/engine/datatypes/UDim2 UDim2]. UDim2 is a data type consisting of 4 values. The order goes XScale, XOffset, YScale YOffset,. ''What the heck does all that mean??'' Well, simply put, Offset is the X or Y size/position of the element in pixels. Scale is the X or Y size/position in screen sizes. ''What do you mean, "screen sizes"???'' 1 scale is equal to the entire size of the screen. 1 XScale is equal to the width of the screen, while 1 YScale is equal to the height of the screen. Scale is very important, as it allows for the ScreenElement to look the same no matter the size of it. In order to make a screen element cover the entire screen, you would put the scale at 1 for both. So what you can do is make the screen size equal to UDim2.new(1, 0, 1, 0). ''Whats the point of putting the zeros? Is there an easier way to do it?'' There is an easier way to do it. instead of using UDim2.new, you can use UDim2.fromScale, which is where you only need to put 2 values instead of 4, UDim2.fromScale(1, 1). Before I continue, if you need further help understanding UDim2, go visit the [https://create.roblox.com/docs/reference/engine/datatypes/UDim2 Creator documentation page]. Now comes the part where I actually explain how to make a new element. So first to create the element, you need to get the screen from the port. <syntaxhighlight lang="lua" line="1">
    Line 118: Line 133:
    *Position is the position of the element in the top left corner
    *Position is the position of the element in the top left corner
    *when doing scale for position, the top is y=0, the bottom is y=1, the left is x=0, the right is x=1. This confuses some people because positive y is actually down, not up.
    *when doing scale for position, the top is y=0, the bottom is y=1, the left is x=0, the right is x=1. This confuses some people because positive y is actually down, not up.
    ===Modem===

    <!--If someone knows anything about the post and get requests stuff, PLEASE put it down below-->
    The [[Modem]] is an extremely useful object in WoS programming. It allows you to send messages across the entire universe! You could even create a makeshift internet of sorts with it! This being said, it seems like a scary and confusing prospect for new programmers. But the reality is, that it is very simple. There are two main things you need to know to make a messaging system or something like that. First is the SendMessage method. This is very simple. It sends a message across the universe. All you need to put in the method's parameters is the message you with to send, and the [[Modem]] ID you wish to send it on. Here is an example:
    <syntaxhighlight lang="lua" line="1">
    local modem = GetPartFromPort(1, "Modem")--gets the modem object
    modem:SendMessage("Hello WoS", 1) --sends the message "Hello WoS" on the Modem
    </syntaxhighlight>
    ''Well you can send messages, but how do you receive?''
    You receive messages using an event. If you are unfamiliar with events, please read the tutorial above about events. The event name is MessageSent. It fires when a message is received from a modem with the same ID. It only provides a singular parameter, and that is the data that was sent. Here is an example of a modem that picks up messages and writes it to a sign.
    <syntaxhighlight lang="lua" line="1">
    local modem = GetPartFromPort(1, "Modem")--gets the modem object
    local sign = GetPartFromPort(2, "Sign")--gets the sign object
    modem:Connect("MessageSent", function(message)
    sign:Configure({SignText=message})--write the message to a sign
    end)
    </syntaxhighlight>
    There you go! You have just made a simple messaging system. You can expand on this by using [[Keyboard|Keyboards]] to send messages, or use screens to make a nice looking UI.
    == Resources ==
    == Resources ==


    Cookies help us deliver our services. By using our services, you agree to our use of cookies.

    Recent changes

  • Jo857294 • 8 days ago
  • 116.111.185.163 • 21 days ago
  • 116.111.185.163 • 21 days ago
  • 116.111.185.163 • 21 days ago
  • Cookies help us deliver our services. By using our services, you agree to our use of cookies.