[Solved]Bomb overlap assistance

Started by Zefk, July 21, 2018, 09:58:18 AM

Previous topic - Next topic
July 21, 2018, 09:58:18 AM Last Edit: July 21, 2018, 11:38:34 PM by Zefk
Okay, I want an entity to be disabled when a bomb is placed on it. I am guessing that it is not working because the bomb is not created before the start of the map.
Code ( lua) Select
--bomb test
sol.timer.start(1000, function()
    if bomb_1:overlaps(test) then
      test:set_enabled(false)
    end
  return true  -- To call the timer again (with the same delay).
end)


Error: In on_started: [string "maps/soulia_forest/soulia_forest.lua"]:57: attempt to index local 'bomb_1' (a nil value)



Yeah, I wouldn't put this test in map:on_started() necessarily. Is this something that only happens in this map? If so, maybe having a timer check over and over works, but it might make more sense to run the check code whenever you place a bomb.

Either way, what I would do is run some code that checks to see if both entities bomb and test both exist before trying to disable one. Try the method map:has_entities(), and if you get nil back, don't do the code.

If map:has_entitiy("bomb_1") and bomb_1:overlaps(test) then
...

July 21, 2018, 08:37:48 PM #2 Last Edit: July 21, 2018, 09:09:25 PM by Zefk
I actually tried map:has_entities() and creating the bomb beforehand. I thought it was the timer, but that was not the case after some more testing.

Code ( lua) Select
local x, y, layer = hero:get_position()
  local direction = hero:get_direction()
  if direction == 0 then
    x = x + 16
  elseif direction == 1 then
    y = y - 16
  elseif direction == 2 then
    x = x - 16
  elseif direction == 3 then
    y = y + 16
  end

  map:create_bomb{
    x = x,
    y = y,
    layer = layer
  }

--bomb test
sol.timer.start(1000, function()
    if map:has_entitiy("bomb_1") and bomb_1:overlaps(test) then
      test:set_enabled(false)
    end
  return true
end)


Error: In on_started: [string "maps/soulia_forest/soulia_forest.lua"]:80: attempt to call method 'has_entitiy' (a nil value)

I also tried:

Code ( lua) Select
sol.timer.start(1000, function()
for bomb in map:get_entities("bomb") do
  if bomb_1:overlaps(test) then
      test:set_enabled(false)
  end
end
  return true
end)


As well as...

Code ( lua) Select
sol.timer.start(1000, function()
  if map:has_entities("bomb") and bomb_1:overlaps(test) then
    test:set_enabled(false)
  end
  return true
end)


July 21, 2018, 11:38:01 PM #3 Last Edit: July 21, 2018, 11:45:40 PM by Zefk
Oh, this is a little hilarious. I forgot to add a name to the created entity. It works now.

Code ( lua) Select
  local x, y, layer = hero:get_position()
  local direction = hero:get_direction()
  if direction == 0 then
    x = x + 16
  elseif direction == 1 then
    y = y - 16
  elseif direction == 2 then
    x = x - 16
  elseif direction == 3 then
    y = y + 16
  end

  map:create_bomb{
    name = "bomb",
    x = x,
    y = y,
    layer = layer
  }

--bomb test
sol.timer.start(1000, function()
if map:has_entities("bomb") and bomb:overlaps(test) then
      test:set_enabled(false)
    end
  return true
end)





An easy way to do this is to allow to define a custom event map.on_bomb_created(bomb_entity). You can call it, if it exists, from the bomb metatable (just define the function bomb_metatable.on_created).
"If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you."