Skip to content

Commit 2407728

Browse files
committed
Improve item selection documentation in modding.md with detailed examples and best practices
Co-authored-by: Qwen-Coder <[email protected]>
1 parent 63d7181 commit 2407728

File tree

1 file changed

+157
-16
lines changed

1 file changed

+157
-16
lines changed

docs/modding.md

Lines changed: 157 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -452,14 +452,14 @@ end
452452
## Item Selection
453453
454454
### Purpose
455-
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.
456456
457457
### Library
458458
`itemSelector.lua`
459459
460460
### 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`)
463463
- `selectUnidentifiedItem(callback, title, selector)`: Select unidentified items only
464464
- `selectUpgradeableItem(callback, title, selector)`: Select upgradeable items only
465465
- `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.
469469
- `selectArrow(callback, title, selector)`: Select arrows only
470470
471471
### 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
478490
```lua
479491
local RPD = require "scripts/lib/commonClasses"
480492
local itemSelector = require "scripts/lib/itemSelector"
@@ -483,24 +495,134 @@ local itemSelector = require "scripts/lib/itemSelector"
483495
itemSelector.selectItem(function(item, selector)
484496
if item then
485497
RPD.glog("Selected item: " .. item:name())
498+
-- Perform actions with the selected item
499+
-- For example, identify it
500+
item:identify()
486501
else
487502
RPD.glog("No item selected")
488503
end
489-
end, RPD.BackpackMode.ALL, "Choose an item")
504+
end, RPD.BackpackMode.ALL, "Choose an item to identify")
490505
491-
-- Select a weapon
506+
-- Select a weapon to upgrade
492507
itemSelector.selectWeapon(function(item, selector)
493508
if item then
494-
RPD.glog("Selected weapon: " .. item:name())
495-
-- Equip the weapon
496-
item:execute(selector, "EquipableItem_ACEquip")
509+
RPD.glog("Upgrading " .. item:name() .. "!")
510+
-- Upgrade the weapon
511+
item:upgrade()
512+
item:level(item:level() + 1)
513+
end
514+
end, "Choose a weapon to upgrade")
515+
```
516+
517+
### Example Usage in NPC Interactions
518+
```lua
519+
local RPD = require "scripts/lib/commonClasses"
520+
local itemSelector = require "scripts/lib/itemSelector"
521+
local mob = require "scripts/lib/mob"
522+
523+
return mob.init({
524+
spawn = function(self, level)
525+
RPD.setAi(self, "NpcDefault")
526+
end,
527+
528+
interact = function(self, chr)
529+
RPD.glog("Hello! I can help you identify items.")
530+
531+
-- Show item selection window for unidentified items only
532+
itemSelector.selectUnidentifiedItem(function(item, selector)
533+
if item then
534+
RPD.glog("I've identified your " .. item:name() .. "!")
535+
item:identify()
536+
-- Play a visual effect
537+
RPD.zapEffect(self:getPos(), chr:getPos(), "Identification")
538+
else
539+
RPD.glog("You didn't select any item.")
540+
end
541+
end, "Select an item for me to identify")
542+
end
543+
})
544+
```
545+
546+
### Advanced Example: Curse Item Spell
547+
Here's a complete example of how to create a spell that allows players to curse an item:
548+
549+
```lua
550+
local RPD = require "scripts/lib/commonClasses"
551+
local itemSelector = require "scripts/lib/itemSelector"
552+
local spell = require "scripts/lib/spell"
553+
554+
return spell.init{
555+
desc = function()
556+
return {
557+
image = 0,
558+
imageFile = "spellsIcons/necromancy.png",
559+
name = "Curse Item",
560+
info = "Select an item from your backpack to curse it.",
561+
magicAffinity = "Necromancy",
562+
targetingType = "self",
563+
level = 1,
564+
castTime = 0,
565+
spellCost = 3,
566+
cooldown = 5
567+
}
568+
end,
569+
570+
cast = function(self, spell, chr)
571+
-- Show item selection window
572+
itemSelector.selectItem(function(item, selector)
573+
if item then
574+
-- Check if the item is already cursed
575+
if item:isCursed() then
576+
RPD.glog("That item is already cursed!")
577+
else
578+
-- Curse the selected item
579+
item:setCursed(true)
580+
item:setCursedKnown(true)
581+
RPD.glog("You have cursed your " .. item:name() .. "!")
582+
583+
-- Apply a visual effect
584+
RPD.zapEffect(chr:getPos(), chr:getPos(), "ShadowParticle")
585+
586+
-- Play a sound effect
587+
RPD.playSound("snd_cursed.mp3")
588+
end
589+
else
590+
RPD.glog("You decided not to curse anything.")
591+
end
592+
end, RPD.BackpackMode.ALL, "Select an item to curse")
593+
594+
return true
497595
end
498-
end, "Choose a weapon to equip")
596+
}
499597
```
500598

501-
### Test Spell
599+
### Test Spell and Debug Configuration
502600
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.
503601

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+
504626
## JSON Configuration Files
505627

506628
### Purpose
@@ -684,6 +806,25 @@ To give a hero a spellbook with a specific spell, use the following format:
684806

685807
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.
686808

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:
812+
813+
```lua
814+
spells["Necromancy"] = {"RaiseDead","Exhumation", "DarkSacrifice","Possess"}
815+
spells["Common"] = {"TownPortal","Heal","RaiseDead","Cloak","Calm","Charm"}
816+
spells["Combat"] = {"DieHard","Dash","BodyArmor","Smash"}
817+
spells["Rogue"] = {"Cloak","Backstab","KunaiThrow","Haste"}
818+
spells["Witchcraft"] = {"Roar","LightningBolt","Heal","Order"}
819+
spells["Huntress"] = {"Calm","Charm","ShootInEye","SummonBeast"}
820+
spells["Elf"] = {"MagicArrow","Sprout","HideInGrass","NatureArmor"}
821+
spells["Priest"] = {"Heal","Calm","Charm","Order"}
822+
spells["PlagueDoctor"] = {"Heal","Calm","Charm","Order"}
823+
spells["Dev"] = {"TestSpell", "CurseItem"}
824+
```
825+
826+
The "Dev" category is specifically for development and testing spells like "CurseItem" that are not meant for regular gameplay.
827+
687828
### Treasury Files
688829

689830
Treasury files define what items can be found in shops and their probabilities.

0 commit comments

Comments
 (0)