Author Topic: [Solved]Bomb overlap assistance  (Read 594 times)

Zefk

  • Hero Member
  • *****
  • Posts: 536
  • Just helping Solarus
    • View Profile
    • Zelzec
[Solved]Bomb overlap assistance
« on: July 21, 2018, 09:58:18 am »
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)

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


Max

  • Full Member
  • ***
  • Posts: 222
    • View Profile
Re: Bomb overlap assistance
« Reply #1 on: July 21, 2018, 05:46:04 pm »
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
...

Zefk

  • Hero Member
  • *****
  • Posts: 536
  • Just helping Solarus
    • View Profile
    • Zelzec
Re: Bomb overlap assistance
« Reply #2 on: July 21, 2018, 08:37:48 pm »
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)

Code: [Select]
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)

Zefk

  • Hero Member
  • *****
  • Posts: 536
  • Just helping Solarus
    • View Profile
    • Zelzec
Re: [Solved]Bomb overlap assistance
« Reply #3 on: July 21, 2018, 11:38:01 pm »
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)




Diarandor

  • Hero Member
  • *****
  • Posts: 1047
  • Cats are cool! (ΦωΦ)
    • View Profile
Re: [Solved]Bomb overlap assistance
« Reply #4 on: July 22, 2018, 12:38:56 am »
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.”