Map Database  •  FAQ  •  RSS  •  Login

Problem with Dynamic Script in new RC3

<<

Strangelove

User avatar

Crossbowman

Posts: 230

Joined: 30 Jul 2013, 06:32

KaM Skill Level: Fair

Post 02 Oct 2014, 18:31

Problem with Dynamic Script in new RC3

For testing purposes I copied my maps for my upcoming campaign in into the new RC3. The scripts that worked in RC2 do not work in RC3. I tried excluding certain parts of the script to troubleshoot, however, the error jumps from one part to the other. Also the error-massage does not give any hit, where the problem might be. It only states that there is a "mistake in script usage States.GroupMembers: 0, 0" (see attachment).
  Code:
//-- TBK08 --// var bGiveNext: array[1..10] of Boolean; var iX, iY, iGiveU, iGiveG: array[1..10] of Integer; var iD, iType, iOwner: array[1..10] of Byte; procedure UnitsRandomHungerSet(); var aUnits: array of Integer; var i, x, iHunger, iGroup: Integer; var iPlayerLoop: Byte; begin for iPlayerLoop := 1 to States.StatPlayerCount()-1 do begin aUnits := States.PlayerGetAllUnits(iPlayerLoop); for i := 0 to Length(aUnits)-1 do begin if States.UnitsGroup(aUnits[i]) = -1 then begin Actions.UnitHungerSet(aUnits[i], States.UnitMaxHunger/2 + States.KaMRandomI(States.UnitMaxHunger/2 +1)); end else iGroup := States.UnitsGroup(aUnits[i]); if States.GroupMember(iGroup, 0) = aUnits[i] then begin iHunger := States.UnitMaxHunger/2 + States.KaMRandomI(States.UnitMaxHunger/2 +1) for x := 0 to States.GroupMemberCount(iGroup)-1 do Actions.UnitHungerSet(States.GroupMember(iGroup, x), iHunger); end; end; end; end; procedure KilledUnitData(aUnitID, aGroupID: Integer; i: Byte); begin iX[i] := States.UnitPositionX(aUnitID); iY[i] := States.UnitPositionY(aUnitID); iD[i] := States.UnitDirection(aUnitID); iGiveU[i] := aUnitID; iGiveG[i] := aGroupID; iOwner[i] := States.UnitOwner(aUnitID); iType[i] := States.UnitType(aUnitID); Actions.UnitKill(aUnitID, true); end; procedure OnMissionStart(); begin UnitsRandomHungerSet(); //Block Ware to prevent AI Storehouse from getting too crowded //To be done end; procedure OnWarriorEquipped(aUnitID, aGroupID: Integer); var i, iOwnerLocal, iTypeLocal: Byte; var iFlagholder: Integer; begin iOwnerLocal := States.UnitOwner(aUnitID); //"Transform" Units of AI Players if (iOwnerLocal <> 0) then begin iTypeLocal := States.UnitType(aUnitID); if (States.GroupMemberCount(aGroupID) > 1) and (States.UnitType(aUnitID) = States.UnitType(States.GroupMember(aGroupID, 0))) then begin end; if (States.GroupMemberCount(aGroupID) > 1) and (States.UnitType(aUnitID) <> States.UnitType(States.GroupMember(aGroupID, 0))) then for i := 1 to 10 do begin if (iGiveU[i] = -1) then begin //Map specific if ((iOwnerLocal = 1) or (iOwnerLocal = 2)) and (iTypeLocal = 15) then KilledUnitData(aUnitID, aGroupID, i); //AxeFighter break; end; end; if (States.GroupMemberCount(aGroupID) = 1) then begin if States.KaMRandomI(2) = 0 then //% Chance begin for i := 1 to 10 do begin if (iGiveU[i] = -1) then begin if ((iOwnerLocal = 1) or (iOwnerLocal = 2)) and (iTypeLocal = 15) then KilledUnitData(aUnitID, aGroupID, i); break; end; end; end; end; end; end; procedure OnTick; var iGroup: Integer; var i: Byte; begin for i := 1 to 10 do begin //Give Unit to Player if (iGiveU[i] <> -1) and States.UnitDead(iGiveU[i]) then begin if bGiveNext[i] then begin bGiveNext[i] := false; iGiveU[i] := -1; //Replace Units (Map specific) if iType[i] = 15 then iGroup := Actions.GiveGroup(iOwner[i], 23, iX[i], iY[i], iD[i], 1, 1); //Axefighter -> Barbarian //Check if Unit belongs to a Group and Link it if not States.GroupDead(iGiveG[i]) then Actions.GroupOrderLink(iGroup, iGiveG[i]); end else bGiveNext[i] := true; end; end; end;
What the script does:
UnitsRandomHungerSet() finds all no-human-player units and gives them a individual Hunger-Level when executed. All Units that belong to the same group get the same Hunger level.
Pretty much the rest of the code checks if the AI builds a AxeFighter. If yes there is a change that it gets "replaced" (killed and "summoned") with a Barbarian by a certain chance. They become Barbarians when they are supposed to get linked to barbarians and will not be killed if they are supposed to link to a group of axefighters. If there are no other Units in that group, it gets rolled. The code is pretty messy and immature at the moment...

Sorry for the lack of comments in the code... :$



Maybe you guys got an idea? Did something fundamentally change from RC2 to RC3?
You do not have the required permissions to view the files attached to this post.
<<

Michalpl

Sword Fighter

Posts: 318

Joined: 10 May 2014, 21:46

KaM Skill Level: Fair

Post 02 Oct 2014, 20:04

Re: Problem with Dynamic Script in new RC3

