Could not find instance unknown script error but works as expected?

I am seeing some unusual behavior that I just don’t understand. This may not be the best way to do this in the first place, I am just experimenting to learn.

Here is my collection layout:
image

In station.script I have:

local aide = require "game.aide"

go.property("glow_color", vmath.vector4(0, 0, 1, 0.8))

local THRUST = "thrust"
local THRUST_SPRITE = "thrust#sprite-thrust"
local THRUST_SPRITE_GLOW = "thrust#sprite-thrust-glow"

function init(self)
  if go.get_parent() then msg.post(go.get_parent(), aide.MOVING_LISTENER) end
  self.move_input = { x = 0.0, y = 0.0 }

  -- go.animate("#sprite-center-glow", "tint", go.PLAYBACK_LOOP_PINGPONG, vmath.vector4(0,0,1,1), go.EASING_INOUTBOUNCE, 2)
  go.set("#sprite-center-glow", "tint", self.glow_color)
  go.set("#sprite-engine-glow", "tint", self.glow_color)

  go.set(THRUST_SPRITE, "tint", aide.HIDE)
  go.set(THRUST_SPRITE_GLOW, "tint", aide.HIDE)
end

function update(self, dt)
  if (self.move_input and (self.move_input.x ~= 0 or self.move_input.y ~= 0)) then
    local rot = math.atan2(self.move_input.x, -1 * self.move_input.y)
    local angle = 0.0
    if rot then angle = rot end

    go.set(THRUST_SPRITE, "tint", aide.SHOW)
    go.set(THRUST_SPRITE_GLOW, "tint", self.glow_color)
    go.set(THRUST, "rotation", vmath.quat_rotation_z(angle))
  else
    go.set(THRUST_SPRITE, "tint", aide.HIDE)
    go.set(THRUST_SPRITE_GLOW, "tint", aide.HIDE)
  end
end

function on_message(self, message_id, message, sender)
  if (message_id == aide.MOVING) then
    self.move_input = message.move_input
  end
end

It seems to work just fine when I build the game. I am basically passing whether player is moving through a message to the station script from player script. And in update I am showing/hiding a thrust animation depending on whether or not player is moving. It all works just fine in the game…

However, in my console it is spamming this:

ERROR:SCRIPT: game/player/station/station.script:31: could not find any instance with id '<unknown>'.
stack traceback:
  [C]:-1: in function set
  game/player/station/station.script:31: in function <game/player/station/station.script:21>

If I comment out 31/32 in station script (the lines that hide the thrust sprites), the error goes away but then the thrust is always visible now (because it is no longer being hidden obviously.) When those 2 lines are there, it disappears, as expected, when not moving but spams that error.

Line 17 on init (same statement, hiding the thrust) also has the same script error, yet works fine.

I must be missing something Defoldy here… but I just can’t seem to figure out what it is. If anyone can spot my problem, that’d be awesome. Feel free to suggest a more Defoldy way to do such a thing too. I’m just at a loss. :frowning:

I figured out the problem! :tada:

I had another ‘station’ game object on the main collection. So all the while it worked just fine, that station did not have a ‘thrust’ sibling or child and so was spamming the error. And since it was not receiving the moving message, it was always hitting the else section.

Think I’m going to have to optimize this a little anyway, because it doesn’t make much sense to show and hide the thrust every update, should be able to toggle it when the buttons are pressed/released I think.

1 Like

Yes! This is exactly the kind of solution that will give you the best result. Do as little as possible in your update() function and instead react to state changes such as key press and release.

1 Like