Scripting Tutorial By Kronos1000

Hello and welcome to my scripting tutorial I’ll explain how this works. :?: means a question of course it are all over the guide and then I’ll explain how. :!: means IMPORTANT these are things you’ll have to keep in mind while you’re writing your script.

This font is used as a place for a scripting example

Well then, there are three parts in this guide Basic, Advanced and Expert. Each of them explains a part of the scripting language the contain the following:

Basic: Messages, end; questions, variables and easy functions, triggers and syntaxes. Advanced: Harder triggers like OBJECT_CAPTURE_TRIGGER, startThread() and “for loops” plus more syntaxes and harder functions. Expert: “while loops”, Combat scripts, even more syntaxes :shock: and harder functions once again.

If you already understand everything of one part, feel free to skip it. ;-)


Basic


:?: How to create a script :?:

To create a script you’ll have to go to the map editor create a new map or open an excisting one it doesn’t matter.

:!: It has to be a single mission or else the script won’t work :!:

Go to the map properties and select the script tag. Now, if you press the edit script button you’ll the there are no help bars like in Heroes IV so you’ll have to write everything yourself 8-O Don’t be in a shock there’s nothing to worry about you have this guide so everything should be fine. :D

:?: How to create a message box :?:

You can’t write a message in the script window like Heroes IV so you’ll have to write somewhere else first click this button: http://img218.imageshack.us/img218/1760/intrctns1zj6.jpg Then you open the resources tab and add a new file: http://img218.imageshack.us/img218/8626/intrctns2gd9.jpg Then you’ll have to add a new file: http://img218.imageshack.us/img218/4674/intrctns3pl7.jpg Let’s name it StartMessage. Now write your Message. So now do you have a Message what to do next? The answer is simple, just place this line in the script window:

MessageBox(“Maps/SingleMission/The name of your Map/StartMessage.txt”)

Note: Everything that you just place in the script editor will trigger on the first day.

Do you understand what we’ve just done? A h5m file can be opened with any zip program (such as, AlZip, UltimatZip and WinRar) The Message you wrote was added to the zip file and HoMM5 can read it in his data. If you want you can experiment a bit with MessageBoxes, but if you want to continue let’s move on.

:?: How to trigger a Message on a specific moment :?:

Not so fast to do that you’ll have to learn about the Syntax. Since the Heroes script uses the syntax of Lua it is not differant exept for the functions of course, let’s check out Lua’s signs:

== Means Is equal to. > Greater then. < Means smaller then. >= means greater then or Equal to. ⇐ means Smaller then or Equal to. ~= means Unequal to.

:!: You must know this signs by heart or else you can’t write more advanced scripts then MessageBoxes :!:

Now you know this you must know how to use them. These signs are used in function to you compare two values with each other. First we’re going to trigger a Message at Day 2. First create a message, now return to your script, to compare the current day with the day it must have you use the following function: GetDate(DAY). Enter this in your script:

function NewDay() if GetDate(DAY) == 2 then MessageBox(“Maps/SingleMissions/Your Map/Your Message”) end; end; Trigger(NEW_DAY_TRIGGER, “NewDay”)

Explaination: function means a function made by you with various things made by you (A compairison isn’t neccisarry). NewDay is the name of the function you can change it to whatever you like. if GetDate(DAY) == 2 then should be clear. end; THAT’S VERY IMPORTANT! At the end of an if you place an end; and you place another one at the end of the function. A function can have two if’s but the second one has to come after the first end; Trigger(NEW_DAY_TRIGGER means it activates a function every day so he compares it every day until the values are correct. “NewDay” is the name of the function the NEW_DAY_TRIGGER works on, if you change the function’s name change this to the new name as well.

:!: It’s critical to experiment with this you just have to understand this because this is the basic and it’ll be expanded later :!:

To do more things with GetDate you can enter these values between the (): DAY – day WEEK – week MONTH – month DAY_OF_WEEK – day of week ABSOLUTE_DAY – same as DAY

Now we’re going to trigger a MessageBox when you’re in a specific area. First write your Message you have to know how by heart by now. When you’re done you have to draw a region see this picture: http://img209.imageshack.us/my.php?image=regiowg9.png

You’ll se my Enter tbhe Abyss map has much Regions but look close on the picture you can select the new region button here and draw a new region on your map :D A region can have many function we’re going to use one of them, the others will be handeled in the Anvanced part of the guide. What are we going to do? We’re going to make a message that triggers once you step on the region there are several ways this is the most common one: The underlined parts will be explained.

function Region([u]heroname[/u]) if [u]GetCurrentPlayer() == PLAYER_1[/u] then MessageBox(…) end; end; Trigger([u]REGION_ENTER_AND_STOP_TRIGGER[/u], “RegionName”, “Region”)

heroname = the name of the hero that enters the region you can change the if to this:

if heroname == “Linaas” then it can only be used with triggers that allow you to see the script functions.pdf for more information.

Note: The hero’s scripting name has to be used it can be found if you go to the hero’s properties and select settings.

GetCurrentPlayer() another function it gets the player that comes by like PLAYER_1 or PLAYER_2 etc.

REGION_ENTER_AND_STOP_TRIGGER means that the hero who enters the region stands still until all scripts are executed, you can also use REGION_ENTER_WITHOUT_STOP_TRIGGER wich means the Hero just walks through even if the script is not completed yet.

:!: Once again experiment with this you just have to understand this it’ll be expanded later :!:

:?: What are Variables :?:

A Variable is a word that you can give your own value, you can define one by using one = like this:

MyVar = 1234

There are three kinds of Variables: Numeric, Bolean and String.

Numeric = A variable that is a number or that gives a number such as:

Day = GetDate(DAY) and MyVar = 1234 are both numeric variables.

Bolean = A variable that is true or false (0 or 1) of a function that gives tru of false.

String is a word or gives a word between ""

:?: How to use a Variable :?:

Bolean: Have you ever noticed that if you use a Region script it triggers each time to prevent that you’ll have to edit the following:

Bolean = 0 function Region(heroname) if GetCurrentPlayer() == PLAYER_1 and Bolean == 0 then MessageBox(…) Bolean = 1 end; end; Trigger(REGION_ENTER_AND_STOP_TRIGGER, “RegionName”, “Region”)

Try it now it only triggers once :D

Numeric: I don’t use this much but it can be used as shortcuts like this:

Day = GetDate(DAY) function GetDay() if Day == 2 then MessageBox(…) end; if Day == 3 then Win() end; end; Trigger(NEW_DAY_TRIGGER, “GetDay”)

A Numeric Variable can also contain sums like this:

Date = GetDate(DAY) Yoh = Day/2

Sum syntax:

+ means plus - means minus * means times / means divided by

There is a great chance you allready knew this but only times and divided by are different.

String: Will be explained in the advanced guide it doesn’t have a function if I check what you know now.

What you have learned

Syntax:

== Means Is equal to. > Greater then. < Means smaller then. >= means greater then or Equal to. ⇐ means Smaller then or Equal to. ~= means Unequal to. + means plus - means minus * means times / means divided by

Functions:

GetDate() GetCurrentPlayer() Win() MessageBox()

Other:

The first three Variables How to make a function

Well that was pretty though wasn’t it but you’re done with the basics now, great job 8-)

scripting_tutorial/basic.txt · Last modified: 2007/07/20 19:55 by kronos
Creative Commons License Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0