Weapon creation plugin

This commit is contained in:
MaddoScientisto 2026-02-08 13:11:14 +01:00
commit b4c38b159e
14 changed files with 1617 additions and 1 deletions

View file

@ -0,0 +1,181 @@
@tool
extends EditorScript
# EditorScript to create a new 3D weapon with all required resources
# Usage: Open this file in the Godot editor, then go to File > Run
func _run():
# Configuration - modify these values before running
var weapon_name = "New Weapon"
var weapon_item_key = "NEW_WEAPON"
var weapon_short_name = "NW-1"
var weapon_description = "A new weapon for testing"
# Default bullet resource to use
var default_bullet_path = "res://Resources/Bullets/simple_ice_bullet.tres"
# Resource paths
var weapon_resource_path = "res://Resources/Weapons/" + weapon_item_key + ".tres"
var item_resource_path = "res://Resources/Items/" + weapon_item_key + "_Item.tres"
var items_database_path = "res://Resources/ItemsDatabase.tres"
print("=== Starting Weapon Creation ===")
print("Weapon Name: ", weapon_name)
print("Item Key: ", weapon_item_key)
# Step 1: Create WeaponResource
if create_weapon_resource(weapon_resource_path, weapon_name, weapon_item_key, default_bullet_path):
print("✓ Created WeaponResource at: ", weapon_resource_path)
else:
print("✗ Failed to create WeaponResource")
return
# Step 2: Create LootItem resource
if create_loot_item_resource(item_resource_path, weapon_name, weapon_short_name,
weapon_description, weapon_item_key, weapon_resource_path):
print("✓ Created LootItem at: ", item_resource_path)
else:
print("✗ Failed to create LootItem")
return
# Step 3: Add to ItemsDatabase
if add_to_items_database(items_database_path, item_resource_path):
print("✓ Added to ItemsDatabase")
else:
print("✗ Failed to add to ItemsDatabase")
return
print("=== Weapon Creation Complete ===")
print("Remember to:")
print("1. Set appropriate weapon stats in ", weapon_resource_path)
print("2. Add a sprite texture to the LootItem in ", item_resource_path)
print("3. Save all modified resources")
func create_weapon_resource(path: String, weapon_name: String, item_key: String, bullet_path: String) -> bool:
# Check if file already exists
if ResourceLoader.exists(path):
print("Warning: WeaponResource already exists at ", path)
return false
# Load the WeaponResource script
var weapon_script = load("res://Scripts/Resources/WeaponResource.cs")
if weapon_script == null:
print("Error: Could not load WeaponResource.cs script")
return false
# Load the bullet resource
var bullet_resource = load(bullet_path)
if bullet_resource == null:
print("Error: Could not load bullet resource from ", bullet_path)
return false
# Create new WeaponResource
var weapon_resource = Resource.new()
weapon_resource.set_script(weapon_script)
# Set properties (using reflection-like approach in GDScript)
weapon_resource.set("Name", weapon_name)
weapon_resource.set("BulletData", bullet_resource)
weapon_resource.set("ItemKey", item_key)
weapon_resource.set("AmmoKey", "BATTERY")
weapon_resource.set("Priority", 10)
weapon_resource.set("AmmoPerShot", 1)
weapon_resource.set("RateOfFire", 0.2)
weapon_resource.set("BulletCapacity", 50)
weapon_resource.set("ReloadTime", 1.0)
weapon_resource.set("InfiniteAmmo", false)
weapon_resource.set("AutoReload", true)
weapon_resource.set("RechargeTime", 0.5)
weapon_resource.set("RechargeAmount", 5)
weapon_resource.set("BulletsPerShot", 1)
weapon_resource.set("SpreadAngle", 0.0)
weapon_resource.set("RandomSpread", 0.0)
# Save the resource
var err = ResourceSaver.save(weapon_resource, path)
if err != OK:
print("Error saving WeaponResource: ", err)
return false
return true
func create_loot_item_resource(path: String, item_name: String, short_name: String,
description: String, item_key: String,
weapon_resource_path: String) -> bool:
# Check if file already exists
if ResourceLoader.exists(path):
print("Warning: LootItem already exists at ", path)
return false
# Load the LootItem script
var loot_item_script = load("res://Scripts/Resources/LootItem.cs")
if loot_item_script == null:
print("Error: Could not load LootItem.cs script")
return false
# Load the weapon resource we just created
var weapon_resource = load(weapon_resource_path)
if weapon_resource == null:
print("Error: Could not load weapon resource from ", weapon_resource_path)
return false
# Create new LootItem
var loot_item = Resource.new()
loot_item.set_script(loot_item_script)
# Set properties
loot_item.set("ItemName", item_name)
loot_item.set("ShortName", short_name)
loot_item.set("ItemDescription", description)
loot_item.set("ItemKey", item_key)
loot_item.set("Item", 13) # ItemTypes.Weapon = 13 (0-indexed)
loot_item.set("WeaponData3D", weapon_resource)
loot_item.set("Amount", 1)
loot_item.set("Max", 1)
loot_item.set("Selectable", true)
loot_item.set("DropScenePath3D", "res://Scenes/Items/GenericItem3D.tscn")
# Save the resource
var err = ResourceSaver.save(loot_item, path)
if err != OK:
print("Error saving LootItem: ", err)
return false
return true
func add_to_items_database(database_path: String, item_resource_path: String) -> bool:
# Load the ItemsDatabase
var items_database = load(database_path)
if items_database == null:
print("Error: Could not load ItemsDatabase from ", database_path)
return false
# Load the item we just created
var new_item = load(item_resource_path)
if new_item == null:
print("Error: Could not load item from ", item_resource_path)
return false
# Get current items array
var loot_items = items_database.get("LootItems")
if loot_items == null:
print("Error: Could not get LootItems array from database")
return false
# Check if item already exists (by comparing resource paths)
for item in loot_items:
if item.resource_path == item_resource_path:
print("Warning: Item already exists in database")
return false
# Append the new item
loot_items.append(new_item)
items_database.set("LootItems", loot_items)
# Save the database
var err = ResourceSaver.save(items_database, database_path)
if err != OK:
print("Error saving ItemsDatabase: ", err)
return false
return true

