Looping over NPCs

Ask and answer questions about making games and related topics. Unrelated topics go in that other forum.

Moderators: marionline, SDHawk

Bluefeather42
Slime Knight
Posts: 102
Joined: Fri Jan 13, 2023 1:13 am

Re: Looping over NPCs

Post by Bluefeather42 »

...Okay, I promise I will stop trying to code after 9 PM, for real this time. Thanks again James, that fixed everything.

Code: Select all

script, critterloop, begin

	variable (ref, next ref)
	ref := Next NPC Reference()
	while (ref) do(
		next ref := Next NPC Reference(ref)
		if(get NPC Pool(ref) == pool:global) then (
			critterbrain (ref)
		)
		ref := next ref
	)
	 
end

# Check whether critters can see the hero
script, critterbrain, ref, begin
	variable (npcSl, FV, RV)	
	
	npcSl := get NPC Slice(ref)

	switch (get NPC ID(ref)) do (
		case (critter:shadowdude) do (
			FV := 6
			RV := 3
			if (check line of sight (NPC X(ref), NPC Y(ref), hero X(me), hero Y(me), NPC Direction(ref), FV, RV)) then (
				change NPC ID (ref, critter:shadowdudeangry, pool:global)
			)
		)
	)
end
shadowdudeangry is set to "Chase Hero", so the script triggers with the appropriate behavior.
User avatar
Bob the Hamster
Lord of the Slimes
Posts: 7732
Joined: Tue Oct 16, 2007 2:34 pm
Location: Hamster Republic (Ontario Enclave)
Contact:

Re: Looping over NPCs

Post by Bob the Hamster »

Wonderful! I am glad it worked :)
Post Reply