Bad argument #8 to collectionfactory.create? (SOLVED)

#1

Hello everyone, I am trying to figure out a strange error in my game. I have a code line here:

self.level_collection = collectionfactory.create("#mmap1factory", nil, nil, { sub_level = 5 }, 1)

Which is causing the following error message:

ERROR:SCRIPT: /maps_dungeons/dungeon1/dungeon1_main.script:21: bad argument #8 to 'create' (hash expected, got string)
stack traceback:
	[C]: in function 'create'
	/maps_dungeons/dungeon1/dungeon1_main.script:21: in function 'load_map'
	/maps_dungeons/dungeon1/dungeon1_main.script:46: in function </maps_dungeons/dungeon1/dungeon1_main.script:34>

The error refers to a bad argument #8. The prototype for the function in the editor only shows 5 possible arguments, the last 4 which are optional. I have no idea what’s going on here. How can the eighth argument be bad if there are only 5 possible arguments?

0 Likes

#2

It’s hard to give suggestions without an example. It seems like some weird arguments are being used.

0 Likes

#3

I don’t really use this function but the documentation leads me to believe that your 4th parameter is the issue:

{ sub_level = 5 }

Per the documentation:

Script properties in the created game objects can be overridden through a properties-parameter table. The table should contain game object ids (hash) as keys and property tables as values to be used when initiating each spawned game object.

The example lists the properties parameter as follows:

  local props = {}
  props[hash("/enemy_leader")] = { health = 1000.0 }
  props[hash("/enemy_1")] = { health = 200.0 }
  props[hash("/enemy_2")] = { health = 400.0, color = hash("green") }

The error message is consistent with this (hash expected, got string). The only mystery is why it’s referring to argument #8.

3 Likes

#4

Thanks, this makes sense to me. I’ll have to figure out another way to pass some starting values to the collection script itself during init.

1 Like

#5

@topbraj gave a good example on how to do that, from the documentation.

1 Like

#6

There is no collection script. Collections don’t exist during runtime. The script you are after is attached to one of the game objects within the collection you are spawning, and you need to specify this game object when passing the parametres.

I believe this should work:

self.level_collection = collectionfactory.create("#mmap1factory", nil, nil, { [hash("/game_object_name")] = { sub_level = 5 } }, 1)
1 Like

#7

Thanks, yeah I understand it now. I have a top level object “map” in my collection but I’ve made so many of these now I was viewing that as the collection itself :stuck_out_tongue:

1 Like