Hello. I have recently been playing around with the SC2 editor and figured out how to make buffs which are only active during certain times of the "day" and I decided I should write a tutorial on it.
In this tutorial we'll create a new unit called the "Night Templar" which cloaks only during the night. We'll go over creating Validators, Behaviors and new Units.
We'll begin by creating the necessary validators. Validators allow behaviors to check for certain conditions before affecting a unit. In this way we will disable or enable cloaking based on the "Time of Day" in the game.
Fire up the SC2 editor and open the Data Editor. Change the Data Type to Validators and then Add a New Object. Name this object IsBeforeSunrise, click Suggest and change the Validator Type to Time of Day. Click OK.
With this newly created validator selected, change the field (None) - Compare to "Less Then Or Equal To" (Note the spelling error, should be 'than' just for your information). Now set the field (None) Value to 06:00:00. You can set this value to any time you'd like to have as the sunrise. I'm going to make night be from 6 PM to 6AM in this tutorial.
Now Add another new Validator. Set the name to IsAfterSunset, click Suggest and change the Validator Type to Time of Day and click OK.
With this validator selected, change the field (None) - Compare to "Greater Than or Equal To" and change the field (None) - Value to 18:00:00. Again, you may set this to whatever time you'd like to have the sunset.
So what we've done is created two new validators that will help us determine whether or not it is day or night. The Compare field describes the type of test we will be performing. What these validators will do is take the current game's Time of Day and compare it with the value we specified. So, if it is 01:00:00 then it will be IsBeforeSunrise but it will not be IsAfterSunset. Now, we know it is night if either one of these validators are true. So, we'll make another validator to "Combine" these two together.
Add another new Validator. Set the name to IsNight, click Suggest and set the Validator Type to Combine. Click OK.
With this new validator selected, Click on the (None) - Combines field. A menu should pop up, Click the Green plus button to add a validator to the list. Select IsAfterSunset and click OK. Now, Click the Green Plus Button again and Select IsBeforeSunrise and Click OK. You should now have both Time of Day validators in your list. Click OK to close the menu.
Your IsNight validator is now complete. Notice that, by default, the editor sets the field (None) - Type to "Or". This is a logical OR operator meaning if any of the validators in the (None) - Combines field are true, then this validator is also true. So, if it is after sunset OR before sunrise then we know it is night. If you want a validator for daytime, use the (None) Negate field and enable it to "flip" the value of the combination. If we were to enable the Negate field then this validator would be true if it is NOT after Sunset or before Sunrise, giving us Daytime.
Ok, so we now have a way to test if it is nighttime, but how do we use this for our soon-to-be buff? First, it's time we create our buff.
In the Data Editor, set the Data Type to Behaviors. Behaviors are elements which can modify units on the fly. Buffs fall under the heading of Behaviors. Add a new Object and set the name to Night Cloak, click Suggest. Change the Behavior Type to Buff, click OK
With "Night Cloak" selected, we'll need to set some of its fields. First, change the Alignment to Positive meaning this behavior benefits the unit. If you wish, you may change the Icon field to some icon that you'd like to represent this buff. This Icon is the small one that will show on the unit's information panel at the bottom of the screen Now, the important field we need to look at for this, and basically all buffs is the "Modification +" field. This field allows us to actually set the way this buff will affect a unit. Double Click Modification + to bring up a new menu. At the top of the new menu, under the "Modify Flags" heading Click the little red X to delete ALL entries in this list. We do this to ensure this buff will not override the effects of another buff by messing with these flags. Then, under the State Flags list remove all BUT the CLOAK entry by clicking the little red X. Again, we don't want to affect any state besides the cloak state of the unit. Highlight the Cloak entry in the list and Check the box marked "Enabled" just below. Click OK.
As you can see, the Modification + panel is where all of the dirty work for Buffs is handled. If you explored more, you noticed plenty of other options of things to affect such as move speed, damage, health etc. Many possibilities for Buffs. But for now, our simple buff will make the unit Cloaked.
So, at the moment, our Night Cloak buff will ALWAYS cloak an object no matter what the time is. To fix this, we'll utilize our IsNight validator. If you look near the bottom of the Night Cloak fields you'll see a field called "Validators (Disable)". Click the Validators (Disable) field to open up a list. Click the little green X and add the IsNight Validator. Click OK.
This makes our buff disable anytime the IsNight validator fails. So, if it's daytime, the buff will be disabled. Notice there is also a Validator (Remove) field. This does just as you'd expect and removes the buff from the unit if the validator fails.
Finally, to make our buff more user friendly Change the Tooltip Field and set enter a message that will appear as the tooltip when the player hovers his mouse over the buff icon. I wrote "Cloaks the unit at night". The final fields of the Buff should be like the following:
So we have our Buff ready to go and it uses our new IsNight validator. It's time now to create a unit that has Night Cloak as a buff.
In the Data Editor, Change the Data Type to Units. Find the Dark Templar Unit. Right click it and Duplicate the Dark Templar. In the tree that pops up, all we need to copy are the Actors. So, Check the Actors Folder to select everything inside of it. Click OK. Note we are not copying everything else because we are not modifying anything besides some unit flags. We have to copy the actor because we need an Actor object to reference this unit so that it will be attached to a visual representation of the Dark Templar. Actor can only be associated with a single Unit.
First things first, we certainly don't want this unit to be called "Dark Templar Copy". So Right Click the Dark Templar Copy on the left panel and click "Modify Object...". Change the name to "Night Templar" then click Suggest and Click OK.
With Night Templar selected, we need to set some important fields. First, since the Night Templar has the same settings as the Dark Templar at the moment, it will be cloaked all the time automatically. To disable this we need to disable the Cloaked flag. Double click the Stats - Flags field and uncheck the Cloaked box. Now the unit will not be cloaked all the time.
It's now time to add our Night Cloak buff to the Night Templar. Click the (None) Behaviors + field. A window should pop up, Click the green X and add the Night Cloak behavior After it is added Click OK.
Now our unit has the Night Cloak buff! The buff will handle enabling or disabling the Night Templar's cloaking based on the Time of Day.
Speeding Up the Time of Day
With our Night Templar created, we probably would like to see our new Night Cloak buff in action. To do this, we need to setup a trigger to speed up the time of day progression a bit so that we can see the Cloaking enable and disable based on the time.
Open the Trigger Editor and select the Map Initialization trigger. Delete all of the actions for the trigger. Now, with the Map Initialization trigger still selected Click the Add Action button and a new menu should pop up. With the "Environment" heading selected on the left select "Set Day Length" and click OK. We've now added an action that will set the length of the game day.
With the Set Day Length action selected, click the linked 300.0 and change it to 30.0. This will make a "day" 30 seconds long.
Use the editor to place a Night Templar on the map for Player 1, and test out the map. You should see the unit cloak and decloak as time goes on!
Adding a Clock (Optional)
Having this functionality of time based buffs is pretty neat, but it invites the question, how do we tell the player what time it is in the game? Luckily this isn't too difficult to setup using some trigger play. This part of the tutorial assumes you have used triggers before and are comfortable moving around the editor.
Building the Dialog
Our clock will use SC2's Dialog system to show a little UI clock at the top of the screen. Add a new global variable of type Dialog Item name ClockDisplay. Leave the default value as it is.
One issue we need to address is conversion of the Time of Day variable type to a string for displaying purposes. Create a new Function called "TimeOfDayToString and set it as follows:
Now, select the Map Initialization trigger and add a Create Dialog action. Change it to "relative to Top of screen" and leave the other settings. Add a Show/Hide Dialog action. Change the action to Show instead of Hide. Add a Create Dialog Item (Label) action. Change the Text parameter to take a conversion from String to Text, and then change the string to be TimeOfDayToString function we added, and then pass in the function Get Current Time of Day function to that. The resulting action should be as follows:
Add a Set Variable action and change the variable to ClockDisplay and the value to Last Created Dialog Item. We now have a reference to this label.
Add a Show/Hide Dialog Item action and set it to Show.
Add a new Trigger and add an event Timer -> Periodic Event and set the period to 1.0 second. In the actions for this trigger add a Set Dialog Item Text action. Change the first parameter to ClockDisplay and change the Text parameter to the string-to-text, TimeOfDayToString, Current Time of Day setup that we used in the Label creation action. Now the label will be updated every second with the new time.
You should now have an ingame clock that the player can use to figure out when buffs will be activated.
This really seems a lot cleaner (at least compared to Blizzard) way to do night / day stuff.
My only problem is that I cannot get the last function and trigger to work :( I copied both and pasted them here to see what was wrong with them (and Bolded what I thought was wrong). I cannot seem to get the function to actually return what I want. Then, because of this, I can't choose the function in the trigger (either the MapInit one or the following renew).
Within the function I have "Use Default Grammar Text" checked... but it doesn't seem to know it is returning the string.
Time Of Day To String
Return Type: String
ToD = 00:00:00 <Time Of Day>
Grammar Text: Time Of Day To String(ToD)
Hint Text: Converts a time of day value to a String.
Custom Script Code
General - Return Value
Dialog - Create a Modal dialog of size (500, 400) at (0, 0) relative to Top of screen
Dialog - Show (Last created dialog) for (All players)
Dialog - Create a label for dialog (Last created dialog) with the dimensions (200, 50) anchored to Center with an offset of (50, 50) with the text (Text((Entered chat string))) color set to White text writeout set to
false with a writeout duration of 2.0
Variable - Set Clock Display = (Last created dialog item)
Dialog - Show (Last created dialog item) for (All players)
Is there a way you could either show me what I've done wrong, or upload a map?
After a few hours of methodically adjusting every possible value (read as: screwing around with determination), it seems to work if you set the "Return - lp_timeOfDay" field from a Value to Custom Script. It doesn't change the way it looks in the editor, just how it compiles. I don't fully understand how it works, so I can't really be any more descriptive than that. :\
For those of you caught up on the Convert Time To String function, (as it seems that's the only real hang-up with his post, it stumped me too I had to spend a few hours on it figuring out what he did) I'll post a step-by-step.
To start, right-click on the tree on the left that lists all your triggers, go to New > New Function. It's important that you Rename the function to "TimeOfDayToString" to be compatible with the rest of Prancer's triggers.
Inside the TimeOfDayToString function you just created, click on Return Type: Boolean. At the bottom of the screen there'll be options to configure the return type, in the drop-down menu select String.
Below the Retun Type is the Parameters. Right-click on Parameters and choose New > Parameter. Click on the Untitled Parameter 001 and - similar to the Return Type - there'll be a drop-down menu at the bottom. In this Drop-down you're going to choose Time Of Day. Now, right-click your Untitled Parameter 001 and rename it to Time Of Day. The parameter should now look like "Time Of Day = 00:00:00 <Time Of Day>" (without quotes)
Your Grammar Text at this point should look like - Grammar Text: TimeOfDayToString(Time Of Day)
Hint text is unimportant to the syntax of the function, name that anything you'd like to help you remember what this function's purpose is (if the title isn't enough =P)
Go on down to Actions. Give this function a new Action. On the left, under General, there'll be the Action called simply Return. Select that.
This is the part that might've screwed you up. Under the Return action that you just created, click Value. By default its type is set to Boolean. You're going to click on the button that says Custom Script (It'll be to the right of the default tab that you're currently on which is Value). The Custom Script page should just have a line that allows you to enter text. In this text you're going to either type or just copy what I've typed here -exactly-
Once that's entered, press okay. Your action should now look like "General - Return lp_timeOfDay" (without quotes again)
Any questions just hit me up, I can upload a map with the basic clock from Prancer's code for you if you still can't get it down.