Knuckle Cracker

Creeper World 3 => The Coder's Corner => Topic started by: kai on October 20, 2013, 04:25:04 AM

Title: CPRL destory after save and load
Post by: kai on October 20, 2013, 04:25:04 AM
The following code works perfect, but if i save the game when building a PZFactory, then load the game,
the PZFactory will be immediately destroyed...
Also another question: How to get the powerzone in range?

i use this function to build a PZFactory(CPRL):

:BuildPZFactory #void BuildPZFactory(int posx, int posy)
 ->posy ->posx
 CreateUnit("CRPLCORE" <-posx <-posy) ->unitUID
 AddScriptToUnit(<-unitUID "PZFactory.crpl")


And the "PZFactory.crpl":
once
 SetImage(Self "main" "Custom0")
 
SetUnitAttribute(Self CONST_ISBUILDING TRUE)
SetUnitAttribute(Self CONST_BUILDCOST 250)
SetUnitAttribute(Self CONST_CONNECTABLE TRUE)
SetUnitAttribute(Self CONST_REQUESTPACKETS TRUE)
SetUnitAttribute(Self CONST_MAXHEALTH 250)
SetUnitAttribute(Self CONST_HEALTH 0)
SetUnitAttribute(Self CONST_CREATEPZ FALSE)
 SetUnitAttribute(Self CONST_NULLIFIERDAMAGES FALSE)
endonce

if (not(GetUnitAttribute(Self CONST_ISBUILDING)))
CreateUnit("POWERZONE" CurrentCoords)
Destroy(Self 1)
endif
Title: Re: CPRL destory after save and load
Post by: eduran on October 20, 2013, 05:19:45 AM
Your code seems fine to me. For some reason the factory instantly finishes building when you load a game and therefore triggers the destruction. Strange, could be a bug.

Quote from: kai on October 20, 2013, 04:25:04 AM
Also another question: How to get the powerzone in range?
GetUnitsInRange(CurrentCoords <-Range) ->unitCount
do(<-unitCount 0)
->unit
if(eq(GetUnitType(<-unit) "POWERZONE"))
#powerzone found, do something
endif
loop
Title: Re: CPRL destory after save and load
Post by: J on October 20, 2013, 06:54:12 AM
I guess that not all states are persisted when saving/loading. Something like this might do the trick until it is fixed:
:Awake
Self CONST_ISBUILDING true SetUnitAttribute
Note that this feuture is not documented in the wiki so I'm not 100% sure if this is correct.
Title: Re: CPRL destory after save and load
Post by: thepenguin on October 20, 2013, 07:39:46 PM
virgil has been notified.
Title: Re: CPRL destory after save and load
Post by: knucracker on October 20, 2013, 07:41:35 PM
There are actually two bugs (just fixed, so in the upcoming first patch).  The first is that the build_cost doesn't get persisted.  This means the newly created unit loads with a build cost of 0, so it instantly completes.  The second bug (being covered by the first) is that the "Request Packets" bool isn't being loaded properly (it was being persisted, though).
Title: Re: CPRL destory after save and load
Post by: kai on October 21, 2013, 12:55:23 AM
Quote from: virgilw on October 20, 2013, 07:41:35 PM
There are actually two bugs (just fixed, so in the upcoming first patch).  The first is that the build_cost doesn't get persisted.  This means the newly created unit loads with a build cost of 0, so it instantly completes.  The second bug (being covered by the first) is that the "Request Packets" bool isn't being loaded properly (it was being persisted, though).
I can't wait for the next patch then ;)
Title: Re: CPRL destory after save and load
Post by: knucracker on October 21, 2013, 11:25:12 AM
You might be able to get by this in the mean time as J mentioned.

Add an :Awake function to your script and set the buildcost and requestpackets attributes again.  I've not tried it, but that might just work.
Title: Re: CPRL destory after save and load
Post by: mopa42 on October 21, 2013, 09:05:51 PM
Quote from: virgilw on October 21, 2013, 11:25:12 AM
Add an :Awake function to your script...

So when is this :Awake function called? Whenever loading a saved game?
How does it compare to InvocationCount?
Title: Re: CPRL destory after save and load
Post by: knucracker on October 24, 2013, 09:55:18 AM
Awake gets called as soon as the core is created (so during the game load before the first frame).
There is another intrinsic method called "GameLoaded" that gets called right after the game load is complete.

The InvocationCount gets incremented just before commands are processed during a normal game update.  So invocationCount will be 0 in the Awake call.