Calling several identical entities at once, in a script? (+collision_test)

Started by Eyth, July 04, 2018, 03:41:44 PM

Previous topic - Next topic
Hi there ;)

I saw this several times in other projects and don't know if its relatively simple to explain...
Lets say, I have 3 custom switches in the same room. And they are named "switch-1, -2, -3". I have a special ability which can activate those switches, but I dont want to tell script: When switch 1 is hit then activate and when switch 2 is hit - then activate... and so on.
How does it work in a script to tell him: When a switch, with name "switch-"+specific number (f.e. "2") is hit, then activate (only) "switch-"+specific number (f.e. "2")??
Or is that different evertime  ???

I don't know if I completely understand what you're asking- for one thing, switches automatically activate only themselves when you hit them, is this some kind of custom item? Maybe provide more details?

If this is like, a custom arrow or something, you probably have like, a custom entity collision test? In which case you're gonna have something like
Arrow:add_collision_test("touching", function(arrow, target)
Code
end)

Right?

Then you'd need code like, if target:get_type() == switch the target:set_activated(), I think? This is all if I'm right about what you're asking.

Thanks for the reply Max ;)

I found out, the function I looked for is map:get_entities([prefix]), with
"for entity in map:get_entities("your_prefix") do
-- some code related to the entity
end
In my case the switches themself are custom entities, because they behave different from normal switches. And wenn I have several of them in the same room, I also pass down the same function to all of them ^^


But your point is good and brings me to another question :P
If I have such a collision_test and want to test 2 custom entities, is that still possible? Because like you wrote in target:get_type() == switch, you test if it is an entity of type switch. But when I do target:get_type() == custom_entity, how does it know, that it is the one custom_entity I want and not every custom_entity on the map??
(In my case it would be a collision_test between the custom switch [custom_entity] and a custom_entity created by an item [custom_entity])

Thanks :)

Because the engine already knows exactly what map entity the collision is with (we're calling it target in this situation), you can do target:get_name() to get the name you have the map entitiy.