In this article, we’ll walk through a solution to fix the error encountered while trying to assign an array of type Array to a variable of type Array[BlueprintRecipeItem] in Godot 4.0.1. Understanding how to properly declare and initialize arrays with custom types will help in avoiding similar issues in the future.
Understanding the Error
The error occurs because the engine is expecting an array of type Array[BlueprintRecipeItem], but the provided value is of type Array. Here’s the code that’s causing the issue:
extends Resource class_name Blueprint @export var recipe: Array[BlueprintRecipeItem] = [null] func _init(p_recipe = [null]): recipe = p_recipe
The problem lies in the default values for the recipe variable and the p_recipe parameter in the _init() function. They are both set to [null], which is treated as an Array, not an Array[BlueprintRecipeItem].
Fixing the Error
To fix the error, you can initialize the recipe variable and the p_recipe parameter with an empty Array[BlueprintRecipeItem] instead:
extends Resource class_name Blueprint @export var recipe: Array[BlueprintRecipeItem] =  func _init(p_recipe: Array[BlueprintRecipeItem] = ): recipe = p_recipe
By initializing the variables with empty arrays of the expected type, you avoid the type mismatch error and ensure that your Blueprint resource works as intended.
In conclusion, the key to resolving the array type mismatch error in this case was to ensure that both the recipe variable and the p_recipe parameter were initialized with the correct type: Array[BlueprintRecipeItem]. By properly initializing arrays with custom types, you can avoid similar issues and make your code more robust and error-free. If you have any further questions or need assistance, please feel free to ask. Happy coding!