Page 1 of 1

[ZScript] Dynamically attaching code to Custom Inventory

Posted: Fri Dec 31, 2021 8:17 am
by stainedofmind
Hello everyone. I'm working on a personal addon for a gameplay mod, and I'm trying to figure out the most efficient way to determine when the player picks up specific types of Custom Inventory (from the original mod) out of a very large list of potential types. I'd rather not touch the original mod, so that I don't have to redo my code for every update, so I was trying to come up with a way to detect pickups dynamically. I was thinking using the "WorldThingSpawned" event to detect any instances of said Custom Inventory, then somehow attach a "pickup" event to it from there. IS this possible, or is there a better way short of a giant loop? I should also note, that due to how the original mod works, simply inheriting and extending the original items isn't an option.

In short, without touching or inheriting from the original mod, what is the most efficient way to detect when a player picks up a type of Custom Inventory?

Thanks.

Re: [ZScript] Dynamically attaching code to Custom Inventory

Posted: Fri Dec 31, 2021 11:16 am
by Caligari87
The way I'd approach something like this is giving the player an invisible inventory item that watches their inventory and does something appropriately. The following example contains a latching value so the condition only triggers on a change; you could of course do anything else, such as destroying the checker or something. Multiple conditionals and latching variables for different items and could be included in the same checker as well.

Code: Select all

class LookForChainguns : Inventory {
	int OldValue;

	// DoEffect runs every tic
	// if this has an owner
	override void DoEffect() {
		if (owner.countinv("Chaingun") != OldValue) {
			console.printf("Debug: Picked up or dropped a chaingun!");
		}
		OldValue = owner.countinv("Chaingun"); 
	}	
}
8-)

Re: [ZScript] Dynamically attaching code to Custom Inventory

Posted: Sun Jan 02, 2022 3:02 pm
by stainedofmind
Thanks for the input @Caligari87. What I ended up doing was using WorldThingSpawned, filtering out the unneeded actors via simple string indexing, then having it register into a queue for later processing through WorldTick. Not as efficient as I would have liked, but it gets the job done, avoids a gigantic ~2000 item loop, and doesn't seem to cause any excess lag on my potato-lappy.

Re: [ZScript] Dynamically attaching code to Custom Inventory

Posted: Sun Jan 02, 2022 11:04 pm
by Kzer-Za
stainedofmind wrote:What I ended up doing was using WorldThingSpawned, filtering out the unneeded actors via simple string indexing, then having it register into a queue for later processing through WorldTick.
Could you share the code, please? It seems I have a similar goal: viewtopic.php?f=122&t=74320

Re: [ZScript] Dynamically attaching code to Custom Inventory

Posted: Wed Jan 05, 2022 6:23 pm
by stainedofmind
Sorry about the late reply. Not sure if this is going to help your cause, but here is a demo version of the code I used:

https://drive.google.com/file/d/1nW29Zc ... sp=sharing

Re: [ZScript] Dynamically attaching code to Custom Inventory

Posted: Thu Jan 06, 2022 1:20 pm
by Kzer-Za
Thanks!