Have some problem to access a property after in spawned collection script instance

Hello everyone. I have following field.script

    go.property("left_field"  , hash("unknown"))
    go.property("right_field" , hash("unknown"))
    go.property("top_field"   , hash("unknown"))
    go.property("bottom_field", hash("unknown"))

and this script is attached to root gameobject in collection

then in another collection i have controller which spawn this fields and then assign neighbors

       for x = 0, board_width - 1 do
		pos.x = cell_size / 2 + cell_size * x 
		self.board[x] = {}
		for y = 0,  board_height -1 do
			pos.y =  cell_size / 2 + cell_size * y
			local all_ids = collectionfactory.create("#field_factory", pos, null, {} )									
			self.board[x][y] = {all_ids = all_ids, field = all_ids[hash("/field")], field_url = msg.url(all_ids[hash("/field")])}
		end
	end    	
	for x = 0, board_width - 1 do		
		for y = 0,  board_height -1 do		
			go.set(self.board[x][y].field_url, hash("left_field"), get_field_at(self, x - 1, y))
			go.set(self.board[x][y].field, hash("right_field"), get_field_at( self, x + 1, y))
			go.set(self.board[x][y].field, hash("bottom_field"), get_field_at(self, x, y - 1))
			go.set(self.board[x][y].field, hash("top_field"), get_field_at(self, x, y + 1)) 			 
		end
	end

and then i get an error when trying to assign value (go.set(…))

ERROR:SCRIPT: board/board.script:38: ‘[default:/collection0/field]’ does not have any property called ‘left_field’

Ok, so the problem here is that the URL passed to go.get() and go.set() must reference the script specifically and not the game object. I have updated an existing factory and properties example I created a while back to also show how to modify properties of spawned game objects. The example can be seen here:

And try it here (up/down to modify property ‘move_speed’ of spawned game objects): http://britzl.github.io/publicexamples/factory_and_properties/index.html

2 Likes

Thanks a lot.

With your help I’ve solved the problem. here are some more details.

  1. you need to be very carefully. property is belongs to script. and if you check the collection you will see that.


    you can see that field (gameObject) doesn’t have these properties, they located only on script

  2. After you spawn this field collection from another collection you get an array with all references to the object so what you need to do is take gameobject url with your script. you can do it like this

    local all_ids = collectionfactory.create(“#field_factory”, pos, null, {} )
    local field_ctrl_url = msg.url(all_ids[hash(“/field”)])

  3. now you have gameobject so what you need to do is just add msg.url[fragment] to it

    field_ctrl_url.fragment = “script”

and if you _print(field_ctrl_url.fragment) variable you will see that it equals to something like

“url: [default:/collection0/field#script]”

see (right part after the ‘#’ sign) and this thing indicates that we will [get|set] data from script and not the gameobject

  1. so now your code will works with line like this (no hash for property name required)

    go.set(self.board[y].field_ctrl_url, “left_field” , get_field_at(self, x - 1, y))

4 Likes