Forum rules - please read before posting.

Reproducing "Player: Switch" with custom scripting?

Hello all,

I've been using the action list visual scripting quite a bit and decided I felt comfortable enough switching over to C# / custom scripting so that I could navigate complex chains quicker (since I've been a coder for a while.) One thing that I want to reproduce in custom scripting are the actions from a single node in the action list called "Player: Switch".

I tried searching the API / scripting guide, but couldn't find any keywords specific to the player for switching in one function. I feel like there's a sequence that Player: Switch runs that is more than one line of code. I also tried searching the forums here, but my forum-search-fu isn't quite powerful enough to conjure the magic words.

Any guidance would be much appreciated. Thank you for your time and help!

Very respectfully,
Tyler

Comments

  • Welcome to the community, @TogaMario.

    Your hunch is right - the Action handles quite a few caveats, edge cases and options beyond simply switching the Player.

    Essentially though, if the new Player is in the current scene, call:

    KickStarter.player = newPlayer;
    

    Otherwise, it's a case of reading the Player's save data for their current scene, and calling SwitchToPlayerInDifferentScene:

    switch (KickStarter.settingsManager.referenceScenesInSave)
    {
        case ChooseSceneBy.Name:
            string sceneNameToLoad = KickStarter.saveSystem.GetPlayerSceneName (playerID);
            KickStarter.saveSystem.SwitchToPlayerInDifferentScene (playerID, sceneNameToLoad, false);
            break;
    
        case ChooseSceneBy.Number:
            int sceneIndexToLoad = KickStarter.saveSystem.GetPlayerSceneIndex (playerID);
            KickStarter.saveSystem.SwitchToPlayerInDifferentScene (playerID, sceneIndexToLoad, false);
            break;
    }
    

    Where playerID is the ID number of the Player, as listed to the left of their prefab field in the Settings Manager.

  • Hi, Chris! Thank you for the welcome, and advice.

    I may have approached the initial setup incorrectly for my intentions, now that I'm thinking more about it. Have you ever played Quest for Glory? I've got a character selection screen setup to change the player when you select your class. Works in the visual scripting, but essentially I'd made three players and was teleporting inactive out in favor of the currently selected.

    I'm not sure if it would make more sense to create three NPCs to serve the purpose of previewing the character selection, and only assign the actual player when they begin the game after the character selection. It's not my intention to allow switching after the game has begun.

    Any thoughts on that would be amazing, and I appreciate the insights!

    Very respectfully,
    Tyler

  • edited September 2022

    Actually, I think I might have gotten it. It feels a bit too much, so I may go with the NPC idea in the long run, but for now I'm doing something like this to swap out the actual current player as a preview on the character selection screen.

    PlayerData currentData = KickStarter.saveSystem.GetPlayerData(KickStarter.player.ID);
    KickStarter.player.RemoveFromScene();
    KickStarter.player = selectedPlayer.SpawnFromPrefab(selectedPlayerID);
    KickStarter.player.LoadData(currentData);
    
    

    If there's anything that you feel I'm missing in that, please point it out. But it seems to work for now without breaking anything noticeable.

    Thanks again!

    Very respectfully,
    Tyler

  • For situations like this where I want to do everything mostly by code, but an AC action is a lot more complex than just a couple of lines of code, I often just create an actionlist asset with said action (in your case, to switch the character), and then I run it with AC.AdvGame.RunActionListAsset (myActionListAsset);

    Looks cleaner, and you still benefit from using c# to organise the rest of your game logic.

  • If there's anything that you feel I'm missing in that, please point it out. But it seems to work for now without breaking anything noticeable.

    Looks solid. Using NPCs for this would be viable as well - particularly if they're just standing in a fixed position.

    I'd imagine it'd really come down to whether or not your "main game" Player is a different prefab based on this scene's choice, or it just results in costume changes to the same prefab via animation parameters. If using different prefabs, then doing as you are now should be enough, since the chosen Player will be kept when you move onto the next scene.

    I often just create an actionlist asset with said action (in your case, to switch the character), and then I run it with AC.AdvGame.RunActionListAsset

    Good point, and it's also worth mentioning that it's also possible to generate AC ActionLists through scripts at runtime - so a third option would be to run the Action without the use of an ActionList in the scene file / asset:

    ActionList actionList = gameObject.AddComponent<ActionList> ();
    actionList.actions = new List<Action>
    {
        ActionPlayerSwitch.CreateNew (newPlayerID, true)
    };
    actionList.Interact ();
    

    See the Manual's "Generating ActionLists through script" chapter for more on this topic.

  • That's amazing. Thank you so much!

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.