Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't remove Object from Objects by PickUp method of Creature #43

Closed
VedVid opened this issue Nov 15, 2018 · 2 comments
Closed

Can't remove Object from Objects by PickUp method of Creature #43

VedVid opened this issue Nov 15, 2018 · 2 comments
Labels
bug Something isn't working critical [priority]

Comments

@VedVid
Copy link
Owner

VedVid commented Nov 15, 2018

func (c *Creature) PickUp(o Objects) bool {
	turnSpent := false
	for i, v := range o {
		if v.X == c.X && v.Y == c.Y && v.Pickable == true {
			item := o[i]
			c.Inventory = append(c.Inventory, item)
			fmt.Println(len(o))
			o[i] = o[len(o)-1] // Doesn't it leaks a memory? \/
			o = o[:len(o)-1]   // It's slice of pointers to struct.
			fmt.Println(len(o))
			turnSpent = true
			break
		}
	}
	return turnSpent
}

After picking item up, len(o) becomes smaller. But item remains on the ground. After another picking up, len(o) is the same as at start of previous call.

It's about pointers?

@VedVid
Copy link
Owner Author

VedVid commented Nov 15, 2018

I created new slice, with local variable, instead of mutating existing slice

@VedVid
Copy link
Owner Author

VedVid commented Nov 16, 2018

Fixed by cf1fe76 (and later it was necessary to 89e9059 )

@VedVid VedVid added bug Something isn't working critical [priority] labels Nov 16, 2018
@VedVid VedVid closed this as completed Nov 16, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working critical [priority]
Projects
None yet
Development

No branches or pull requests

1 participant