I haven't pinned it down completely but it seems related to this, it's an extract from a debug log I created:
0.000 ADDED LIGHT 1
0.000 *** ADDING SPRITE 10045
0.000 Light Sprite 10046
0.000 ADDED LIGHT 2
0.000 *** ADDING SPRITE 10047
0.000 Light Sprite 10048
0.000 ADDED LIGHT 3
0.000 *** Hit Light 10047
0.000 Deleting Light 3
0.000 Deleting Light Sprite 10048
0.000 Deleted Light Sprite 10048
0.000 Replacing Light ID , Lightnum = 3 , i = 3
0.000 Replaced Light ID , Lightnum = 3 , i = 3
0.000 Deleted Light 3
0.000 Deleted Sprite 10047
0.000 *** Hit Light 10043
0.000 Deleting Light 1
0.000 Deleting Light Sprite 10044
0.000 Deleted Light Sprite 10044
0.000 Replacing Light ID , Lightnum = 2 , i = 1
0.000 Replaced Light ID , Lightnum = 2 , i = 1
0.000 Deleted Light 1
0.000 Deleted Sprite 10043
0.000 *** Hit Light 10045
0.000 Deleting Light 2
0.000 Deleting Light Sprite 1951426586
0.000 Deleted Light Sprite 1951426586
0.000 Replacing Light ID , Lightnum = 1 , i = 2
0.000 Replaced Light ID , Lightnum = 1 , i = 2
The last deletion tries to delete sprite 1951426586. You can see at the top it was created as 10048.
I can't reproduce consistently, here I created 24 lights before it broke.
I observed another error where the small sprite was deleted, and the larger transparent one was left behind. Interestingly in this scenario the log shows no redimming (an extra log line I added after the one above was taken)This example shows a good delete followed by a bad one. You can also see the program doesn't crash and allows me to add another light:
0.000 *** Hit Light 10019
0.000 Deleting Light 3
0.000 Deleting Light Sprite 0
0.000 Deleted Light Sprite 0
0.000 Replacing Light ID , Lightnum = 1 , i = 3
0.000 Replaced Light ID , Lightnum = 1 , i = 3
0.000 Redimming Light, Lightnum = 0
0.000 Redimmed Light, Lightnum = 0
0.000 Deleted Light 3
0.000 Deleted Sprite 10019 ===> Should be followed by REPLACE AND REDIM and
0.000 *** ADDING SPRITE 10021
0.000 Light Sprite 10022
[EDIT] I also just noticed, the sprite hit isn't registered either for this scenario.
I think as someone mentioned the dec, inc commands can be flaky. I replaced those but it still broke.
You can't run this directly but here's the code with my debug lines, so you can see where they were:
rem
rem AGK Application
rem
rem Landscape App
#include "debug.agc"
setVirtualResolution(640,480)
global lightNum = 0
type lightType
spr as integer
red as float
green as float
blue as float
alpha as float
range as float
x as float
y as float
img as float
kind as integer
parent as integer
cast as integer
player as integer
endtype
type tGlobal
debug
debugFile
time as float
endtype
global g as tGlobal
global dim light[lightNum] as lightType
global pairNum = 0
type pairType
spr as integer
ID as integer
endtype
global dim pair[pairNum] as pairType
g.debug = 1 :debugStart()
rem A Wizard Did It!
do
print("click to create a sprite")
print("right click to delete a sprite")
x# = screenToWorldX(GetPointerX())
y# = screenToWorldY(GetPointerY())
if getPointerPressed()>0
currentSprite = createSprite(0)
setSpriteSize(currentSprite, 32, 32)
setSpritePositionByOffset(currentSprite, x#, y#)
setSpriteGroup(currentSprite, 1)
setSpriteShape(currentSprite, 3)
setSpriteColor(currentSprite, 255, 255, 255, 255)
debugWrite("*** ADDING SPRITE " + str(currentSprite))
lightID = addLight(0, x#, y#, 255, 255, 255, 255, 100, 0)
debugWrite(" ADDED LIGHT " + str(lightID))
addLightPair(currentSprite, lightID)
endif
if getRawMouseRightPressed()>0
hit = getSpriteHitGroup(1, x#, y#)
if hit>0
debugWriteSave("*** Hit Light " + str(hit))
ID = findLightID(hit)
debugWriteSave(" Deleting Light " + str(id))
deleteLight(ID)
debugWriteSave(" Deleted Light " + str(id))
deleteSprite(hit)
debugWriteSave(" Deleted Sprite " + str(hit))
endif
endif
sync()
loop
// add new light
function addLight(coronaImage, x#, y#, red#, green#, blue#, alpha#, range#, kind)
lightNum = lightnum + 1
dim light[lightNum] as lightType
spr = createSprite(coronaImage)
setSpriteSize(spr, range#*2, range#*2)
setSpritePositionByOffset(spr, x#, y#)
setSpriteColor(spr, red#, green#, blue#, alpha#*0.75)
setSpriteShapeCircle(spr, 0, 0, 8)
debugWriteSave(" Light Sprite " + str(spr))
parent = 0
light[lightNum].spr = spr
light[lightNum].x = x#
light[lightNum].y = y#
light[lightNum].red = red#
light[lightNum].green = green#
light[lightNum].blue = blue#
light[lightNum].alpha = alpha#
light[lightNum].range = range#
light[lightNum].kind = kind
light[lightNum].parent = parent
light[lightNum].cast = true
light[lightNum].player = false
select kind
case 2
rem light only casts no shadow
light[lightNum].cast = false
endcase
endselect
endfunction lightNum
// delete light
function deleteLight(i)
debugWriteSave(" Deleting Light Sprite " + str(light[i].spr))
deleteSprite(light[i].spr)
debugWriteSave(" Deleted Light Sprite " + str(light[i].spr))
// keep pairs aligned
debugWriteSave(" Replacing Light ID , Lightnum = " + str(lightNum) + " , i = " + str(i))
replaceLightID(lightNum, i)
debugWriteSave(" Replaced Light ID , Lightnum = " + str(lightNum) + " , i = " + str(i))
// swap data
light[i].spr = light[lightNum].spr
light[i].x = light[lightNum].x
light[i].y = light[lightNum].y
light[i].red = light[lightNum].red
light[i].green = light[lightNum].green
light[i].blue = light[lightNum].blue
light[i].alpha = light[lightNum].alpha
light[i].range = light[lightNum].range
light[i].kind = light[lightNum].kind
light[i].parent = light[lightNum].parent
light[i].cast = light[lightNum].cast
light[i].player = light[lightNum].player
lightNum = lightNum - 1
debugWriteSave(" Redimming Light, Lightnum = " + str(lightNum))
dim light[lightNum] as lightType
debugWriteSave(" Redimmed Light, Lightnum = " + str(lightNum))
endfunction
// creates a pair of item integers in the pair list
function addLightPair(spr, ID)
pairNum = pairNum + 1
dim pair[pairNum] as pairType
pair[pairNum].spr = spr
pair[pairNum].ID = ID
endfunction
// find matches in the pair library
function findLightID(spr)
for i=1 to pairNum
if pair[i].spr = spr
ID = pair[i].ID
exitfunction ID
endif
next
endfunction -1
// replaces an id with a new id
function replaceLightID(idOld, idNew)
for i=1 to pairNum
if pair[i].ID = idOld
pair[i].ID = idNew
exitfunction
endif
next
endfunction