CPRL destory after save and load

Started by kai, October 20, 2013, 04:25:04 AM

Previous topic - Next topic

kai

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

eduran

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

J

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.

thepenguin

We have become the creeper...

knucracker

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

kai

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

knucracker

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.

mopa42

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?

knucracker

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.