Creating your own potions
So, you wanna make a pack of potions huh? Well let's clear up a few things first of all:
- I do NOT permit you to sell these. They must be free, preferably on Github or Garrysmods.org or something. (I don't mind if you make your own custom potions for your server, this only applies to people who wish to distribute them). If your restricting potions to certain VIP groups that require payment that's fine.
- If you do not own the addon, please, don't try. You won't be able to test them and they probably won't work.
- Two potions cannot share a recipe. To prevent conflictions, please, use your own chemicals as much as possible.
Okay, now that that's over with. Let's get into the juicy stuff.
The basic file structure for a new addon containing potions is like this:
📁 my_addon_folder
- 📁 lua
- 📁 wizardry
- 📁 items
- 📁 my_addon_name
- 📁 chemicals
- 📝 sh_chemical_one.lua
- 📝 sh_chemical_two.lua
- 📁 potions
- 📝 sh_potion_one.lua
- 📝 sh_potion_two.lua
Notice the sh_ in front of every file. This is how wizardry's autoloader works. It tells the addon to load it shared. If you don't do this, the addon won't load your potion.
Chemicals
To create a new chemical, go into your addon's chemicals folder and create a new file. Call it anything, as long as it has sh_ in front of it, although it's preferable to name this something special to your chemical. E.g sh_body_part.lua or something.
Here is a basic example of the code for a new Chemical from the base addon. Don't worry, I'll walk you through it.
First, we need to define a new Chemical object. This is done using Wizardry.Objects.NewChemical(id). The ID must be something that is unique to your addon and chemical, as this is how the addon is able to differentiate between two different chemicals. If your ID is not unique and clashes with another chemical, the addon will refuse to load your chemical.
So, let's create our chemical object.
Now, we need to define it's information. Chemicals have a Name, Description, and a Price. We can define these using the representative functions on the chemical object we just created.
Now, we simply need to call CHEMICAL:Define(). This function checks everything is valid in the chemical, and if everything is in order, register it.
Assuming everything is good, your chemical should now be registered successfully by the addon on server startup. :)
Here's the example in full:
Potions
Potions, while harder, are still quite easy. For this example, we're going to make a potion that will decrease the player's walk speed. Quite pointless, but I'm only using this as an example.
Here is a basic example of a Potion from the base addon itself:
First thing we need to do when we create a new potion, is like the chemicals define a new Potion Object. Once again, we need to give it a unique ID so the addon can tell the difference between two potions.
And, like the chemicals, we need to set it's data. We need to set it's name, description, the effect time (the time the potion is effective), the color and how much you need to stir it in the cauldron.
Now, we need to define some effects and a recipe for it. Unfortunately, due to how Lua's crap implementation of OOP works, we need to define these tables manually instead of using fancy functions. The effects table is purely visual, and simply tells the player what happens to them when they drink the potion. The recipe is what the wizard places into the cauldron to make the potion. The recipe is made up of Chemicals, which are specified using their IDs. We'll use the Body Parts chemical we defined earlier.
Now that that's done, we need to let our potion know what to do when a player drinks it. This is done using two functions, on each side. DrinkSV(player), DesistSV(player) for serverside, and the twin DrinkCL() and DesistCL() for clientside stuff. All the drink functions are for applying affects, and is called when the player initially drinks the potion. The desist is for resetting the player back to normal, when the effect wares off or they die.
Remember: Never trust the client. Only use Clientside for visuals.
So, now that we know what functions to call, we can use them to slow our player.
Now we've done that, we need to call POTION:Define() to let the addon check that our potion is good to go, and to add it to the game.
And, we should be done. If you load up the game, you should be able to buy bodyparts from the Chemical Engineer. Lob two of them into the Cauldron and you should get a potion that slows you down. That was easy, huh?
Full code for that Potion Example:
Where can I upload my l33t potion pack now?
Anywhere, as long as it's free. :)
If you do upload it, shoot over a DM or support ticket with the link. I'm curious to see what you guys are making. I'll also add you to a list of people who've made potions for the addon on the addon page, so thats another bonus.
If you need help creating a potion, join my Discord Server. There is plenty of helpful people there who will help you out, including me!
Welp, I'm off to go and cry at how long this took to write. Bye!