Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

Sign Up Now!
  • There is an ongoing Game Jam event! Learn more here

Functions Guide #2

Sean

Active member
Joined
Sep 12, 2020
Messages
51
Functions are a key part of diamondfire and programming in general. A function is a way to group code down into a single block.

Creating a Function:
To create a function, get the Function Codeblock (Lapis Block), and place it down exactly how you would place an event.

Once your function is placed, grab a Text and set it to whatever you want to name your function, it could be anything.
Once you have your text complete, right click the Function's sign while holding your text item, this will set the function's name.
(I won't go over what a good function name is, but generally use camelCase with no spaces)

Once you have a named function, you can place whatever code you'd like on the function.

Calling A Function:
To call a function, get the Call Function Codeblock (Lapis Ore), and place it down like you would place down an action.
After placing a call function, right click its sign which will bring up a list of all functions you've created.
Locate the function you want to use and click it.
When a Call Function is reached, DiamondFire will run all the code you placed on the matching Function Codeblock, and then continue reading code after the Call Function.


Example:
Code:
[Player Event: Join]
sendMessage("Hello,")
callFunction("myFunction")
sendMessage("World!")


[Function: myFunction]
sendMessage("Hello from function!")
Output:

Code:
Hello,
Hello from function!
World!

Notes:
You can also use a Text to set the name of a Call Function. Which will allow you to use text codes like %default, or, more usefully, %var().

If you place an item inside of a
Function, the icon of the function will change. (The icon will also show the lore of the item you input, which is handy if you want to write what a function does in its icon.)

Functions do not accept parameters, and cannot return values. Although recreating these is not hard.

Local Variables from the code that called the function can be accessed inside the function, and Local Variables created or modified inside the function will be accessible from the code that called the function.

If you're calling the same function repeatedly all over your code, you may get lagslayered. You can mitigate this by placing a wait(0) inside the function.

You can exit out of a function prematurely if you use Control: Return.

If you run out of space on a single code line, you can use functions to "extend" the code line.

Function names are not capped at 16. Even if the name doesn't fit on the sign, it will still work.
 
Last edited:

stinkey

Member
Joined
Sep 8, 2020
Messages
28
another thing about functions: you can use %var in call functions, so say you set variable helloText to '1'. then you can name a function hello1, and you can put a call function with "hello%var(helloText)". this way you can remove a lot of if vars if you want to sort something with functions
 

Sean

Active member
Joined
Sep 12, 2020
Messages
51
another thing about functions: you can use %var in call functions, so say you set variable helloText to '1'. then you can name a function hello1, and you can put a call function with "hello%var(helloText)". this way you can remove a lot of if vars if you want to sort something with functions
I mentioned that
 
Top Bottom