4ian / GDevelop

🎮 Open-source, cross-platform 2D/3D/multiplayer game engine designed for everyone.
https://gdevelop.io
Other
10.83k stars 849 forks source link

"For each layer" event type #3331

Closed Elairyx7301 closed 2 years ago

Elairyx7301 commented 2 years ago

Short description

A "for each layer" event type that iterates through all layers, saving the current layer name in a scene variable (like the "for each child variable" event type does it) for users to easily get all layers automatically.

Description

With the GDevelop "code blocks" it's possible to iterate through all instances, array/structure children etc. but to get all existing layers requires a JS block which I believe many users aren't familiar with (especially with the PixiJS- and GDevelop syntax and -specific actions) and would be opposite to the "with no programming language to learn" slogan.

While making a camera zoom extension where this event type would have saved me some time, I see other applications for this event as well. For example showing all layers, rotate (angle) camera on all layers (to create a rotation effect for an upside-down game etc.) and enable/disable layer effects for all layers.

Current method

Note that even though this isn't much code, I find it difficult to build working logic on top of it.

const allLayerNames = new Array(); //Create array for all layers
runtimeScene.getAllLayerNames(allLayerNames); //Add all layers to the array

allLayerNames.forEach(layerName => //Iterate through all layers
{ //...
})

Solution suggested

A "for each layer" event type that iterates through all layers, saving the current layer name in a scene variable to access all layers / iterate through them. (from a code perspective, this event might be similar to the "for each child variable" event type)

4ian commented 2 years ago

I'm a bit sceptical about making this easier to do. Layers are usually not present in a large number in games - a few for the UI, for the game, for the background. So iterating on them is an unusual operation. I can see the need for sometimes doing a lot of similar operation on a multiple layers (like change the zoom/angle of 4 or 5 layers). But in this case, I would recommend to pack these actions into an extension dedicated to your game, in a custom action :) Because it's quite possible that some games have layers that they don't want an extension to touch.

The "for each layer" solution here would also mean exposing a very specific concept (layers) to a very generic place (the events, which are supposed to define the structure to create the logic of your game) - so it feels a bit out of place.

An alternative could be to expose a way to 1) get the total number of layers and 2) an action to get the name of the layer at a given index. So you could iterate on them using a For Each event - it would be the equivalent of the JS code you exposed. Not as easy - but again this is not necessarily something that we want to make easy.

Sometimes less is more, and in the case of layer I'm hesitant to give more power through events because iterating is something you want to avoid in most cases. Let me know if you have more specific use cases in mind though! :)