Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Itertools like class in gdscript - to aid procedural generation #9264

Closed
blurymind opened this issue Jun 19, 2017 · 12 comments
Closed

Itertools like class in gdscript - to aid procedural generation #9264

blurymind opened this issue Jun 19, 2017 · 12 comments

Comments

@blurymind
Copy link

blurymind commented Jun 19, 2017

Basically I stumbled on an issue that requires me to return an array that holds all possible combinations of a sequence.
I filed it as a question here:
https://godotengine.org/qa/15813/function-that-returns-all-possible-combinations-sequence

Class1 = [1,2,3,4] Class2 = [1,2,3,4] Class3 = [1,2,3,4]

Now what I would like to do is return all possible combinations of
these three classes.

Example:

1 1 1
2 1 1
3 1 1
4 1 1
1 2 1
2 2 1
3 2 1
4 2 1
...

That functionality is available in python - as a module called itertools.
Trying to write a function that does what I want in gdscript is proving to be dificult without the product itertools module.

Doing that in python would be as simple as:

import itertools
iterables = [ [1,2,3,4], [88,99], ['a','b'] ]

for t in itertools.product(*iterables):
     print t

That module would be incredibly useful when you want to automatically generate enemies with different types of armor for example - as it can create an array with all possible combinations of boots + helmets + chest for example

In my case, I am very close to writing a tool for godot that can aid artists and my tool needs that functionality. But I can see this being useful for games too

@blurymind
Copy link
Author

Looking at itertools, this is how the product function there looks like:
https://docs.python.org/3/library/itertools.html#itertools.product

def product(*args, repeat=1):
    # product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
    # product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
    pools = [tuple(pool) for pool in args] * repeat
    result = [[]]
    for pool in pools:
        result = [x+[y] for x in result for y in pool]
    for prod in result:
        yield tuple(prod)

@blurymind
Copy link
Author

blurymind commented Jun 20, 2017

For the record, I came up with a solution to this in godot, but it is kind of ugly.
It generates a custom script with nested for loops and runs it.

Another users suggested a better solution, but it is still quite a long function.
Here is what we have so far:
https://godotengine.org/qa/15813/function-that-returns-all-possible-combinations-sequence

In any case, it would be awesome if somebody implements this in c++ as a built in function in godot.
:)

I hope that these code snipets help

@hubbyist
Copy link

hubbyist commented Jun 28, 2017

Another solution:

extends SceneTree

func _init():
	var a = ['a1','a2','a3']
	var b = ['b1','b2']
	var c = ['c1','c2','c3']
	var d = ['d1','d2','d3']
	var arrays = [a,b,c,d]
	print(arrays)
	for combination in array_combinations(arrays):
		print(combination)
	quit()

func array_combinations(arrays):
	var combinations = []
	var first = arrays.front()
	arrays.pop_front()
	for item in first:
		combinations.push_back([item])
	for array in arrays:
		var sequences = [];
		for item in array:
			for combination in combinations:
				var sequence = combination + [item]
				sequences.push_back(sequence)
		combinations = sequences;
	return combinations

@blurymind
Copy link
Author

blurymind commented Jun 28, 2017 via email

@kubecz3k
Copy link
Contributor

kubecz3k commented Apr 3, 2018

First of all thank you for your report and sorry for the delay.

We released Godot 3.0 in January 2018 after 18 months of work, fixing many old issues either directly, or by obsoleting/replacing the features they were referring to.

We still have hundreds of issues whose relevance/reproducibility needs to be checked against the current stable version, and that's where you can help us.
Could you check if the issue that you described initially is still relevant/reproducible in Godot 3.0 or any newer version, and comment about its current status here?

For bug reports, please also make sure that the issue contains detailed steps to reproduce the bug and, if possible, a zipped project that can be used to reproduce it right away. This greatly speeds up debugging and bugfixing tasks for our contributors.

Our Bugsquad will review this issue more in-depth in 15 days, and potentially close it if its relevance could not be confirmed.

Thanks in advance.

Note: This message is being copy-pasted to many "stale" issues (90+ days without activity). It might happen that it is not meaningful for this specific issue or appears oblivious of the issue's context, if so please comment to notify the Bugsquad about it.

@akien-mga
Copy link
Member

Our Bugsquad will review this issue more in-depth in 15 days, and potentially close it if its relevance could not be confirmed.

As there was no update since the previous post, we close this issue.

If it is still relevant in the way it was described and discussed above, please comment to ask for it to be reevaluated. If it is only partly relevant, or if the original issue has changed, it would be better to open a new issue (potentially referring to this one) focused on the current state.

@Zireael07
Copy link
Contributor

I believe this is still relevant, reopen?

@akien-mga
Copy link
Member

This proposal was discussed a lot a few months ago, as it had slipped into our ideas list for the Google Summer of Code.

After much discussion, we could not really see a lot of use cases that would warrant integrating it directly in GDScript, so we removed it from our ideas list. We try to keep GDScript as simple as possible, which means cutting on "sometimes useful" features to focus on what is really needed.

Since this can't satisfy everyone of course, the best approach would be to implement such features as a GDScript or GDNative plugin, and usage will speak for itself. If many users use such plugin extensively in their games, then it might be considered to be included in the core for convenience.

@frojo
Copy link

frojo commented Jun 11, 2019

Is it possible that a class with custom iterators would work for this usecase?

(I know this issue is closed, this is mostly for people in the future looking for iterator-related solutions)

@tanjunior
Copy link

Is it possible that a class with custom iterators would work for this usecase?

(I know this issue is closed, this is mostly for people in the future looking for iterator-related solutions)

curious about this too, anyway to use custom iterators to do number permutations

@Davo00
Copy link

Davo00 commented Jun 16, 2023

This proposal was discussed a lot a few months ago, as it had slipped into our ideas list for the Google Summer of Code.

After much discussion, we could not really see a lot of use cases that would warrant integrating it directly in GDScript, so we removed it from our ideas list. We try to keep GDScript as simple as possible, which means cutting on "sometimes useful" features to focus on what is really needed.

Since this can't satisfy everyone of course, the best approach would be to implement such features as a GDScript or GDNative plugin, and usage will speak for itself. If many users use such plugin extensively in their games, then it might be considered to be included in the core for convenience.

Or I'll just use a decent programming language with a broad range of libraries ;)

@akien-mga
Copy link
Member

Please don't comment on old issues if you don't have anything constructive to add.

@godotengine godotengine locked as resolved and limited conversation to collaborators Jun 16, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

9 participants