View file

@ -0,0 +1 @@
uid://cri0vqvr1jg5c

View file

@ -0,0 +1,170 @@
# Create Weapon 3D - EditorScript
## Overview
This GDScript EditorScript automates the creation of a new 3D weapon with all required resources in the Cirno project. It handles the tedious process of creating and linking multiple resource files.
## What It Creates
When you run this script, it will automatically:
1. **Create a WeaponResource** (`res://Resources/Weapons/[WEAPON_KEY].tres`)
- Sets up basic weapon stats (rate of fire, ammo capacity, etc.)
- Links to the default bullet resource
- Configures weapon properties
2. **Create a LootItem resource** (`res://Resources/Items/[WEAPON_KEY]_Item.tres`)
- Links to the WeaponResource
- Sets item type to "Weapon"
- Configures the drop scene path to `GenericItem3D.tscn`
3. **Add to ItemsDatabase** (`res://Resources/ItemsDatabase.tres`)
- Automatically appends the new item to the end of the database list
## How to Use
### Method 1: Using the Script Editor
1. Open `Scripts/Editor/CreateWeapon3D.gd` in the Godot Script Editor
2. Modify the configuration variables at the top of the `_run()` function:
```gdscript
var weapon_name = "My Awesome Gun"
var weapon_item_key = "AWESOME_GUN"
var weapon_short_name = "AG-1"
var weapon_description = "An amazing weapon that shoots cool projectiles"
```
3. Go to **File > Run** in the menu (or press `Ctrl+Shift+X`)
4. Check the Output panel for success messages
### Method 2: From the Command Line
You can also run the script from the command line using the Godot executable:
```bash
godot --editor --script Scripts/Editor/CreateWeapon3D.gd --quit
```
## Configuration Variables
Edit these variables in the `_run()` function before running:
| Variable | Description | Example |
|----------|-------------|---------|
| `weapon_name` | Display name of the weapon | `"Plasma Rifle"` |
| `weapon_item_key` | Unique identifier (uppercase with underscores) | `"PLASMA_RIFLE"` |
| `weapon_short_name` | Short abbreviation shown in UI | `"PR-5"` |
| `weapon_description` | Description text for the weapon | `"Fires superheated plasma bolts"` |
## Default Weapon Stats
The script creates weapons with these default values (you can modify them in the created resource afterwards):
- **BulletData**: `res://Resources/Bullets/simple_ice_bullet.tres`
- **AmmoKey**: `BATTERY`
- **Priority**: `10`
- **AmmoPerShot**: `1`
- **RateOfFire**: `0.2` seconds
- **BulletCapacity**: `50`
- **ReloadTime**: `1.0` seconds
- **InfiniteAmmo**: `false`
- **AutoReload**: `true`
- **RechargeTime**: `0.5` seconds
- **RechargeAmount**: `5`
- **BulletsPerShot**: `1`
- **SpreadAngle**: `0.0`
- **RandomSpread**: `0.0`
## Post-Creation Steps
After running the script, you should:
1. **Open the WeaponResource** (`res://Resources/Weapons/[WEAPON_KEY].tres`)
- Adjust weapon stats to match your design
- Change the bullet type if needed
- Add reload and shoot sounds
2. **Open the LootItem** (`res://Resources/Items/[WEAPON_KEY]_Item.tres`)
- Add an inventory sprite texture
- Set the price if applicable
- Configure pickup behavior
3. **Test the weapon** by adding it to a test scene or using the debug menu
## Troubleshooting
### "WeaponResource already exists"
- The script checks if files already exist to prevent overwriting
- Delete the existing files first or choose a different weapon key
### "Could not load WeaponResource.cs script"
- Ensure the C# scripts are properly compiled
- Check that `res://Scripts/Resources/WeaponResource.cs` exists
### "Could not load bullet resource"
- Verify that `res://Resources/Bullets/simple_ice_bullet.tres` exists
- Or change `default_bullet_path` to point to a different bullet resource
### Changes not appearing in editor
- Save the project (`Ctrl+S`)
- Reimport resources if needed
- Restart Godot editor if issues persist
## Technical Details
### Resource Script Classes Used
- **WeaponResource**: C# class that defines weapon properties and behavior
- **LootItem**: C# class that represents items in the game's inventory system
- **ItemsDatabase**: C# class that holds all game items in a centralized list
### Item Type Enum
The script sets the Item property to `13`, which corresponds to `ItemTypes.Weapon` in the C# enum:
```csharp
public enum ItemTypes {
// ... other types ...
Weapon, // Index 13
// ... more types ...
}
```
## Customization
You can modify the script to:
- Change default weapon stats
- Use a different default bullet resource
- Create multiple weapons at once (by calling the functions in a loop)
- Add additional properties to the created resources
## Example Output
```
=== Starting Weapon Creation ===
Weapon Name: Plasma Rifle
Item Key: PLASMA_RIFLE
✓ Created WeaponResource at: res://Resources/Weapons/PLASMA_RIFLE.tres
✓ Created LootItem at: res://Resources/Items/PLASMA_RIFLE_Item.tres
✓ Added to ItemsDatabase
=== Weapon Creation Complete ===
Remember to:
1. Set appropriate weapon stats in res://Resources/Weapons/PLASMA_RIFLE.tres
2. Add a sprite texture to the LootItem in res://Resources/Items/PLASMA_RIFLE_Item.tres
3. Save all modified resources
```
## Notes
- This script is designed for 3D weapons specifically
- The script uses `@tool` annotation to run in the editor
- All paths use Godot's `res://` protocol for project-relative paths
- The script performs safety checks to prevent overwriting existing files
## Support
If you encounter issues:
1. Check the Godot Output panel for detailed error messages
2. Verify all C# scripts are compiled without errors
3. Ensure all referenced resources exist at their expected paths