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

Support for multiple return variables #23055

Closed
Jummit opened this issue Oct 16, 2018 · 10 comments
Closed

Support for multiple return variables #23055

Jummit opened this issue Oct 16, 2018 · 10 comments

Comments

@Jummit
Copy link
Contributor

Jummit commented Oct 16, 2018

It's really not that complicated, here is how it works in Lua (which GDscript is inspired by):

function return_three_values()
    return 1, "string", {"an array"}
end

an_int, a_string, an_array = return_three_values()

Currently you would just create an array and put your variables in there, but there are three issues with that:

  1. You have to pull them out of the array again
  2. Its not as easy to read
  3. Its not typed
@Jummit Jummit changed the title [Feature Request] Support for multiple return variables Support for multiple return variables [Enhancement] [topic:gdscript] Oct 16, 2018
@Chaosus
Copy link
Member

Chaosus commented Oct 16, 2018

in Lua (which GDscript is inspired by):

I think, GDScript was inspired by Python where this feature as in several other languages, called "tuple"

@Chaosus Chaosus changed the title Support for multiple return variables [Enhancement] [topic:gdscript] Support for multiple return variables Oct 16, 2018
@Jummit
Copy link
Contributor Author

Jummit commented Oct 16, 2018

in Lua (which GDscript is inspired by):

Actually GDScript was inspired by Python where this feature as in several other languages, called "tuple"

It also has dictionaries, which are inspired by Lua's, but I guess multiple return variables is a very common thing.

@OvermindDL1
Copy link

It also has dictionaries, which are inspired by Lua's

They are? They seem extremely similar to Python's dictionaries. Lua instead has Tables, which though conceptually similar to Dictionaries have some pretty fundamental differences that GDScript doesn't seem to have?

@ghost
Copy link

ghost commented Oct 16, 2018

I'm neutral on this, but just adding that I've found dictionaries to be more convenient than arrays for returning multiple values. Mostly since it doesn't have a strict ordering of indices to remember, or need for extra work to create constants or enums to make readable labels for the indices.

func get_data():
	# Do stuff
	return {
		player = get_player(),
		health = get_health(),
		score  = get_score()
	}

# Elsewhere

var data = get_data()

UI.set_health(data.health)
UI.set_score(data.score)

@Jummit
Copy link
Contributor Author

Jummit commented Oct 17, 2018

It also has dictionaries, which are inspired by Lua's

They are? They seem extremely similar to Python's dictionaries. Lua instead has Tables, which though conceptually similar to Dictionaries have some pretty fundamental differences that GDScript doesn't seem to have?

Oh, It seems like I have overlooked something there. GDscript also supports the lua syntax of dictionaries:

var table = {
    key = "value"
}

instead of pythons

var table = {
    "key" : "value"
}

http://docs.godotengine.org/en/3.0/getting_started/scripting/gdscript/gdscript_basics.html#dictionary

@OvermindDL1
Copy link

Yep, it supports different syntaxes, but it still 'works' like Python Dictionaries and not Lua Tables.

@nathanfranke
Copy link
Contributor

I would opt for python/c# style tuples

func get_data():
    return (get_player(), get_health(), get_score())

var player
var health
var score
(player, health, score) = get_data()

Also possible support for something like a, b, c = 5, 6, 7 as in python

@atbigelow

This comment has been minimized.

@Calinou
Copy link
Member

Calinou commented Apr 10, 2020

@atbigelow Please don't bump issues without contributing significant new information. Use the 👍 reaction button on the first post instead.

@clayjohn
Copy link
Member

Feature and improvement proposals for the Godot Engine are now being discussed and reviewed in a dedicated Godot Improvement Proposals (GIP) (godotengine/godot-proposals) issue tracker. The GIP tracker has a detailed issue template designed so that proposals include all the relevant information to start a productive discussion and help the community assess the validity of the proposal for the engine.

The main (godotengine/godot) tracker is now solely dedicated to bug reports and Pull Requests, enabling contributors to have a better focus on bug fixing work. Therefore, we are now closing all older feature proposals on the main issue tracker.

If you are interested in this feature proposal, please open a new proposal on the GIP tracker following the given issue template (after checking that it doesn't exist already). Be sure to reference this closed issue if it includes any relevant discussion (which you are also encouraged to summarize in the new proposal). Thanks in advance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants