Last active
November 16, 2024 09:40
-
-
Save tomlarkworthy/11e0d99f69761ca5dd248e67fd398faf to your computer and use it in GitHub Desktop.
Example of the Stateless Scene pattern (see https://corepox.net/devlog/unity-pattern:-stateless-scenes)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void loadMyScene(MySceneParams params, System.Action<MySceneOutcome> callback) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public class MySceneBehaviour: MonoBehaviour { | |
private static MySceneParams loadSceneRegister = null; | |
public MySceneParams sceneParams; | |
public static void loadMyScene(MySceneParams sceneParams, System.Action<MySceneOutcome> callback) { | |
MySceneBehaviour.loadSceneRegister = sceneParams; | |
sceneParams.callback = callback; | |
UnityEngine.SceneManagement.SceneManager.LoadScene("MyScene"); | |
} | |
public void Awake() { | |
if (loadSceneRegister != null) sceneParams = loadSceneRegister; | |
loadSceneRegister = null; // the register has served its purpose, clear the state | |
} | |
public void endScene (MySceneOutcome outcome) { | |
if (sceneParams.callback != null) sceneParams.callback(outcome); | |
sceneParams.callback = null; // Protect against double calling; | |
} | |
} | |
[System.Serializable] | |
public class MySceneParams { | |
public System.Action<MySceneOutcome> callback; | |
// + inputs of the scene | |
} | |
public class MySceneOutcome { | |
// + outputs of the scene | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void testMyScene() { | |
MySceneBehaviour.loadMyScene(new MySceneParams(/* ... */), (outcome) => { | |
Debug.Log("Scene over " + outcome.ToString()); | |
}); | |
} | |
Cool. It's not perfect but it makes more sense to me and it definitely
composes better in practice
…On Thu, Mar 12, 2020, 1:16 AM Timothy Klopotoski ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
I read "a functional approach"
<https://gamedev.stackexchange.com/a/178564> and I got excited. I'm very
new to Unity and just use it for fun. But in my day job I do a lot of
functional coding, so I find it odd that I can't (for example) pass any
context (other than a starting position and parent) to objects that I
create with Instantiate.
So far I've been going with the static-singleton approach
<https://gamedev.stackexchange.com/a/110963> but started looking at
representing everything as addressables instead, and global state became
problematic immediately. I don't even *like* global state, so this might
be a worthy alternative. I will give it a try.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<https://gist.github.com/11e0d99f69761ca5dd248e67fd398faf#gistcomment-3208848>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAODGYROKUTZE65RWK4LDYLRHASVTANCNFSM4LGBJLTQ>
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I read "a functional approach" and I got excited. I'm very new to Unity and just use it for fun. But in my day job I do a lot of functional coding, so I find it odd that I can't (for example) pass any context (other than a starting position and parent) to objects that I create with
Instantiate
.So far I've been going with the static-singleton approach but started looking at representing everything as addressables instead, and global state became problematic immediately. I don't even like global state, so this might be a worthy alternative. I will give it a try.