Lua: OnPickup and OnSlap triggers#4805
Open
Loobinex wants to merge 5 commits into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
Adds new Lua trigger entry points so level scripts can react to player interactions (picking up things into the hand and slapping things), integrating them into the existing TriggerSystem/Builtins event pipeline.
Changes:
- Invoke new Lua triggers from hand pickup and slap power usage (
lua_on_pick_up,lua_on_slap). - Add C trigger wrappers and header declarations for
OnPickUpandOnSlap. - Add Builtins.lua handlers to forward
PickUp/Slapevents intoProcessEvent.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| src/magic_powers.c | Calls Lua triggers when using Hand pickup and Slap powers |
| src/lua_triggers.h | Declares new trigger functions for pickup/slap |
| src/lua_triggers.c | Implements Lua global dispatch for OnPickUp / OnSlap |
| config/fxdata/lua/triggers/Builtins.lua | Adds builtin Lua handlers emitting PickUp and Slap events |
Comments suppressed due to low confidence (1)
src/magic_powers.c:1806
- lua_on_slap is invoked before verifying thing_exists(thing) and before checking whether the slap will actually be applied (instance already PI_Whip / recent drop early-return). This can fire OnSlap for a non-existent thing (Lua gets nil) or for slaps that are ignored, which is likely inconsistent with the trigger semantics. Consider moving lua_on_slap to after the thing_exists check and only on the Lb_SUCCESS path (when the slap is actually performed and counted).
static TbResult magic_use_power_slap_thing(PowerKind power_kind, PlayerNumber plyr_idx, struct Thing *thing, MapSubtlCoord stl_x, MapSubtlCoord stl_y, KeepPwrLevel power_level, unsigned long mod_flags)
{
struct PlayerInfo *player;
struct Dungeon *dungeon;
lua_on_slap(thing, plyr_idx);
if (!thing_exists(thing)) {
return Lb_FAIL;
}
player = get_player(plyr_idx);
dungeon = get_dungeon(player->id_number);
if ((player->instance_num == PI_Whip) || (get_gameturn() - dungeon->last_creature_dropped_gameturn <= 10)) {
return Lb_OK;
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+285
to
+291
| lua_getglobal(Lvl_script, "OnPickUp"); | ||
| if (lua_isfunction(Lvl_script, -1)) | ||
| { | ||
| lua_pushThing(Lvl_script, thing); | ||
| lua_pushPlayer(Lvl_script, plyr_idx); | ||
| CheckLua(Lvl_script, lua_pcall(Lvl_script, 1, 0, 0), "OnPickUp"); | ||
| } |
Comment on lines
+301
to
+307
| lua_getglobal(Lvl_script, "OnSlap"); | ||
| if (lua_isfunction(Lvl_script, -1)) | ||
| { | ||
| lua_pushThing(Lvl_script, thing); | ||
| lua_pushPlayer(Lvl_script, plyr_idx); | ||
| CheckLua(Lvl_script, lua_pcall(Lvl_script, 1, 0, 0), "OnSlap"); | ||
| } |
| if (place_thing_in_power_hand(thing, plyr_idx)) | ||
| else if (place_thing_in_power_hand(thing, plyr_idx)) | ||
| { | ||
| lua_on_pick_up(thing, plyr_idx); |
Comment on lines
+103
to
+113
| function OnPickUp(thing) | ||
| local eventData = {} | ||
| eventData.thing = thing | ||
| ProcessEvent("PickUp",eventData) | ||
| end | ||
|
|
||
| --- Called when a thing is slapped. Can be Creatures, Traps, Shots, Objects, etc. | ||
| ---@param thing Thing | ||
| function OnSlap(thing) | ||
| local eventData = {} | ||
| eventData.thing = thing |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.