Phazorknight / Cogito

Immersive Sim Template Project for GODOT 4
MIT License
827 stars 92 forks source link

Enemy, Script: Patrol points not added on scene enter #264

Closed OvercastInteractive closed 4 days ago

OvercastInteractive commented 1 week ago

Cogito and Godot Engine Version: Godot: 4.2.2 Cogito: beta 202409.02

Description: Saving in the Lobby before entering the Laboratory breaks the enemy's patrol points.

Reproduction steps:

  1. Start a new game
  2. Save in the Lobby
  3. Enter the Laboratory
    • Should get an index out of bounds exception

Expected behavior: Enemy patrol points are saved or unaffected by scene changes.

Screenshots: Cogito_Enemy_Patrol_Points_Issue_01

Phazorknight commented 1 week ago

Welp, another one of these saving/loading problems.

Looking into this but so far it seems trickier than I thought. I've tried saving and loading an array of NodePaths, but the save system can't store whole arrays (or at least not in a straight-forward way) so i might have to figure out a workaround.

Phazorknight commented 6 days ago

I've added some code to at least prevent crashes when transitioning to level scenes with enemies in them, see https://github.com/Phazorknight/Cogito/commit/5182d7ad93e02ed3ff3b4beafbb2bc089e3082f4

Will look into this more but for now at least this prevents crashes.

Phazorknight commented 6 days ago

Okay, I've changed the implementation of patrolling a bit to make this work in https://github.com/Phazorknight/Cogito/commit/e96be850e44fe03654708a61f507019b598e87b0:

There's now a script that can be attached to a node called cogito_patrol_path.gd which holds the Node3D Array to the patrol points. You can set a reference to a cogito_patrol_path in your enemy's inspector and this reference is saved and loaded as expected.

@OvercastInteractive pls let me know if this resolves this issue for you.

OvercastInteractive commented 6 days ago

I'm seeing a different issue now, the patrol_points array isn't getting initialized on load so it throws an error when attempting to get the array's size.

Cogito_Enemy_Patrol_Points_Issue_02

My basic workaround just assumes all the patrol points are children of the patrol path node and are added on ready: Cogito_Enemy_Patrol_Points_Issue_03

Phazorknight commented 6 days ago

Oh that's really odd. When exactly does this error happen? On testing the level scene directly? or when/after transitioning?

As the CogitoPatrolPath node is part of the level scene, it should always be available before the enemy gets loaded...

I personally don't want to dynamically add the patrol points to the array as I want to be able to re-use points between different paths and also leave it open to change their order within the array.

OvercastInteractive commented 6 days ago

My reproduction steps:

  1. Start new game
  2. Save in Lobby
  3. Enter Laboratory
Phazorknight commented 5 days ago

@OvercastInteractive thanks for this. I've realized that this issue is caused by the order in which godot loads nodes/scripts.

Please see https://github.com/Phazorknight/Cogito/commit/6c396f598a83ce757492510e9302c123a4fd5a7b for my attempt to fix this and hopefully improve robustness to the enemy script in general.

OvercastInteractive commented 4 days ago

Good to know, I've come across similar cases in my own projects in regard to script call order. Your changes look to have fixed this issue, thanks!

Phazorknight commented 4 days ago

Awesome, closing this.