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

[REG-2168] Support a bot action to 'ResetForNextTest' .. and 'RestartGame' and 'QuitGame' #351

Merged
merged 12 commits into from
Nov 8, 2024

Conversation

nAmKcAz
Copy link
Collaborator

@nAmKcAz nAmKcAz commented Nov 6, 2024

  • Adds support for 'RestartGame' and 'QuitGame' Bot Actions
    • ResetForNextTest will run the registered restart impl, OR default to running the restart game action
    • Restart game will restart the game in the editor, or will load a custom impl of what to do in a standalone runtime
    • Quit game will stop the game in the editor, or exit the application in a standalone runtime
  • Also, refactors the package membership of criteria and actions to make them easier to find in the explorer .. this should be mostly contained to the first commit if you want to avoid the noise

Find the pull request instructions here

Every reviewer and the owner of the PR should consider these points in their request (feel free to copy this checklist so you can fill it out yourself in the overall PR comment)

  • The code is extensible and backward compatible
  • New public interfaces are extensible and open to backward compatibility in the future
  • If preparing to remove a field in the future (i.e. this PR removes an argument), the argument stays but is no longer functional, and attaches a deprecation warning. A linear task is also created to track this deletion task.
  • Non-critical or potentially modifiable arguments are optional
  • Breaking changes and the approach to handling them have been verified with the team (in the Linear task, design doc, or PR itself)
  • The code is easy to read
  • Unit tests are added for expected and edge cases
  • Integration tests are added for expected and edge cases
  • Functions and classes are documented
  • Migrations for both up and down operations are completed
  • A documentation PR is created and being reviewed for anything in this PR that requires knowledge to use
  • Implications on other dependent code (i.e. sample games and sample bots) is considered, mentioned, and properly handled
  • Style changes and other non-blocking changes are marked as non-blocking from reviewers

@nAmKcAz nAmKcAz requested a review from vontell as a code owner November 6, 2024 18:34
@@ -0,0 +1,10 @@
namespace StateRecorder.BotSegments.Models
{
public interface IRGRestartGameAction
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New interface for custom restart implementations

namespace RegressionGames.StateRecorder.BotSegments.Models.BotActions
{
[Serializable]
public class RestartGameBotActionData : IBotActionData
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New restart game bot action

namespace RegressionGames.StateRecorder.BotSegments.Models.BotActions
{
[Serializable]
public class QuitGameBotActionData : IBotActionData
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New quit game bot action

/**
* <summary> Add restart game bot action</summary>
*/
public const int VERSION_25 = 25;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new sdk api version

@@ -47,6 +49,12 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
case BotActionType.Mouse_ObjectDetection:
data = jObject["data"].ToObject<CVObjectDetectionMouseActionData>(serializer);
break;
case BotActionType.RestartGame:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handle the new actions when reading in bot segment json

@nAmKcAz nAmKcAz requested review from batu and abeizer November 6, 2024 18:47
@nAmKcAz nAmKcAz changed the title Zack/restart game action [REG-2168] Support a bot action to 'RestartGame' Nov 6, 2024
Copy link
Contributor

@addisonbgross addisonbgross left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall. Just 1 non-blocking Q

Copy link
Contributor

@batu batu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good!

Copy link
Collaborator

@vontell vontell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice!! I only had one question about default behavior in the editor for the restart mode, but looks good otherwise. Will resolve that quickly once I get an answer.

// un-register our listener
EditorApplication.playModeStateChanged -= PlayGameInEditor;
// just finished playing ... start it back up
EditorApplication.isPlaying = true;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm glad this actually works!

Comment on lines 100 to 107
RGDebug.LogInfo($"Restarting the game in the editor...");
_error = null;
error = _error;
// register our hook so that the game will start right back up
EditorApplication.playModeStateChanged += PlayGameInEditor;
// stop the game in the editor
EditorApplication.isPlaying = false;
return true;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean in editor we only allow a restart that completely restarts the game? I might be misreading this but it seems like the _action is only considered outside of editor?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct.. as of right now..

