• Hey! Register here to create your account, engage with the community, and talk about what is new!

You can put %var in CALL FUNCTION blocks

rdococ

New member
Joined
Jan 13, 2023
Messages
17
Reaction score
7
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.

1673791725765.png

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.

1673792366258.png

1673792450248.png

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 the RightClick 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 a LCWith.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.
You can do this trick with just about any text value, not just item names. Here are some more ways to do it:
  • 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 use RCWith.%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), the RC.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 like close, 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:

ARMcPro

Forum adept
Overlord
Joined
Oct 20, 2020
Messages
1,226
Reaction score
347
I recommend you moving it to tutorials so we can upvote it 🥰
 

pixlii

Forum adept
Overlord
Joined
May 20, 2021
Messages
215
Reaction score
34
i use this all the time, whether it's custom abilities for weapons or enchantments, it's really helpful for gamedev
 

superbaconbro

Forum adept
Overlord
Joined
Jul 14, 2021
Messages
161
Reaction score
23
me omw to change every single item and function name in battle box :troll:
 
Top Bottom