Page 1 of 1

Have a problem in scripting

PostPosted: 21 Dec 2013, 23:03
by mroni
Hello!
I've got the problem in writing script. I want to spawn group of knights, when the players troop reach position X, and then, to attack player with them. But, I don't know how to do it. This one isn't validated '[Error] (9:56): Type mismatch'. When i delete 'Knights :=' near Actions, is validated, but they don't attack. Also, they don't spawn correctly. When < 93, they spawn immediately (but my troop are below 93 on the Map editor!), when >93, they don't spawn at all. No idea what to do.
  Code:
var Knights: Integer; var Scout: Integer; procedure OnMissionStart; begin Scout := Actions.GiveGroup(0, 21, 51, 108, 0, 3, 3); if States.UnitPositionY(Scout) < 93 then Knights := Actions.GroupOrderAttackUnit(Knights, Scout); if States.UnitPositionY(Scout) < 94 then Knights := Actions.GiveGroup(1, 24, 58, 90, 6, 9, 3); end;
And also I have some questions:
1. Could someone explain me more about var and integer - what they are and what they are using for?
2. How to attribute UnitID to unit, which I placed on the map in KAM Remake Map Editor (and do the same for buildings)?
3. How to order specific group attack not unit, but the whole player's army?

Re: Have a problem in scripting

PostPosted: 22 Dec 2013, 10:18
by Lewin
var means variable. A variable is a place where you can store some value and access it later in your code. Integer means the variable contains a number, in this case the number is the ID of the group. This code creates a variable with the name "Knights" which can contain a number:
var Knights: Integer;

Then problem is that Actions.GroupOrderAttackUnit does not return a value, so this line won't work and causes the error:
Knights := Actions.GroupOrderAttackUnit(Knights, Scout);

You can't have "Knights :=" before a command unless that command will return something. Also, you can't use the variable Knights before you assign it to something. You need to do something like:
Knights := Actions.GiveGroup(1, 24, 58, 90, 6, 9, 3);

Before you do anything that uses the variable Knights like this:
Actions.GroupOrderAttackUnit(Knights, Scout);

Re: Have a problem in scripting

PostPosted: 22 Dec 2013, 10:24
by mroni
Thanks for your answer. Two more questions - I spawned group and ordered it to walk, and it works. But how to order it to attack player's army? I can't find proper command. Or it's just impossible at the moment?
And how to make event once when condition are met? Like OnTick, but only once, without using time condition (e.g. when player’s group reach one position (no matter what time), give AI group of knights).

Re: Have a problem in scripting

PostPosted: 22 Dec 2013, 10:59
by Lewin
Thanks for your answer. Two more questions - I spawned group and ordered it to walk, and it works. But how to order it to attack player's army? I can't find proper command. Or it's just impossible at the moment?
If you know the exact target (for example you created it with Actions.GiveGroup) you can use Actions.GroupOrderAttackUnit. If you don't know the exact target you need to search for an enemy unit nearby. In the current release you need to write your own search procedure, but in the next release you will be able to use commands like States.ClosestGroup.
And how to make event once when condition are met? Like OnTick, but only once, without using time condition (e.g. when player’s group reach one position (no matter what time), give AI group of knights).
You can make a global variable named "EventHasBeenActivated" of type Boolean (which means true or false). OnMissionStart set it to false, then when you run the event set it to true, and don't run the event again if it is already true (so it will only happen the first time when the variable is false).

Re: Have a problem in scripting

PostPosted: 22 Dec 2013, 11:34
by mroni
Thanks, I'll try it :)

EDIT:
And the last question ;)
I managed with EventHasBeenActivated and it works, but I still can't force group to attack player. I use:
  Code:
Actions.GroupOrderAttackUnit(Knight, Scout);
Both groups were spawned and marked earlier, but knight don't do anything.
And when i spawn a single unit for player instead of group, it works perfectly. Like the command is only fora attacking unit, not group.

Re: Have a problem in scripting

PostPosted: 22 Dec 2013, 13:42
by Lewin
The problem is that Groups and Units are different. A unit is an individual soldier or villager, a group is a collection of soldiers linked together. If you have:
Actions.GroupOrderAttackUnit(Knight, Scout);

Then Scout must be a unit, not a group. You should be able to solve it with:
Actions.GroupOrderAttackUnit(Knight, States.GroupMember(Scout, 0));

That will tell the knights to attack the first member of the Scouts group (the flag holder). States.GroupMember lets you get an individual unit out of the group.

Re: Have a problem in scripting

PostPosted: 22 Dec 2013, 13:51
by mroni
Works, thanks a lot!