How to delete only one collision test on custom entitiy

Started by Diarandor, August 20, 2015, 06:30:44 PM

Previous topic - Next topic
I requested for this feature in https://github.com/christopho/solarus/issues/743. Anyway, I got an easy idea that I will try to use to simulate this, as a workaround, using a script. I want to share the idea:

First, we create a function game:add_custom_collision_test(entity, collision_test_type, callback), which adds all the info of the collision test to the list "entity.custom_collision_tests = {}" (storing the variables collision_test_type and callback), and at the same time creates the collision test with the function of the engine. This function would return the "key" of the list where the info of this collision test is stored (so we can recover the info of the collision test with "entity.custom_collision_tests[collision_test]" where collision_test is the "key").

Then, with other function called game:clear_custom_collision_test(entity, collision_test) we would first clear all collision tests, set to nil the info of the collision test (with "entity.custom_collision_tests[collision_test] = nil"), and re-create again all the other collision tests with the info which remains in the list.

Anyway, if this function is implemented someday in the engine, I will delete my script.
"If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you."

August 20, 2015, 07:32:58 PM #1 Last Edit: August 20, 2015, 07:46:26 PM by Diarandor
It's still untested (I hope it works), and maybe useless for most of you. But anyway, here it is. (Put it in the script collision_test_manager.lua)

local game = ...

--[[
This script is used as a workaround to be able to delete only one collision test on a custom entity.
To use it write the following in the game manager:
sol.main.load_file("scripts/collision_test_manager.lua")(game)
--]]

-- Add a collision test to an entity. Assign a name to reference to it, in case we need to stop it later.
function game:add_custom_collision_test(entity, name, collision_test, callback)
  -- Create the list of collision tests if necessary.
  if not entity.custom_collision_tests then entity.custom_collision_tests = {} end
  -- Store the info of the collision test.
  local info = {collision_test = collision_test, callback = callback}
  entity.custom_collision_tests[name] = info
  -- Create the collision test (the engine does it).
  entity:add_collision_test(collision_test, callback)
end

-- Restart custom collision tests.
function game:restart_custom_collision_tests(entity)
  -- Stop all collision tests (done by the engine).
  entity:clear_collision_tests()
  -- Re-create all the collision tests stored on the list.
  for _, info in pairs(entity.custom_collision_tests) do
    entity:add_collision_test(info.collision_test, info.callback)
  end
end

-- Remove a collision test of an entity of a given reference name.
function game:clear_custom_collision_test(entity, name)
  -- Remove info of the collision test from the list.
  entity.custom_collision_tests[name] = nil
  -- Restart collision tests.
  game:restart_custom_collision_tests(entity)
end
"If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you."