Have you ever been in this situation? You've got a long line of IFs that decides what to do based on a certain value- like the type of item you're holding, or the material of a block you just clicked or paid respects to.
This gets really cumbersome really fast. (If it's not cumbersome for you yet, don't worry too much about following the guide.)
But here's a fun fact: you can use
Here, the
This looks more cumbersome at first, but there are plenty of benefits:
This gets really cumbersome really fast. (If it's not cumbersome for you yet, don't worry too much about following the guide.)
But here's a fun fact: you can use
%var
in function calls. This means you can get a text value, like the type of an item or its item tag, and use that to decide which function to run.Here, the
RightClick
event gets the type of an item, stores it in the type
variable and calls the function RCWith.%var(type)
. Depending on whether you're holding a feather, a stick or paper, either RCWith.feather
, RCWith.stick
or RCWith.paper
will be called and perform the appropriate action with no conditional in sight. (There's nothing special about the syntax here, it's just a useful naming scheme.)This looks more cumbersome at first, but there are plenty of benefits:
- To add a new item, just add a new function! If I want to add a gun I could define a function
RCWith.iron_hoe
that checks and sets a cooldown and fires bullets. I don't have to touch theRightClick
code at all. - If your items have multiple actions, like a gun that can reload or a magic wand with multiple spells, you can do the same trick with left-click, pressing F and so on, giving you multiple functions for each item and functions like
LCWith.feather
,LCWith.stick
,LCWith.paper
and so on; I might add aLCWith.iron_hoe
function that reloads the gun. You can group the functions together by item, so if you want to change how an item works it's much quicker to find all its functions. - It's just less messy. Your code for handling events can be separated from the features of your game, and this gets more important as you add more things and ways things can behave to your game.
- Item tags; a trick I use in basically every plot of mine with more than one kind of item is to give each item a
type
tag and useRCWith.%var(type)
,FWith.%var(type)
etc. to define what each item does. - Block types with custom interactions. If your right-click event gets the type of the event block and calls
RC.%var(block)
, theRC.crafting_table
function could open a custom crafting menu,RC.anvil
a forging system, and so on. - It's not restricted to item or block names; for example, it could be the different ways you move in a platformer, like
Standing
,Falling
,Diving
etc. You can bind the same action, such as sneaking, to different abilities depending on how you're currently moving. - UIs- when I create plot menus I give items
type
tags likeclose
,craft
, etc. to perform different actions when you click them in the menu. To add new menu items and menus you can define the functions for each item, and then just show the menu when you want.
%var
in functions is a valuable feature given how hidden it is. I think it would be neat if more people knew about it and its potential!
Last edited: