Multiple Reference Variables returned (byref) in Functions and Subroutines #9164
Replies: 3 comments 1 reply
-
The influences of GDScript are mainly Python and Lua which do not have this feature. Furthermore, from what I've heard this might not be straightforward to add to GDScript. On the flip side having to maintain references to caller local variables would have made In fact, in GDScript captures are not treated as the same variable:
Outputs:
See: #8558 Our workaround is to pass an
Or:
Outputs:
This is similar to the workaround of passing a list in Python. Let us say we want to make Syntactic sugar for the parameter declaration is easy: But on the caller side it is not so much because they mean to use a different transformation depending on the parameter of what is being called, and that might not be known. Furthermore, if I want to pass Right now, I believe the path for GDScript would be along the lines of returning tuples (#2944) or shaped arrays/dictionaries (what reduz is calling "structs'' #7329). The syntactic sugar for that would be much easier to add to GDScript, but it is not here yet. And yes, you can return an Anyway, I gave you a workaround, hopefully that is good enough for you. On the flip side, yes GDScript has optional parameters by specifying the default value (there is no concept of missing) but unlike other languages the default value does not have to be a constant, it can be an expression evaluated on runtime! |
Beta Was this translation helpful? Give feedback.
-
Thank you Theraot for writing that. It makes me feel like my arms are chopped off without instant variable byref's. The top mini code above is a bit over my head so I'll look more into it. I get the last one though. I was using something along the lines of (all pseudo code below) _update_variables(A):{Function that receives an array} Same as above, VB scenario So, is that what I need to do from now on? Stage all data into an array then unpack it later, just to get byref functionality? |
Beta Was this translation helpful? Give feedback.
-
Thanks again Theraot. I'm very familiar with VB6 classes, so I'll try to figure out what GDScript classes are and how they are different. One person was already saying that might be the best workaround solution in this case. I mostly used classes for interface components though. I'll have to wrap my head around 'callable' things, though, I can't percieve how in my case variables as functions would work. Per, say a monster attack, A crap tonne of info is transported down, Character1 & Character2's entire SQL instances, then clothing/limb and physical/limb data and so on, and so on. All of that gets managed in modules-subs-functions depending on the attack etc. And all those functions call up other subs to work out data/situations on the fly (unrelated to Characters, ie: weapons, armour data etc). Anyhow, you've given me some things to dig around with, many thanks! Ah, I know nothing of VB.Net, other than it's poison. petwooehy. Cheers |
Beta Was this translation helpful? Give feedback.
-
Hi, total Newbie for GDScript, strong VB6 coder.
In VB we have variables passed by reference to functions and subs as the default.
I'm requesting a discussion or implementation of a commonly available operation like VB6 has in regards to handling multiple referenced variables in Subs and Functions.
VB6 Example: Printing x,y,z after updating them via a function (passed by reference)
Sub Start_Program
Dim x=1 as integer: Dim b="My String" as string: Dim c =2.4 as single {Declare variables}
call do_something( x, y, z) {Call the function}
print(x,y,z) {Print the returned result}
end {End program}
Sub _do_something( byref A as integer, byref B as string, byref C as single )
A = A + 1
B = B & " added to string"
C = C + 5.6 {Adjust all given entries}
Exit Function
Result from printing x,y,z:
2, My String added to string, 8.0
The alternative way to do this in VB is: (as byref is the default) 'By reference' {ie: no need to declare}
_do_something( A as integer, B as string, C as single )
To send a copy of the values without returning them in VB is via this: (byval) 'By value'
_do_something( byval A as integer, byval B as string, byval C as single )
When you have 'several layers deep' function calls, sending byref variables is absolutley necessary, for example
Manipulating piercing damage to hand armour, body armour, arm armour all at once, but then pushing those variables further down into other functions because you need to add ice damage, then blunt damage, then acid damage. Or some other deeper layer and pertinent calculation (inside the Piercing Damage function). Eventually you recieve all armour sections updated without having to go through convoluted staging.
Ie:
call _add_piece_damage(MyArmour(a),MyArmour(b),MyArmour(c))
call _add_ice_damage(MyArmour(a),MyArmour(b),MyArmour(c))
The reciever sub or function looks like this:
Sub _add_piece_damage( byref ArmourPart1 as int, byref ArmourPart2 as int, byref ArmourPart3 as int)
Likewise the same style for others
Sub _add_ice_damage( byref ArmourPart1 as int, byref ArmourPart2 as int, byref ArmourPart3) {Note ArmourPart3 is a 'variant' here}
Functions and Subs behave in exaclty the same manner, but the Function usually returns a set 'thing' as well.
Ie: x = _call_my_function(a,b,c) {a,b,c defined earlier}
Function _call_my_function(g,h,i) as single {will return a single type value, in VB, all of these variables are byref(!)}
_call_my_function = g + h + i + 10.5
exit Function
With Godot sending value only variables, I've found this irregular and limiting.
For example, below is a Function that returns a colour pixel value, AND, updates any coordinate system given to Map_X & Map_Y which are Absolute Coordinates. (Those coordinates are then used in further functions. The function accepts ANY coordinate system initially too, Print_X & Y are mouse click locations, Tile_Number is an isometric tile on the map)
Public Function Return_Pixel_Colour_From_Layer_From_Any_Coord_Type(ByVal My_Map_Layer As String, Optional ByVal Print_X As Single = 0, Optional ByVal Print_Y As Single = 0, Optional ByRef Map_X As Integer = 0, Optional ByRef Map_Y As Integer = 0, Optional ByVal Tile_Number As Integer = 0) As Long
I don't know if Godot uses 'Optional' yet, but it is super handy in VB.
Having and parsing Byref variables into functions and subs is common, I'm just not certain as to why Godot doesn't have this straight up?
Beta Was this translation helpful? Give feedback.
All reactions