Adding an OnClick-event to a Notification¶
This guide is authored by Voxel Tycoon community member Nebruzias
Introduction¶
This tutorial will guide you through the process of triggering an action whenever you click on a Notification.
After finishing this tutorial, you will know how to:
- Create a class with the INotificationAction Interface
- Add an action to your Notifications
Note
This guide continues on Creating your first script mod. If you dont know how to setup the environment or how to get a mod in game, please follow this guide first.
This guide is based on Visual Studio 2019 Community Edition
Main Class¶
Start a new project and call it "NotificationActionExample".
An unwritten rule in programming is that you give your variables, classes, methods, etc, clear names. They should be named in such a way so that you know what their function is when you read their name.
As you might have noticed, when we created this project, the class inside the project is named "Class1" by default.
Obviously this is not a very clear name. Lets start with renaming Class1 and give it the name "Main". The reason for this name is that usually the first class in your project will be your main class which manages and executes everything else. Of course you are free to call it whatever, it is not required to be named "Main" for the mod to work, just make sure the name represents the function of this class.
To rename the class, right click on Class1 in your Solution Explorer and select Rename.
Then type in "Main" and press enter.
Note
After you have pressed enter, you are asked if you want to perform a rename for all references, select Yes.
Now we need to get access to callbacks which allows us to execute code at a certain point in the games lifecycle. To get access to these callbacks we need to inherit from the Mod
-class.
Where it says public class Main
, change this to public class Main : Mod
.
The Mod
-class gives us access to a couple of callbacks. For this example we are interested in the OnGameStarted()
-callback.
Inside your Main
-class, add:
Now we can set up our notification inside OnGameStarted()
, just like in the "Creating your first script mod" guide.
Lets add the following code to OnGameStarted()
:
Note
Instead of using Color
, string
, etc, you can also use the var
type. Personally Im not a fan of var
, but you are free to use it if you prefer using var
instead.
Unfortunately using Color
instead of var
can cause a small issue in this example. When we added the variable type Color
, the namespace System.Drawing
was automatically added, since Color
is part of this namespace. While Company.Current.Color
does return a value of type Color
, it is not part of the System.Drawing
namespace, instead it belongs to the UnityEngine
namespace. It is throwing an error because the value Company.Current.Color
returns does not fit in System.Drawing.Color
.
There are 2 ways to fix this problem:
- Replace
using System.Drawing;
withusing UnityEngine;
- If you can't replace/remove the
System.Drawing
namespace (because you might need it for something else), declare the color-variable like:UnityEngine.Color color = Company.Current.Color;
NotificationManager.Current.Push()
requires 6 arguments:
- Priority (Optional)
- Color (Optional)
- Title
- Message
- Action
- Icon
We have set variables for all the arguments, except Action. Before we can create and set a variable for this argument, we have to create a new Class
.
NotificationAction Class¶
Lets create a new Class
.
Right click on your project in your Solution Explorer, then go to Add > New Item...
Then in the next window select Class and give it the name NotificationAction.cs and click Add.
Once you have added this new class, follow the steps below:
- Implement the
INotificationAction
-interface.
After you implemented the interface, your NotificationAction
-class should now look like this:
- (Optional) Remove the
throw new NotImplementedException();
from all 3 callbacks, to prevent any exceptions showing up in game. -
Lets add some code to the
Act()
-callback. This is the callback that will be triggered when you click on the linked notification. As an action we are going to show another notification that tells us that we succesfully triggered a notification action.Lets set up a new notification:
Note
The Color32
-type accepts the full RGBA values (0 - 255), while the Color
-type only accepts values from 0 to 1. When you have a RGB value you want to use, it is recommended to use the Color32
-type, else you have to convert your RGB values to a value between 0 and 1. This is not difficult to do, but it requires a bit of extra work which can be avoided.
Then we need to call the Notification Manager to actually display this notification. We do this with adding the following statement:
NotificationManager.Current.Push(priority, color, title, message, null, icon);
Note
INotificationAction action
is a required argument while calling NotificationManager
. Since we dont have an action we want to happen when this notification is clicked, we just pass null
for this argument.
As a final result your NotificationAction
-class should now look like this:
Now that we have a class which can go in the INotificationAction action
-variable, we can implement it.
Implement the Notification Action¶
Add the following line to your OnGameStarted()
-callback in your Main
-Class:
INotificationAction action = new NotificationAction();
Your Main
-class should now look like this:
Now we have all the required arguments to call the Notification Manager.
Add the following statement to your OnGameStarted
-callback in your Main
-Class:
NotificationManager.Current.Push(priority, color, title, message, action, icon);
As a final result your Main
-class should look like this:
Compile and run the mod¶
Now we can add this mod to the game and try it out.
Tip
To read in detail how to do this, check Run the mod section of the Creating your first script mod guide.
Follow the steps below:
- Build your project into a .dll-file
- Locate your .dll-file and place it in
Content/<your_mod_folder>
- Create a mod.json file
- Start Voxel Tycoon and start a new game with your just created mod activated
- Wait for your game to load...
- CLICK THAT NOTIFICATION!!!
Play around¶
If you're not yet very familiar with coding, I suggest to play around some more to see what other awesome things you can do with Notification Actions. This is a good way to get better with coding.