Solarus-Games English Forum

Solarus => Development => Topic started by: Eyth on July 04, 2018, 03:41:44 PM

Title: Calling several identical entities at once, in a script? (+collision_test)
Post by: Eyth on July 04, 2018, 03:41:44 PM
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  ???
Title: Re: Calling a specific entity out of several identical, in a script??
Post by: Max on July 06, 2018, 08:13:27 PM
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.
Title: Re: Calling several identical entities at once, in a script? (+collision_test)
Post by: Eyth on July 09, 2018, 10:42:55 AM
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 :)
Title: Re: Calling several identical entities at once, in a script? (+collision_test)
Post by: Max on July 09, 2018, 04:06:11 PM
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.
Title: Re: Calling several identical entities at once, in a script? (+collision_test)
Post by: Eyth on July 10, 2018, 04:09:02 PM
Nice, that's helpful - thanks very much ;)