Here some examples script and trick that you can use in your codes.
IMPORTANT: For the sake or making this page short and sweet, i won't add all 4 global function to each piece of codes. But you must do it for your mod!
Open your notepad++ and write those code in it. Save it in your Lua folder as shown in the creating your first mod section of this site and run the game!
Note: Many of this examples use the chatlog to display informations. If you don't know how to enable it, read the basic knowledge section. All function/variables starting with Shroud is referenced in the API References page of this site, make sure to have it on the side for a quick reference!
Now let's go straight to the point!
Hello World!We start by using a ShroudOnStart() function which will be triggered only once as soon as the game is started.
function ShroudOnStart() ShroudConsoleLog("Hello World!"); end
Using variablesA variable is a place in the computer memory holding a value. Here an example on how to declare and use a variable, always using ShroudOnStart().
Note: If you are already in game and want to see the result of this, you will need to do /lua reload in order for the ShroudOnStart() function to work.
local myVariableName = "This is a test!" function ShroudOnStart() ShroudConsoleLog(myVariableName) end
Using the main game loop ShroudOnUpdateShroudOnUpdate run once every frame A period of time, especially a specified period in which something occurs or is planned to take place. which is about ~60 times a second. You can guess it can be spammy Any excessive quantity of communication in a medium that isn't appropriate for it. For example, sending hundreds of lines of nonsense in a chat-room. if you wrote a hello world in this, it will show up 60 times each second in the chat. To avoid this, we must put some logic to hold up the main code. We could use a boolean A binary variable, having two possible values called “true” and “false.”. or a time function for example. This one use os.time() function to get current time and a modulus This is an operation dividing A by B, and keeping the rest.
Example 1: 11 % 5 = 1
Example 2: 23 % 5 = 3 operator % to get a result of 0 or 1. If the time is 0, then the code inside will execute.
function ShroudOnGUI() if (os.time() % 2 < 1) then ShroudConsoleLog("Hello world again!"); end end
Checking if a key has been pressed (once a second!)Now we are declaring 2 local variables Global variables are declared outside any function, and they can be accessed (used) on any function or other scripts of the program. Local variables are declared inside a function or script, and can be used only inside that context. for our time logic. If the current time is greater than the old time saved in the variable plus the update rate delay is true, we execute the main code which set keyPressed to false. Then immediately after, we have another piece of code that check if keypressed is true, if it is, we run the code inside it. Note that the local variable are declared outside of the ShroudOnUpdate function because if it was inside, the value of oldtime would reset 60s time a second, which mean key could be pressed more than once each second.
local keyPressed = false; local _UpdateRate = 1 local _oldTimer = os.time() function ShroudOnUpdate() if os.time() > _oldTimer + _UpdateRate then _oldTimer = os.time() keyPressed = false end if keyPressed == false then if ShroudGetKeyDown("Space") then ConsoleLog("up pressed") keyPressed = true end end end
To be continued...