Instance (null) not found (SOLVED)

Hi guys, I’m trying to store the positions of some factory spawned objects that are in a table…

if #self.runaway_aliens > 0 then
    local positions = {}
    for i,v in ipairs(self.runaway_aliens) do
		local url = msg.url(v)
		local p = go.get_position(url)
		print("stacker, input, runaway, position:", p.y)
		--table.insert(positions, p.y)
    end
end

I sometimes get this message…
/stacker_game/controller.script:196: Instance (null) not found
stack traceback:
[C]: in function ‘get_position’

The strange thing is that I’ve used the exact same for loop in other parts of the script (i.e. to get positions of spawns) and this message hasn’t occurred for any them. Any ideas about what’s going on?

Could it be that some of these runaway_aliens have been deleted?

1 Like

I’ve set things up so that the spawn hashes print to the screen when a runaway is added to the table and then I pprint just before I iterate through the table to see if all the instance are the same. They have always been the same so far. One thing I just noticed though is that sometimes one instance is added twice, which is strange because the instruction to insert an item into the runaway table is used only once in the script.

e.g.
1 = url: [stacker:/instance11#alien],
2 = url: [stacker:/instance15#alien],
3 = url: [stacker:/instance16#alien],
4 = url: [stacker:/instance11#alien],
5 = url: [stacker:/instance18#alien],
6 = url: [stacker:/instance19#alien],

Instance 11 is in there twice. But the amount of runaways is correct. Don’t know why there’s a repeated number. Also, even when there aren’t repeated numbers the same error still occurs, but not all the time. Anything else you might think could be causing the problem? Thanks

Note that there is no guarantee that the generated id is always incrementing. If you delete “instance11” and spawn a new instance, it may get id “instance11”. Can this be what has happened here?

3 Likes

Store the ids, not the urls. The url objects are treated as unique objects even though they refer to the same in-game url:

local url1 = msg.url("foo:/foo#foo")
local url2 = msg.url("foo:/foo#foo")
local urls = {
	[url1] = "Foo",
	[url2] = "Bar",
}
pprint(urls)

DEBUG:SCRIPT: 
{
  url: [foo:/foo#foo] = Bar,
  url: [foo:/foo#foo] = Foo,
}
2 Likes

Hi, I’m not sure that I understand, do you mean use something like go.get_id() instead of msg.url or to assign specific IDs manually?
When the aliens are first created this is the setup that I have…

local alien = factory.create("#alienfactory"...)
	local url = msg.url(alien)
	table.insert(self.alien_wave, alien)

Oh! table.insert() will insert whatever value you give it to the end of the array part of a table. It will not care about checking for duplicates and always insert at the end (unless you specify a third argument which is at the position you want the value to be inserted, shifting other values up). I see now in your previous post that this is indeed what you’re doing, but I overlooked that when I gave my answer.

And now that I re-read your question it’s clear what you’re asking:

And @sicher gave a probably reason for this. If you delete and spawn a game object the same frame it could be that the instance id is reused. Not sure if this explains your problem. If it doesn’t make sense to you then please share more of your code, what you are trying to do and what in your view is the the expected behaviour.

1 Like

cool thanks, I will take you up on that offer :slight_smile: Here’s what I’ve got so far…

  • It’s a stacker game. you’ve got to line the aliens up within the red boundary lines.
  • When you hit the space bar, Aliens within the boundary stay put
  • The ones outside the boundary breakoff and head toward the bottom of the screen
  • The boundaries are determined by how many aliens you kept within them on the previous turn
  • If you’ve gone through all the aliens in the pool, the ones on the block are destroyed and added to your score.

This is a mini-game so I want to get the position of aliens at the bottom of the screen (the runaways), store their positions in a table in a module, then when the standard game is reloaded which happens when you empty the alien pool, I want to spawn a commensurate number of runaway aliens there in the same positions. Everything seems to be working ok except for the getting the positions of the runaways. Here’s the last test that I ran, which seems typical of the results that I’ve been getting (stuff inside parentheses are annotations to explain what is happening, or at least what I think is happening) …

(I have a function that sets up the next wave, there were 2 aliens in this wave)
DEBUG:SCRIPT: stacker, control, alien_setup, line 27, pprint, wave…
DEBUG:SCRIPT:
{
1 = hash: [/instance15],
2 = hash: [/instance20],
}

(those 2 feel outside the boundaries and are added the runaway table)
DEBUG:SCRIPT: adding alien to runaway array hash: [/instance15]
DEBUG:SCRIPT: adding alien to runaway array hash: [/instance20]
DEBUG:SCRIPT: stacker, after redirect loop, line 133 runaways 6
DEBUG:SCRIPT:

(this is now all the aliens in the runaway table)
{
1 = url: [stacker:/instance10#alien],
2 = url: [stacker:/instance11#alien],
3 = url: [stacker:/instance15#alien],
4 = url: [stacker:/instance18#alien],
5 = url: [stacker:/instance15#alien],
6 = url: [stacker:/instance20#alien],
}

(no more aliens are left in the wave table)
DEBUG:SCRIPT: stacker, control, input, wave should have runaways removed, line 151…
DEBUG:SCRIPT:
{
}

(Mini game is coming to an end because there’s no more aliens in the pool. Just checking that the runaways table is still the same as last time we saw it, and that we haven’t deleted any of its aliens while we’ve been messing with other tables. It looks the same)
DEBUG:SCRIPT: input, end mini-game, runaways, pprint line 232…
DEBUG:SCRIPT:
{
1 = url: [stacker:/instance10#alien],
2 = url: [stacker:/instance11#alien],
3 = url: [stacker:/instance15#alien],
4 = url: [stacker:/instance18#alien],
5 = url: [stacker:/instance15#alien],
6 = url: [stacker:/instance20#alien],
}

(enter the for loop from my initial post, to try to get positions of runaways. Just getting the p.y on the test run, because I wasn’t sure if vectors can stored in a standard lua file. We get the first two positions. Then it crashes. Still can’t figure out why… )

DEBUG:SCRIPT: stacker, input, runaway, position: 580
DEBUG:SCRIPT: stacker, input, runaway, position: 600
ERROR:SCRIPT: /stacker_game/controller.script:238: Instance (null) not found
stack traceback:
[C]: in function ‘get_position’
/stacker_game/controller.script:238: in function </stacker_game/controller.script:97>

4 Likes

Thanks for your help guys. I found the problem. Sorry, silly mistake. I thought that I had changed a go.property for the runaway aliens to switch them to standard game movement and collision detection, but I actually re-spawned them from a factory and changed the property that way. Doh. A fair few hours were spent trying to find that bug, I think I need to be more systematic in my approach. My usual MO is to just hack stuff together on the spot until it works. Another valuable lesson for the books. Thanks again :+1:

2 Likes

Good to hear that you solved it!

1 Like