Knuckle Cracker

Creeper World 3 => The Coder's Corner => Topic started by: wolkiewicz on July 23, 2015, 06:56:26 AM

Title: What's wrong?
Post by: wolkiewicz on July 23, 2015, 06:56:26 AM
I wrote some code and it doesn't work, I don't know why.
What should it do:
-Create unit every 45 seconds when fully armed
-Change colors
-Request packets if there is no creeper
-If there is creeper shouldn't be connectable and set ammo to 0
What it arleady does:
-Isn't connectable if creeper > 0

The active variable is used in another script, don't look at it.

There is a script:
$payload:50
$type:"ORE"

CurrentCoords GetCreeper 0 lte if
self CONST_CONNECTABLE TRUE SetUnitAttribute
self CONST_REQUESTPACKETS TRUE SetUnitAttribute
self CONST_CANREQUESTAMMO TRUE SetUnitAttribute
endif

CurrentCoords GetCreeper 0 gt if
self CONST_CONNECTABLE FALSE SetUnitAttribute
self CONST_REQUESTPACKETS FALSE SetUnitAttribute
self CONST_CANREQUESTAMMO FALSE SetUnitAttribute
self CONST_AMMO 0 SetUnitAttribute
self "main" 73 126 238 SetImageColor
endif

self CONST_MAXAMMO GetUnitAttribute ->max
self CONST_AMMO GetUnitAttribute ->ammo

<-max <-ammo eq if
TRUE ->active
self "main" 255 255 255 255 SetImageColor
SetTechLimit("BUILDSPEED" 10)
SetTechLimit("PACKETSPEED" -1)
1350 SetTimer0
GetTimer0 eq0 if
@CreatePacket
endif
else
FALSE ->active
SetTechLimit("BUILDSPEED" 0)
SetTechLimit("PACKETSPEED" 0)
endif

CurrentCoords GetCreeper lte 0
<-max <-ammo lt and
if
Self "main" 193 193 193 255 SetImageColor
endif



:awake
once
self CONST_COUNTSFORVICTORY FALSE SetUnitAttribute
self CONST_NULLIFIERDAMAGES FALSE SetUnitAttribute
self CONST_MAXAMMO 20 SetUnitAttribute
endonce

:CreatePacket
RandXCoord ->targetX
RandYCoord ->targetY
<-targetX <-targetY GetCreeper 0 gt while
repeat
RandXCoord ->targetX
RandYCoord ->targetY
endwhile
"CRPLCore" <-targetX <-targetY CreateUnit ->unit
<-unit "main" "Custom8" SetImage
<-unit "Packet.crpl" AddScriptToUnit
<-unit "Packet.crpl" "payload" <-payload SetScriptVar
<-unit "Packet.crpl" "type" <-type SetScriptVar
<-type "ORE" eq if
"AETHER" ->type
else
"ORE" ->type
endif
Title: Re: What's wrong?
Post by: Tyler21 on July 23, 2015, 07:08:38 AM
I think one problem is that you set your timer every frame to 1350, just before the script checks its value.

So timer0 is always 1350 and that's why @createPacket is never called.

Put "1350 SetTimer0" within the @createPacket call. I didn't check the rest of the code, so there might other issues as well.
Title: Re: What's wrong?
Post by: wolkiewicz on July 23, 2015, 08:38:02 AM
Now it create "Packets" correctly, but it still don't care about ammo or creeper.

I fixed self "main" 73 126 238 SetImageColor
and
CurrentCoords GetCreeper lte 0

When I trace
Trace(self CONST_AMMO GetUnitAttribute)
Trace(self CONST_MAXAMMO GetUnitAttribute)

It says:
0
0
Title: Re: What's wrong?
Post by: warren on July 23, 2015, 12:01:02 PM
MaxAmmo is assigned in :awake once. Did you compile, save and load after making those fixes?

In my test I found that it did care about ammo and it did respond to creeper beneath it.

Edit: I implemented a solution similar to what tyler suggested:
I added 135 ->timer0 to the awake once block then changed

1350 SetTimer0
GetTimer0 eq0 if

to

        <-timer0 1 sub ->timer0
<-timer0 0 lte if
                135 ->timer0             

As a consequence I got a warning in the trace log:
empty stack at repeat at line 60.

I usually put showTraceLog in the :awake function for scripts I am testing.

Put the while at the beginning of the line.