The ScriptLoader is a singleton class that handles the dynamic loading and management of compiled script components in the Pi-Engine.
public class ScriptLoader
| Property |
Type |
Description |
instance |
ScriptLoader |
Singleton instance of ScriptLoader |
rootDirectory |
File |
Root directory for compiled scripts |
urlClassLoader |
URLClassLoader |
ClassLoader for dynamically loading compiled scripts |
| Method |
Parameters |
Return Type |
Description |
getInstance() |
None |
ScriptLoader |
Static method to get or create the singleton instance |
reset() |
None |
void |
Static method to reset the ScriptLoader state and clear loaded components |
| Method |
Parameters |
Return Type |
Description |
loadClass(String fullyQualifiedName) |
String fullyQualifiedName |
Class<?> |
Loads a class by its fully qualified name |
loadComponentScript(File scriptFile) |
File scriptFile |
void |
Loads a single component script from a file |
loadComponentFolder(String folderPath) |
String folderPath |
void |
Loads all component scripts from a folder |
loadSystemScripts(String folderPath) |
String folderPath |
void |
(Reserved) Loads system scripts from a folder |
loadBehaviorScripts(String folderPath) |
String folderPath |
void |
(Reserved) Loads behavior scripts from a folder |
| Method |
Parameters |
Return Type |
Description |
close() |
None |
void |
Closes the URLClassLoader and cleans up resources |
- Script files are located in the compiled scripts directory
- Each script is loaded using the URLClassLoader
- If the loaded class extends Component:
- It is registered with the ComponentFactory
- The component becomes available for use in the engine
- Loading errors are logged to the Console
// Get ScriptLoader instance
ScriptLoader loader = ScriptLoader.getInstance();
// Load all components from the compiled folder
loader.loadComponentFolder("Compiled");
// Load a specific component
loader.loadComponentScript(new File("Compiled/Scripts/MyComponent.class"));
// Reset the ScriptLoader (e.g., for hot-reloading)
ScriptLoader.reset();
- Handle script loading errors gracefully
- Use the ComponentFactory to create component instances
- Clean up resources when reloading scripts
- Follow proper package naming conventions
- Maintain script organization by type