Map Database  •  FAQ  •  RSS  •  Register  •  Login

Have a problem in scripting

<<

mroni

Peasant

Posts: 4

Joined: 05 Apr 2012, 20:47

Post 21 Dec 2013, 23:03

Have a problem in scripting

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?
<<

Lewin

User avatar

KaM Remake Developer

Posts: 3821

Joined: 16 Sep 2007, 22:00

Location: Australia

KaM Skill Level: Skilled

Post 22 Dec 2013, 10:18

Re: Have a problem in scripting

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);
<<

mroni

Peasant

Posts: 4

Joined: 05 Apr 2012, 20:47

Post 22 Dec 2013, 10:24

Re: Have a problem in scripting

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).
<<

Lewin

User avatar

KaM Remake Developer

Posts: 3821

Joined: 16 Sep 2007, 22:00

Location: Australia

KaM Skill Level: Skilled

Post 22 Dec 2013, 10:59

Re: Have a problem in scripting

mroni wrote: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.

mroni wrote: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).
<<

mroni

Peasant

Posts: 4

Joined: 05 Apr 2012, 20:47

Post 22 Dec 2013, 11:34

Re: Have a problem in scripting

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.
<<

Lewin

User avatar

KaM Remake Developer

Posts: 3821

Joined: 16 Sep 2007, 22:00

Location: Australia

KaM Skill Level: Skilled

Post 22 Dec 2013, 13:42

Re: Have a problem in scripting

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.
<<

mroni

Peasant

Posts: 4

Joined: 05 Apr 2012, 20:47

Post 22 Dec 2013, 13:51

Re: Have a problem in scripting

Works, thanks a lot!

Return to Feedback / Discussion

Who is online

Users browsing this forum: No registered users and 4 guests