  • Editor - 'restart' actually restarts the game
  • External Runtime -
    • Default - 'restart' loads scene 0, which is as close as we can get to restarting the game without ending the process runtime; this restarts the game to the starting scene, but potentially without cleaning up many things as noted in the comments in the code... we could explore making this much harsher... like.. destroy all objects (event don't destroy on load) before loading scene 0 which would make it much closer to the editor behaviour... however, this still wouldn't kill background threads, or unity task running on entities/etc
    • Custom - 'restart' runs whatever commands are in your custom impl

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose that was fine for now, but I was thinking that even in editor it would run the custom impl, since my restart logic might just be "respawn at the respawn point and remove all enemies". But on second thought, it might be good to allow the custom action in the editor because this means the only way for you to test the custom logic is to keep rebuilding the game, which could take a long time.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently we weren't on the same page at all when initially discussing this task.

It seems you wanted an action to define 'reset for next test', but I got 'restart game' out of the initial discussion.

I guess I'll start over .

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Start over? I'm confused as I feel like all the code here aligns with exactly what I was thinking, including the default of loading the initial scene. From what I see here, this aligns almost exactly with my thinking but I had in my mind one small extension, but if that extension is not small I'd rather have this PR in.

@nAmKcAz nAmKcAz requested a review from vontell November 7, 2024 15:45
@nAmKcAz nAmKcAz closed this Nov 7, 2024
@nAmKcAz nAmKcAz reopened this Nov 7, 2024
@nAmKcAz nAmKcAz changed the title [REG-2168] Support a bot action to 'RestartGame' [REG-2168] Support a bot action to 'ResetGame' .. and 'RestartGame' and 'QuitGame' Nov 7, 2024
@nAmKcAz nAmKcAz changed the title [REG-2168] Support a bot action to 'ResetGame' .. and 'RestartGame' and 'QuitGame' [REG-2168] Support a bot action to 'ResetGameForNextTest' .. and 'RestartGame' and 'QuitGame' Nov 7, 2024
@nAmKcAz nAmKcAz changed the title [REG-2168] Support a bot action to 'ResetGameForNextTest' .. and 'RestartGame' and 'QuitGame' [REG-2168] Support a bot action to 'ResetForNextTest' .. and 'RestartGame' and 'QuitGame' Nov 7, 2024
@nAmKcAz
Copy link
Collaborator Author

nAmKcAz commented Nov 7, 2024

@vontell I debated whether to start a new PR for this, but I re-opened the existing. There is now another new action ResetForNextTest which should fulfill what you were looking for, but I am leaving the RestartGame and QuitGame actions in here as they feel safer / more appropriate to me.

You can now do what you intended with a partial reset for the next test without restarting the game, but I don't really agree with this function being different than restart game as there is generally no 1 single action you can take to safely reset the game's state other than fully restarting it.

I feel that this is only going to add confusion for users on how to implement 'reset' safely for multiple test scenarios.

@nAmKcAz nAmKcAz requested review from vontell and removed request for vontell November 7, 2024 18:16
@@ -11,6 +11,9 @@ public enum BotActionType
Mouse_CVImage,
Mouse_CVText,
Mouse_ObjectDetection,
RestartGame,
QuitGame,
ResetForNextTest
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather name this ResetGame, but that is too close to RestartGame to be completely clear.

Ultimately, I don't like Reset existing separately from Restart, but that was the only way to fufill @vontell 's ask that the custom impl be testable in the editor if desired.

@vontell
Copy link
Collaborator

vontell commented Nov 8, 2024

Approving but happy to discuss this further at some point, I have a feeling what I'm about to write out via text is better explained over a quick huddle at some point.

I think where the confusion came from is what we were assuming would go in this RestartGame custom impl, and I am fine going with whatever you were expecting inside of that... but anyhow, I was expecting the result of my message to not be an entirely new type, it was simply arguing that the custom impl would run in the editor - I guess I am missing some nuance around why we couldn't just delete that UNITY_EDITOR conditional in the ResetGame handler, that was what I thought would be the change (i.e. just making sure if I provide a custom impl, it uses that in editor so I can make sure that works before building). I suppose that means that the sentence "Ultimately, I don't like Reset existing separately from Restart, but that was the only way to fufill @vontell 's ask that the custom impl be testable in the editor if desired" is the part I don't understand, I'll dive a little deeper into the code though and see if I can figure that out for myself

@nAmKcAz
Copy link
Collaborator Author

nAmKcAz commented Nov 8, 2024

@vontell I fundamentally dis-agree with using the custom impl in the editor. The BEST and only truly clean option is to restart the game. In the editor, this is trivial to achieve and thus is what should happen. I don't like there being a reset and restart at all. I want just restart, as doing anything other than fully restarting the game is less than ideal. Unfortunately in a built runtime, the options for restarting the game are limited. That is why I had the initial discussion with you about possibly starting a new process for the game runtime as our preferred runtime option, but that didn't work well in docker. I would still prefer that to what is here now though.

@vontell
Copy link
Collaborator

vontell commented Nov 8, 2024

Hmmmm I think I see where the disconnect is, I suppose my question would be what exactly we imagine going into this custom impl for RestartGame.

In terms of custom impl in the editor, I think we are actually in agreement there, I am pretty ok with taking a stance that we should be doing the best possible restart to ensure things are not getting messed up. The only reason I was pushing for the above (this reasoning slightly shifted as we conversed) is that I really don't like the idea of code that I can't test until after the build is made, it just makes iterating for the developer tough. But then again, maybe that goes back to what kind of code is going to be inside this custom impl, maybe the code you are imagining is very simple and therefore that wouldn't be as much of a concern

Also just to demonstrate what I thought my comment was going to result in, here is the edit to the code that I thought would be made, I suppose I don't know why this change wouldn't work (this was a quick edit, small pieces of it may not be correct fully): 16e4823

@nAmKcAz nAmKcAz merged commit 68ebc00 into main Nov 8, 2024
2 checks passed
@nAmKcAz nAmKcAz deleted the zack/restart-game-action branch November 8, 2024 16:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants