for i in player

This is a discussion / support forum for the Hugo programming language by Kent Tessman. Hugo is a powerful programming language for making text games / interactive fiction with multimedia support.

Hugo download links: https://www.generalcoffee.com/hugo
Roody Yogurt's Hugo Blog: https://notdeadhugo.blogspot.com
The Hugor interpreter by RealNC: http://ifwiki.org/index.php/Hugor

Moderators: Ice Cream Jonsey, joltcountry

User avatar
Ice Cream Jonsey
Posts: 28925
Joined: Sat Apr 27, 2002 2:44 pm
Location: Colorado
Contact:

for i in player

Post by Ice Cream Jonsey »

OK, I am absolutely losing my mind. I can't understand what is going on here.

I am doing the following to get rid of everything in the player's inventory:

Code: Select all

		
for i in player
{
	print i.name
	remove i 
}
And this code only prints the name of the first object in the player, and only removes that object. The other two he happens to have are left there, so I assume the for loop closes.

If I do not have the "remove i" line in there, it prints all three objects.

... but here's the thing. I've used this same code in at least two other games. I *know* it works. It iterates through the player and removes everything the player has.

... doesn't it? I guess there is a slight chance that the game never did it for Necrotic Drift, and I never noticed, but I could have sworn that I'd see a tester try to do something weird if that was the case.

I am really, really confused. I assume that you just can't "remove i" then? Because that kills the loop? What do you guys do when you want to remove the player character of all items he might have?
the dark and gritty...Ice Cream Jonsey!

User avatar
Ice Cream Jonsey
Posts: 28925
Joined: Sat Apr 27, 2002 2:44 pm
Location: Colorado
Contact:

Post by Ice Cream Jonsey »

Nevermind. While that code WAS in Necrotic Drift, I had the following above it. I guess I did both, but the following was the thing that actually worked:

Code: Select all

routine EmptyTarget(obj)
{
local i, j, c

	for i in obj
	{
		c++
	}

	if c ~= 0
	{
	   while c > 0
		{
		    for i in obj
		    {
			    j = i

		    }

		    remove j 
		    c--
		}
	}
}
the dark and gritty...Ice Cream Jonsey!

User avatar
Tdarcos
Posts: 9343
Joined: Fri May 16, 2008 9:25 am
Location: Arlington, Virginia
Contact:

Re: for i in player

Post by Tdarcos »

Ice Cream Jonsey wrote:OK, I am absolutely losing my mind. I can't understand what is going on here.

I am doing the following to get rid of everything in the player's inventory:

Code: Select all

		
for i in player
{
	print i.name
	remove i 
}
I have a thought. The "remove" is deleting items from the top down and the new items are being sucked up from the bottom to collapse the array. Thus the items are disappearing as a result. Consider this

A
B
C
D

On the first pass, it shows A and deletes it, but now B is number 1 (because number 1 is deleted) so now it shows C, deletes that one, and then D is number 2 and the list has been exhausted.

--
Paul Robinson - paul@paul-robinson.us - My Blog
"I really feel that I'm losin' my best friend
I can't believe this could be the end."
- No Doubt, Don't Speak

User avatar
Ice Cream Jonsey
Posts: 28925
Joined: Sat Apr 27, 2002 2:44 pm
Location: Colorado
Contact:

Post by Ice Cream Jonsey »

Paul!!!!! Ben has spoke very glowingly of you, so please let me welcome you to the BBS. It is great to have you here. And yeah, I do believe that's exactly what I was doing with that little loop right there. I should have a feature to my text editor that lets me hover over code and see what time I wrote it. If it's beyond two in the morning, a little pop-up window should come up and say, "WARNING: PROBABLE SLOP!"

Once again, thanks for stopping by, and I hope you have some fun on this BBS.

-- Robb
the dark and gritty...Ice Cream Jonsey!

User avatar
Tdarcos
Posts: 9343
Joined: Fri May 16, 2008 9:25 am
Location: Arlington, Virginia
Contact:

Post by Tdarcos »

Ice Cream Jonsey wrote: And yeah, I do believe that's exactly what I was doing with that little loop right there.
I think I've probably been bitten by that at one time or another; typical practice is either do delete from the bottom up, or start at the top, process the items then delete the entire list once one passes the last one. Either way will work, but deleting from the top down almost certainly won't.
"I really feel that I'm losin' my best friend
I can't believe this could be the end."
- No Doubt, Don't Speak

Roody_Yogurt
Posts: 2181
Joined: Mon Apr 29, 2002 6:23 pm
Location: Milwaukee

Post by Roody_Yogurt »

I'm sure the Necrotic Drift code is more useful, but here's my own take on the initial problem:

Code: Select all

 while child(player) ~= 0
	{
	for i in player
		{
		print i.name
		remove i
		}
	}
I can only imagine that in the first code, at the end of the for loop, the code thinks, welp, just got rid of i- guess I'm done! Definitely a bit counter-intuitive so I guess it's yet another thing somebody ought to keep in mind.

Post Reply