hi, I’m creating a game and wanted to know how to detect an instance collision and retrieve the animation, for example create a condition that executes an action only if object 1 collides with an object 1 instance and that instance has a specific animation.
something like this (this is just an example)
Hi! You could set a variable for it then set an event to change the variable according to what animation the chosen object is set to.
There are probably other ways to do it but here’s some examples. And you did mention it was for a specific animation… So I think you’ll need the third one here.
Can you be more detailed? What’s the ultimate goal? There’s a condition to check for the number of instances that are picked. It depends on your needs. Conditions work like filters. They pick matching objects. You can check the number of objects in a scene or the number currently picked.
Objects are in collision
animation equals something
picked instances count equals 2
Or if you want to make sure there are only 2 objects total with the animation.
Objects are in collision
picked instances count equals 2
animation equals something
picked instances count equals 2
That would check if there were 2 objects colliding and 2 objects with a particular animation.
There might be a better way. It depends on what you’re making.
If I may venture a guess, I suppose the idea might be that if one wire that’s “live” and playing an electric animation, and another wire comes into contact with it, there would be an effect. If that were the case, I guess my initial reply might need each instance to have a specific ID variable. But yes, there may be other ways to get it done.
I’m actually making a system slightly similar to minecraft’s redstone where I have an object called wire that when it comes into contact with an Energy source like a battery will turn red, now I’d like it if I place another wire object next to a first one that is red it will turn red too.
here’s an example that explains it a little closer.
Ah there it is. Lol. Well, Keith was right. You just need two object variables for each instance of wire. An ID variable, and a variable for if they’re powered or not.
See if one with a certain ID number collides with another with a different ID number. Then check variables if they’re powered. The animation check could work too, but you decide which is more convenient or which is more efficient.
Though I’d prefer the powered variable method since animation check method would still require another extra variable, so yeah.
And there may still be other ways… I can’t think of any at the moment…
Edit: the ID variable thing may also need an extra object to be placed in the same spot as each of the wire objects. These will be used to compare the IDs.
Are the blocks snapping together or snapping to a grid? Point is inside might be easier or even the linked objects behavior. It depends on the complexity of your project. Do they only get placed on a certain side of the battery or all 4 sides? Is there 1 battery or many? What is the goal?
I did something similar with the classic pipe puzzle game to connect pipes to a water source. The check needed to start at the water source and go outwards.
It’s not as hard as it sounds but I’m not going to go into more details because it might be overkill for your needs. I’m imagining batteries placed anywhere with wire blocks that snap to a grid and can be placed on any side of the batteries or any side of the wires.
Here is a test project. Even if it’s not exactly what you need, it should help.
project (click the green [code] button and select [download zip] extract and open the JSON file)
https://github.com/doug13579/gdevelop-wire-battery-example
Try me: All objects are draggable.
https://gd.games/keith_13579/batteries
This uses an array to hold the touching objects.
Start by adding an ID to the objects
Snap the objects to a grid and trigger a check
To check, you add the battery IDs to an array. You then go through the array and position a plus shaped test object. Check the collision between the test object and the wires. If they’re in collision then change the animation to “on” and add them to the array.
test object
It uses 3 objects, 1 group that has the battery and the wire object.
It works just like I wanted it to
I’m glad it’s working. I continued to play with it. I got rid of the object IDs and instead it adds the objects x,y to the list array. That way the objects don’t need any setup.
I also added switches and lightbulbs just for fun. The switches are toggled with the right mouse button because they’re draggable. I’ll probably change this. The lightbulbs don’t transfer the power to other objects. I also made it update the state of the objects when a block is dragged. I like this.
The events are a bit of a mess. I’m torn between using the object name to identify different object types or using a bunch of Boolean variable states like isLight or transfersPower or IsSwitch. I added a dragging animation. I don’t like that either. I might make that a Boolean.
try me:
https://gd.games/instant-builds/bfdd0cb2-8020-4ef1-a8b5-4da31bae765f
New project files. I left the previous repository.
https://github.com/doug13579/gdevelop-wire-battery-example-2
that’s a great idea👌
you should make it an extension in terms of configuration and other it might be easier.
I’m not sure what type of extension it would be. I’m still playing with the concept. I added a drag layer, so the tile being dragged doen’t go behind the other tiles. It’s easier than playing with the Z-order. It’s pretty straight forward.
It needs to be completely reworked. I don’t like having different tile objects. I think I’ll move all of the animations to 1 object type. That will take work.
I’m also moving the tiles to a tray. The space key toggles the switch and the delete key deletes a tile. You can also drop the tile back on the tray to delete the tile.
https://gd.games/instant-builds/78585652-2670-4caa-a995-f7779168dc65
Source:
https://github.com/doug13579/gdevelop-wire-battery-example-3