One small error and maybe it will work change
if (States.GroupMemberCount(aGroupID) = 1) then
to
if States.GroupMemberCount(aGroupID) = 1 then

and the group id i think shouldn't be lower than 1
<<

Lewin

User avatar

KaM Remake Developer

Posts: 3822

Joined: 16 Sep 2007, 22:00

KaM Skill Level: Skilled

ICQ: 269127056

Website: http://lewin.hodgman.id.au

Yahoo Messenger: lewinlewinhodgman

Location: Australia

Post 03 Oct 2014, 03:56

Re: Problem with Dynamic Script in new RC3

Maybe you guys got an idea? Did something fundamentally change from RC2 to RC3?
The only change is that we now display these errors to you. These errors still occurred in RC2, but we ignored them, and luckily for you the errors didn't cause any major problems in your script.

"Mistake in script usage" means you gave an invalid parameter, I'll reword that message so it makes sense. "States.GroupMember: 0, 0" means your script executed this command: States.GroupMember(0, 0). The problem is that 0 (in the first parameter) is an invalid group ID. Take a look at your code:
  Code:
if States.UnitsGroup(aUnits[i]) = -1 then begin Actions.UnitHungerSet(aUnits[i], States.UnitMaxHunger/2 + States.KaMRandomI(States.UnitMaxHunger/2 +1)); end else iGroup := States.UnitsGroup(aUnits[i]); if States.GroupMember(iGroup, 0) = aUnits[i] then begin iHunger := States.UnitMaxHunger/2 + States.KaMRandomI(States.UnitMaxHunger/2 +1) for x := 0 to States.GroupMemberCount(iGroup)-1 do Actions.UnitHungerSet(States.GroupMember(iGroup, x), iHunger); end;
I find it too hard to read the way you formatted it, so lets improve the formatting:
  Code:
if States.UnitsGroup(aUnits[i]) = -1 then begin Actions.UnitHungerSet(aUnits[i], States.UnitMaxHunger/2 + States.KaMRandomI(States.UnitMaxHunger/2 +1)); end else iGroup := States.UnitsGroup(aUnits[i]); //This code always runs, even when you haven't set iGroup! if States.GroupMember(iGroup, 0) = aUnits[i] then begin iHunger := States.UnitMaxHunger/2 + States.KaMRandomI(States.UnitMaxHunger/2 +1) for x := 0 to States.GroupMemberCount(iGroup)-1 do Actions.UnitHungerSet(States.GroupMember(iGroup, x), iHunger); end;
I added a comment to show why the error is occurring. You should add a "begin..end" to that else statement so the "if States.GroupMember(iGroup, 0) = aUnits then" code only runs if you set iGroup.

I think this also shows why it's important to format your scripts nicely, errors like this become obvious. Don't just delete all the linebreaks to make it more compact. Compact unreadable code is much worse than less compact but readable code.

One small error and maybe it will work change
if (States.GroupMemberCount(aGroupID) = 1) then
to
if States.GroupMemberCount(aGroupID) = 1 then

Those two lines are equivalent and won't make any difference.
<<

Strangelove

User avatar

Crossbowman

Posts: 230

Joined: 30 Jul 2013, 06:32

KaM Skill Level: Fair

Post 03 Oct 2014, 14:51

Re: Problem with Dynamic Script in new RC3

Thanks for the fast answer/help! In the future it would be nice to see in what line the Error occurs, because at first I did not know what "States.GroupMember" it was refering to.

Also, after I fixed and optimized (since there is a Actions.GroupHungerSet now) that part, the script showed another error, but I could fix it by setting the "iGiveU"-array to -1. However, why did it ignore those errors in the previous RCs and why is it so picky now? :P
<<

Lewin

User avatar

KaM Remake Developer

Posts: 3822

Joined: 16 Sep 2007, 22:00

KaM Skill Level: Skilled

ICQ: 269127056

Website: http://lewin.hodgman.id.au

Yahoo Messenger: lewinlewinhodgman

Location: Australia

Post 03 Oct 2014, 16:22

Re: Problem with Dynamic Script in new RC3

Thanks for the fast answer/help! In the future it would be nice to see in what line the Error occurs, because at first I did not know what "States.GroupMember" it was refering to.

Also, after I fixed and optimized (since there is a Actions.GroupHungerSet now) that part, the script showed another error, but I could fix it by setting the "iGiveU"-array to -1. However, why did it ignore those errors in the previous RCs and why is it so picky now? :P

Unfortunately it's hard to get it to tell you which line number the error occurred on.

I chose to make it display these errors to help script authors fix problems which these errors can cause. It can help you spot mistakes in your scripts, for example using an incorrect house or ware index. I wasn't expected so many scripts to be affected by this change. It seems that a lot of scripts are regularly using invalid parameters and yet mostly still working fine. I'll take this into account. I would still like to continue reporting these errors since they can be very useful for pointing out mistakes in your script. But I will at least publish some help for script authors and try to get every map included in the Remake checked for errors. Perhaps the errors can be made less scary looking, since often the script will still work.
<<

Strangelove

User avatar

Crossbowman

Posts: 230

Joined: 30 Jul 2013, 06:32

KaM Skill Level: Fair

Post 03 Oct 2014, 16:41

Re: Problem with Dynamic Script in new RC3

Before my scripts stopped working I did not even know that I was using invalid parameters to be honest. It worked so I never double-checked why to be honest. However, I am not complaining that KaM tells me that I did something wrong, since it helps me troubleshoot and learn how to do it porperly in the future. I was just a little surprised that it suddenly stopped working. Making it less scary-looking might be a good idea!

Return to “Dynamic Scripting”

Who is online

Users browsing this forum: No registered users and 10 guests