if loops and local variables?

Started by transistor77777, June 23, 2014, 05:30:35 PM

Previous topic - Next topic

transistor77777

I have a script for a CPRLTower that is supposed to create one new CprlTower orbital cannon when it reaches full ammo, then tell the cannon to launch itself into orbit when a player clicks the launch button above it.
Spoiler


# orbitalCannonBase.crpl
# Created on: 6/22/2014 4:56:30 PM
# ------------------------------------------

$MaxAmmo:200

once
SetUnitAttribute(self CONST_MAXAMMO <-MaxAmmo)
SetUnitAttribute(self CONST_BEAMTARGET false )
SetUnitAttribute(self CONST_CANREQUESTAMMO true )
SetUnitAttribute(self CONST_CONNECTABLE true )
SetUnitAttribute(self CONST_CREATEPZ false )
SetUnitAttribute(self CONST_NULLIFIERDAMAGES false )
SetUnitAttribute(self CONST_REQUESTPACKETS true )
SetUnitAttribute(self CONST_SNIPERTARGET false )
SetUnitAttribute(self CONST_SUPPORTSDIGITALIS false )
SetUnitAttribute(self CONST_COUNTSFORVICTORY false )
endOnce

if(GetUnitAttribute(self CONST_AMMO) <-MaxAmmo eq )
SetImage(self "fireButton" "Custom0_128")
SetImagePosition(self "fireButton" 0 20 -0.01)
SetImageScale(self "fireButton" 3 3)

if(<-created eq0)
CreateUnit("CRPLCore" CurrentCoords) ->cannonID
AddScriptToUnit(<-cannonID "orbitalCannon.crpl")
SetScriptVar(<-cannonID "orbitalCannon.cprl" "baseID" self)
1 ->created
endIf

GetMouseCell ->my ->mx
if(and(and(<-my gt(sub(CurrentY 4)) <-mx gt(sub(CurrentX 4)))and(<-my lt(sub(CurrentY 1)) <-mx lt(add(CurrentX 4)))))

if(getMouseButton(0))
SetUnitAttribute(self CONST_AMMO 0)
SetUnitAttribute(self CONST_CANREQUESTAMMO false )

SetScriptVar(<-cannonID "orbitalCannon.cprl" "launching" 1)
endIf
endIf
else
SetImage(self "fireButton" "NONE")
endIf

[close]
The problem is that when it reaches full ammo it spawns an orbital cannon every game tick and doesn't ever stop. I'm pretty sure its being caused by this if loop:

if(<-created eq0)
CreateUnit("CRPLCore" CurrentCoords) ->cannonID
AddScriptToUnit(<-cannonID "orbitalCannon.crpl")
SetScriptVar(<-cannonID "orbitalCannon.cprl" "baseID" self)
1 ->created
endIf

<-created is supposed to start out at 0 then get set to 1 after the cannon is created. it should make the if loop only run once because it doesn't equal 0 anymore, but for some reason the loop executes repeatedly!
I have no idea why this isn't working, is there something i did wrong with eq0?

knucracker

I attached your script to a core and it seems to actually work ok for me.  Make sure you have saved and recompiled all scripts.  Then save and reload the mission.  It might be as simple as something related to that...

transistor77777

It turns out that the base script wasn't even the problem. I had accidentally programmed the orbital cannon to create 5 orbital cannons instead of bombs, and then each of those would create 5 more orbital cannons, and so on until the end of time. thanks for your help anyway!

knucracker

Ahhh... I see.  I named my script something different so I didn't get the problem (I was only trying to check the "created" var and the eq0).