I'm trying to use arrays to hold data for randomly spawned enemies, however I am getting Runtime Error 118 - Array does not exist or array subscript out of bounds at line 1052 error when it actually generates the first random enemy, code snippets below:
From main program:
dim RandomEnemies(0) as Enemy
dim Enemies(0) as Enemy
empty array RandomEnemies(0)
empty array Enemies(0)
From Spawning include file
function spawnchance() `There is a chance of spawning a baddie at an unoccupied spawn point.
randomize timer()
if array count (RandomEnemies()) < ((mapLevel * 5) + CharLevel-1)
chance = rnd(100)+1
if chance <= 20
spawnlocation()
curSpwnIndex = array count (RandomEnemies()) + 1
spawnEnemy(curSpwnIndex)
endif
endif
`if spawnoccupied = 0 AND startX <> Spawnpoints(i).XPos AND startY <> Spawnpoints(i).YPos
` spawnEnemy(curSpwnIndex)
`endif
endfunction
function spawnEnemy(spwn_index) `Determine what the spawn is.
randomize timer()
entity_index = array count(Entitytable())
rnd_Spawn = rnd(entity_index)
spwnCatName$ = Entitytable(rnd_Spawn).CategoryName
spwnCatID = Entitytable(rnd_Spawn).CategoryID
spwnCatEntityID = rnd(Entitytable(rnd_Spawn).CategoryCount - 1) + 1
load xml "Enemies.xml", 2, 1
enemyCnt = GetEntity(2, spwnCatName$,spwnCatID,spwnCatEntityID)
delete xml 2
if array count(RandomEnemies()) <> spwn_index
array insert at bottom RandomEnemies(0)
endif
RandomEnemies(spwn_index) = Enemies(enemyCnt) <== Error Here
RandomEnemies(spwn_index).XPos = Spwn_X
RandomEnemies(spwn_index).YPos = Spwn_Y
endfunction
From Data reading include:
function GetEntity(xmlNo, spwnCatName$,spwnCatID,spwnCatEntityID)
rootValue = xml document root(xmlNo)
`Check if reading Enemy file.
if xml node name(xmlNo, rootValue) <> "Enemies" `"Characters" "Races"
text 0, 0, "Invalid Enemies File Format!"
else
`Read through all Enemy Types in the file.
EnemyList = xml find first element(xmlNo,rootValue,"Enemy","type","category")
while EnemyList > 0
`read Enemy type and categoryID
categoryName$ = xml attribute value by name (xmlNo, EnemyList, "type")
categoryID = val(xml attribute value by name (xmlNo, EnemyList, "category"))
if categoryName$ = spwnCatName$ and categoryID = spwnCatID
nameList = xml find first element (xmlNo, EnemyList, "Name", "name", "")
while nameList > 0
`Extract Enemy property information.
IDValue = val(GetChildElementValue(xmlNo, nameList, "ID"))
if IDValue = spwnCatEntityID
array insert at bottom Enemies()
local_index = array count(Enemies())
Enemies(local_index).CategoryName = categoryName$
Enemies(local_index).CategoryID = categoryID
Enemies(local_index).Name = xml attribute value by name (xmlNo, obj, "name")
Enemies(local_index).ID = val(GetChildElementValue(xmlNo, nameList, "ID"))
Enemies(local_index).Tile = val(GetChildElementValue(xmlNo, nameList, "Tile"))
Enemies(local_index).Level = val(GetChildElementValue(xmlNo, nameList, "Level"))
Enemies(local_index).BaseHP = val(GetChildElementValue(xmlNo, nameList, "BaseHP"))
Enemies(local_index).PDef = val(GetChildElementValue(xmlNo, nameList, "PDef"))
Enemies(local_index).PAtt = val(GetChildElementValue(xmlNo, nameList, "PAtt"))
Enemies(local_index).PDmgMin = val(GetChildElementValue(xmlNo, nameList, "PDmgMin"))
Enemies(local_index).PDmgMax = val(GetChildElementValue(xmlNo, nameList, "PDmgMax"))
Enemies(local_index).MDef = val(GetChildElementValue(xmlNo, nameList, "MDef"))
Enemies(local_index).MAtt = val(GetChildElementValue(xmlNo, nameList, "MAtt"))
Enemies(local_index).MDmgMin = val(GetChildElementValue(xmlNo, nameList, "MDmgMin"))
Enemies(local_index).MDmgMax = val(GetChildElementValue(xmlNo, nameList, "MDmgMax"))
Enemies(local_index).TreasureCode = GetChildElementValue(xmlNo, nameList, "TreasureCode")
Enemies(local_index).XPBase = val(GetChildElementValue(xmlNo, nameList, "XPBase"))
Enemies(local_index).Collides = val(GetChildElementValue(xmlNo, nameList, "Collidable"))
Enemies(local_index).isEnemy = val(GetChildElementValue(xmlNo, nameList, "isEnemy"))
exit
endif
nameList = xml find next element (xmlNo, nameList, "Name", "name", "")
endwhile
endif
EnemyList = xml find next element(xmlNo,rootValue,"Enemy","type","category")
endwhile
endif
endfunction local_index
I'm pretty sure the problem is with RandomEnemies array but can't see where or why.