Scripting help for Ammo auto-pickup

Ask for help about creating mods and scripts for Grimrock 2 or share your tips, scripts, tools and assets with other modders here. Warning: forum contains spoilers!
Post Reply
User avatar
akroma222
Posts: 1029
Joined: Thu Oct 04, 2012 10:08 am

Scripting help for Ammo auto-pickup

Post by akroma222 »

Hey All,

As the title suggests, I am about to try reworking the auto-pickup of (fired/shot) ammunition
This is pretty much the last detail to be done for a v small Missile weapon /Projectile/Ammo asset pack Ive been working on
(I will eventually turn this thread into the asset pack link once all runs well)

I am firstly wondering if Anyone has already done this and is willing to share ideas/ provide hints... ??
(not asking for anyone to do this for me, just want to know if Im missing something important or obvious )

The way I see it being scripted...
SpoilerShow
onMove party hook >> iterate through all items on the square party is moving onto

>> check for Ammo item components (or if the item has "ammo" trait) >> if so....

>>check the item's ScriptComponent ("data") for a reference to champions ordinal ("champOrd")

- (ordinal info is passed to data when creating the normal non-enchanted converted item) -

if not "champOrd" then (it hasnt been fired/shot yet).... return

also check if the square has a floortrigger/pressure plate >> if so then.... return

if "champOrd" reference .....

>> check champs hands for normal non-enchanted Ammo >> if so AUTO PICKUP (into hands) and destroy ammo item

>> check champs hands for empty Hand slot + Missile weapon in other hand >> if so AUTO PICKUP (into hands) and destroy ammo item

>> if different Ammo in champs hand, check inventory for existing Stack of normal non-enchanted Ammo >> if so AUTO PICKUP (inventory) and destroy item

>> if no existing Stack in inventory and there is an empty slot >> AUTO PICKUP (inventory) ,destroy item and create new stack in backpack

AUTOPICKUP will involve some altered GUI stuff (maybe champ:showAttackResult() or drawGUI hook check) - I dont care if this is not exactly replicated
I was gong to try and not post bulky code and definitions as the lua scripts are dense and messy
(using 3 definitions - 1 for all 14 ammo items, 1 for their projectile versions and 1 for their hit effects)
But Im happy to share the code now if it helps context

Any help/guidance appreciated! 8-)
minmay
Posts: 2790
Joined: Mon Sep 23, 2013 2:24 am

Re: Scripting help for Ammo auto-pickup

Post by minmay »

There are three issues here that stand out to me:
1. when creating your "normal non-enchanted converted item" you don't need to add an entire ScriptComponent just to store the champion's ordinal. Instead use ItemComponent:addTrait("champion1") or "champion2" or "champion3" etc.

2. onMove is called whether the move is actually going to succeed or not - the square could be blocked, or the party could be overloaded or grappled, and then there's the possibility that there's a teleporter in the square, etc. Instead your onMove hook should spawn a floor trigger with the id of "autopickup_trigger" or whatever (destroying any existing "autopickup_trigger" first) that is only triggered by the party, and you should do your autopickup behaviour when it's triggered. Note that this still won't work for knockback, pit falls, teleporter destinations, etc...

3. you need to check that the item you're trying to autopickup doesn't have a ProjectileComponent; the approach you describe would autopickup items that are still flying through the air
Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.
User avatar
akroma222
Posts: 1029
Joined: Thu Oct 04, 2012 10:08 am

Re: Scripting help for Ammo auto-pickup

Post by akroma222 »

Ahh yup, glad I asked! :lol:
minmay wrote: 1. when creating your "normal non-enchanted converted item" you don't need to add an entire ScriptComponent just to store the champion's ordinal. Instead use ItemComponent:addTrait("champion1") or "champion2" or "champion3" etc.
Ive been adding ScriptComponents to most things as Im storing other info (hitEffect, power etc) too...
still, I hadnt actually considered using item traits for these things
minmay wrote: 2. onMove is called whether the move is actually going to succeed or not - the square could be blocked, or the party could be overloaded or grappled, and then there's the possibility that there's a teleporter in the square, etc. Instead your onMove hook should spawn a floor trigger with the id of "autopickup_trigger" or whatever (destroying any existing "autopickup_trigger" first) that is only triggered by the party, and you should do your autopickup behaviour when it's triggered. Note that this still won't work for knockback, pit falls, teleporter destinations, etc...
Really glad I asked :oops: great idea, will def go with that approach
minmay wrote: 3.you need to check that the item you're trying to autopickup doesn't have a ProjectileComponent; the approach you describe would autopickup items that are still flying through the air
We cant have that - Will be making the appropriate checks! (and thats actually given me another idea)

Thanks again minmay, muchly appreciated :)
Once Ive addressed the above, Ill post the auto pickup code + a link for the missile weapon/ammo pack
Post Reply