You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Allow Lua scripts to prompt the player to select an item from their backpack.
455
+
Allow Lua scripts to prompt the player to select an item from their backpack. This feature enables interactive gameplay where players can choose specific items to interact with through spells, NPC interactions, or other game mechanics.
456
456
457
457
### Library
458
458
`itemSelector.lua`
459
459
460
460
### Key Methods
461
-
- `selectItem(callback, mode, title, selector)`: Show item selection window
462
-
- `selectAnyItem(callback, title, selector)`: Select any item
461
+
- `selectItem(callback, mode, title, selector)`: Show item selection window with custom filtering
462
+
- `selectAnyItem(callback, title, selector)`: Select any item (equivalent to `selectItem` with `RPD.BackpackMode.ALL`)
463
463
- `selectUnidentifiedItem(callback, title, selector)`: Select unidentified items only
464
464
- `selectUpgradeableItem(callback, title, selector)`: Select upgradeable items only
465
465
- `selectWeapon(callback, title, selector)`: Select weapons only
@@ -469,12 +469,24 @@ Allow Lua scripts to prompt the player to select an item from their backpack.
469
469
- `selectArrow(callback, title, selector)`: Select arrows only
470
470
471
471
### Parameters
472
-
- `callback`: Function to call when an item is selected (receives item and selector as parameters)
473
-
- `mode`: Selection mode (optional, defaults to ALL)
474
-
- `title`: Window title (optional, defaults to "Select an item")
475
-
- `selector`: Character that is selecting the item (optional, defaults to hero)
476
-
477
-
### Example
472
+
- `callback`: Function to call when an item is selected. The callback receives two parameters:
473
+
- `item`: The selected item object, or `nil` if no item was selected
474
+
- `selector`: The character that selected the item (usually the hero)
475
+
- `mode`: Selection mode that filters which items are shown (optional, defaults to `RPD.BackpackMode.ALL`)
476
+
- `title`: Window title displayed to the player (optional, defaults to "Select an item")
477
+
- `selector`: Character that is selecting the item (optional, defaults to `RPD.Dungeon.hero`)
478
+
479
+
### Available Selection Modes
480
+
- `RPD.BackpackMode.ALL`: Show all items
481
+
- `RPD.BackpackMode.UNIDENTIFED`: Show only unidentified items
482
+
- `RPD.BackpackMode.UPGRADEABLE`: Show only upgradeable items
483
+
- `RPD.BackpackMode.WEAPON`: Show only weapons
484
+
- `RPD.BackpackMode.ARMOR`: Show only armor
485
+
- `RPD.BackpackMode.WAND`: Show only wands
486
+
- `RPD.BackpackMode.SEED`: Show only seeds
487
+
- `RPD.BackpackMode.ARROWS`: Show only arrows
488
+
489
+
### Example Usage in Spells
478
490
```lua
479
491
local RPD = require "scripts/lib/commonClasses"
480
492
local itemSelector = require "scripts/lib/itemSelector"
@@ -483,24 +495,134 @@ local itemSelector = require "scripts/lib/itemSelector"
483
495
itemSelector.selectItem(function(item, selector)
484
496
if item then
485
497
RPD.glog("Selected item: " .. item:name())
498
+
-- Perform actions with the selected item
499
+
-- For example, identify it
500
+
item:identify()
486
501
else
487
502
RPD.glog("No item selected")
488
503
end
489
-
end, RPD.BackpackMode.ALL, "Choose an item")
504
+
end, RPD.BackpackMode.ALL, "Choose an item to identify")
end, RPD.BackpackMode.ALL, "Select an item to curse")
593
+
594
+
returntrue
497
595
end
498
-
end, "Choose a weapon to equip")
596
+
}
499
597
```
500
598
501
-
### Test Spell
599
+
### Test Spell and Debug Configuration
502
600
A test spell called "CurseItem" is included in the debug configuration to demonstrate the item selection functionality. This spell can be found in `scripts/spells/CurseItem.lua` and is automatically added to the player's inventory when running in debug mode. The spell allows the player to select any item from their backpack and curse it.
503
601
602
+
To add this spell to a hero in debug mode, the following configuration is used in `initHeroesDebug.json`:
603
+
604
+
```json
605
+
{
606
+
"common": {
607
+
"items": [
608
+
{
609
+
"kind": "SpellBook",
610
+
"identified": true,
611
+
"spell": "CurseItem"
612
+
}
613
+
]
614
+
}
615
+
}
616
+
```
617
+
618
+
### Best Practices
619
+
1. Always validate that an item was selected before attempting to use it (check if `item` is not `nil`)
620
+
2. Provide clear titles to help players understand what they're selecting
621
+
3. Use appropriate selection modes to filter items and improve user experience
622
+
4. Give feedback to the player about what happened after they selected an item
623
+
5. Consider adding visual or audio effects to enhance the user experience
624
+
6. Handle edge cases such as already cursed, identified, or upgraded items
625
+
504
626
## JSON Configuration Files
505
627
506
628
### Purpose
@@ -684,6 +806,25 @@ To give a hero a spellbook with a specific spell, use the following format:
684
806
685
807
Note that the `spell` field should contain just the name of the spell file without the path or `.lua` extension. The game will automatically look for the spell in the `scripts/spells/` directory.
686
808
809
+
##### Spell Categories
810
+
811
+
Spells are organized into categories in the `CustomSpellsList.lua` file. When creating new spells, you should add them to an appropriate category:
0 commit comments