Map Database  •  FAQ  •  RSS  •  Login

Problem with laggy script

<<

Aasfresser_

Peasant

Posts: 4

Joined: 21 Jan 2018, 15:52

KaM Skill Level: Veteran

Post 06 Mar 2019, 17:03

Problem with laggy script

Heyho KaM community.
I hope you guys can help me with a script I'm writing. It is for a multiplayer map. In this map you can unlock technologies, that let your production buildings to be more efficent. So for example I have a technologie, which lets your woodcutters produce 2 Treetrunks instead of one, if they are build on dirt. The script is working fine, but is creating lag :(. I checked the log file and found a billion messages with : "22:04:01.107 2915,481s 1773ms Script Invalid parameter(s) passed to States.StatResourceProducedCount: 3, 0", "2:04:01.207 2915,581s 93ms Script Invalid parameter(s) passed to States.StatResourceProducedCount: 3, 0". I assume that this means the problem is in my code. But my scripting skills and understanding of it isn't good enough to understand why it is creating lag.

Here is the complete code I use to unlock the technologie (1), store the woodcutters on dirt (2) and adding extra Treetrunks to the building (3)

I'm thankful for any help. :D

(1)
  Code:
if ((States.UnitType(aUnit) = 14) and (States.HouseType(States.HouseAt(States.UnitPositionX(aUnit),(States.UnitPositionY(aUnit) -1))) = 18) and (bTimberSale = true) and (bPinePlantation = false)) then begin iOwner := States.UnitOwner(aUnit); Actions.UnitKill(aUnit, true); if bPinePlantation = true then Actions.ShowMsg(iOwner, 'Pine Plantation has already been researched'); if bPinePlantation = false then begin iaPinePlantation[iOwner] := iaPinePlantation[iOwner] +1 sTechText[iOwner] := ('[$'+ States.PlayerColorText(iOwner) + ']' + States.PlayerName(iOwner) + ' is researching: Pine Plantation '); sTechCount[iOwner] := inttostr(iaPinePlantation[iOwner]); sTechCountNeeded[iOwner] := (' /20'); Overlay; if iaPinePlantation[iOwner] = 20 then begin bPinePlantation := true iPinePlantationP := iOwner; iTree := States.StatResourceProducedCount(iPinePlantationP, 0); Actions.ShowMsgGoto(iPinePlantationP, 142, 2, 'Pine Plantation has been researched.'); sTechText[iOwner] := (''); sTechCount[iOwner] := (''); sTechCountNeeded[iOwner] := (''); Overlay; Actions.FogRevealCircle(iPinePlantationP, 138, 2, 2); Actions.FogRevealCircle(iPinePlantationP, 140, 2, 2); Actions.FogRevealCircle(iPinePlantationP, 142, 2, 2); Actions.FogRevealCircle(iPinePlantationP, 144, 2, 2); Actions.FogRevealCircle(iPinePlantationP, 146, 2, 2); iaHouses := States.PlayerGetAllHouses(iPinePlantationP); for iWhich := 0 to Length(iaHouses)-1 do begin iXW := States.HousePositionX(iaHouses[iWhich]); iYW := States.HousePositionY(iaHouses[iWhich]); if (States.HouseType(iaHouses[iWhich]) = 9) and (States.MapTileType(iXW, iYW) >=34) and (States.MapTileType(iXW, iYW) < 60) then begin iaTreesHouse[iTreesCounter] := iaHouses[iWhich]; iaTreesStored[iTreesCounter] := States.HouseResourceAmount(iaHouses[iWhich], 0); iTreesCounter := iTreesCounter +1 end; end; end; end; end;
(2)
  Code:
if ((States.HouseType(aHouse) = 9) and (States.HouseOwner(aHouse) = iPinePlantationP) and (bPinePlantation = true)) then begin iXB := States.HousePositionX(aHouse); iYB := States.HousePositionY(aHouse); if ((States.MapTileType(iXB, iYB) >=34) and (States.MapTileType(iXB, iYB) < 60)) then begin iaTreesHouse[iTreesCounter] := aHouse; iaTreesStored[iTreesCounter] := States.HouseResourceAmount(aHouse, 0); iTreesCounter := iTreesCounter +1 end; end;
(3)
  Code:
//Pine Plantations if ((States.StatResourceProducedCount(iPinePlantationP, 0) > iTree) and (bPinePlantation = true)) then begin iTree := iTree +1 for iWhich := 0 to iTreesCounter -1 do begin if ((States.HouseResourceAmount(iaTreesHouse[iWhich], 0) > iaTreesStored[iWhich]) and (States.HouseDestroyed(iaTreesHouse[iWhich]) = false)) then begin iTree := iTree +1 Actions.HouseAddWaresTo(iaTreesHouse[iWhich], 0, 1); iOverlayPinePlantation := iOverlayPinePlantation +1 end; iaTreesStored[iWhich] := States.HouseResourceAmount(iaTreesHouse[iWhich], 0); end; end;
You do not have the required permissions to view the files attached to this post.
Last edited by Aasfresser_ on 06 Mar 2019, 18:13, edited 1 time in total.
<<

grayter

Barbarian

Posts: 107

Joined: 18 Aug 2014, 12:06

KaM Skill Level: Skilled

Location: Poland

Post 06 Mar 2019, 17:26

Re: Problem with laggy script

Script you posted is fine. Attach entire mission then we will be able to recreate that bug and fix it.
<<

Aasfresser_

Peasant

Posts: 4

Joined: 21 Jan 2018, 15:52

KaM Skill Level: Veteran

Post 06 Mar 2019, 18:14

Re: Problem with laggy script

Thanks for the quick reply. I've added the mission to the original post. But I warn you the total code is a bit lengthy :)
<<

grayter

Barbarian

Posts: 107

Joined: 18 Aug 2014, 12:06

KaM Skill Level: Skilled

Location: Poland

Post 06 Mar 2019, 19:04

Re: Problem with laggy script

In map editor there are 6 players numbered 1-3, 6, 8-9. Players are 0-based which means that player 1 in game is player 0 in script. So in the end you have players 0-2, 5, 7-8 in script.

The error you encountered is caused by wrong ownerId. You are passing '3' as owner parameter but this owner does not exists.

What should you do? Change player numbers in mapEd OR adjust player number in script.
IMPORTANT NOTE: If some of specified players will be defeated the situation will be exact the same. Before calling Stat method you need to make sure that player you checks exists (States.PlayerEnabled) and is not defeated yet (States.PlayerDefeated).

I found also "Invalid parameter(s) passed to Actions.HouseDestroy" error. This situation is exactly the same as above. You need to check if house is destroyed before calling HouseDestroy function. You cannot destroy one house two times.

Your script is very advanced so there are many potential bugs. Before calling any modifying function you need to check if house, unit, grup etc. exists and can be modified with those functions. If you skip those conditions game do it for you but your code will lag.

I tested your script and unfortunately, it is little bit unefficient. If you really want to change mechanics of some functionalities you should control it by yourself using some advanced programming techniques.

If you want some help send me PM.

Cheers
<<

Aasfresser_

Peasant

Posts: 4

Joined: 21 Jan 2018, 15:52

KaM Skill Level: Veteran

Post 07 Mar 2019, 10:41

Re: Problem with laggy script

Thanks for the help. I'll change the players in the map Editor to fit my script and try to update the conditions for my actions :)

Return to “Dynamic Scripting”

Who is online

Users browsing this forum: No registered users and 3 guests