Solarus Forum

Solarus => Development => Topic started by: Diarandor on August 20, 2015, 06:30:44 pm

Title: How to delete only one collision test on custom entitiy
Post by: Diarandor on August 20, 2015, 06:30:44 pm
I requested for this feature in 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.
Title: Re: How to delete only one collision test on custom entitiy
Post by: Diarandor on August 20, 2015, 07:32:58 pm
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)
Code: [Select]
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:

-- 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)

-- Restart custom collision tests.
function game:restart_custom_collision_tests(entity)
  -- Stop all collision tests (done by the engine).
  -- 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)

-- 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.