Post

Code Monkey
Code Monkey@UnityCodeMonkey·
Is there a problem with this code? private void SetCameraFollowFunc(Func cameraFollowFunc) { } SetCameraFollowFunc(() => new Vector3(1, 0, 0)); A) No B) Should be: SetCameraFollowFunc(() => return new Vector3(1, 0, 0)); C) Should be: SetCameraFollowFunc((Vector3(1, 0, 0)) => { }); (answer in reply below)
English
3
0
10
2.1K
Code Monkey
Code Monkey@UnityCodeMonkey·
#### ANSWER #### #### ANSWER #### #### ANSWER #### #### ANSWER #### #### ANSWER #### #### ANSWER #### #### ANSWER #### #### ANSWER #### #### ANSWER #### #### ANSWER #### #### ANSWER #### #### ANSWER #### (A) There's no error so A) is correct, but option B) is also valid. The compiler automatically adds return when receiving a delegate into a Func parameter so it's not necessary but works both ways. The technical term for the inline function is a Lambda expression, super useful and compact learn.microsoft.com/en-us/dotnet/c… Func and Action are types of delegates which can be super useful, I covered them in detail here: ##REF##video_small, 3ZfwqWl-YI0, What are Delegates? (C# Basics, Lambda, Action, Func)##REF## Also, this particular pattern, setting a Camera class to receive a Func instead of a single Vector3, is a super easy way to make your Camera (or other objects) follow something that moves. For example this is how in my game Dinky Guardians I handle a lot of the tutorial logic which shows an arrow dynamically pointing to a Dinky which is moving. The Arrow indicator class receives a Func which it then executes on every Update to figure out what position to point to. ##REF##video_small, 7m2EJYFDgWI, My game is RELEASED! I hope you like it!##REF##
English
0
0
3
538
Share