#Field in struct not updating?

1 messages · Page 1 of 1 (latest)

chrome sonnet
#

Hey there, I'm quite new to Rust, game development and this game framework "ggez". I'm working on my version of Pong.
Thought you might've heard of it and could help me with this little problem that I'm having.

Here you have the code.
https://gist.github.com/earomc/6f89c0bc80f18038e6c24b51a8e364f7

See, in lines 116 and 117 I'm updating the dest parameter of the DrawParam fields that come with the two player struct instances.

I'm updating them based on the pos fields. At least I'm trying to.

As printing out both the pos fields and the draw_param fields reveals that the pos fields are being updated, but the draw_param fields aren't. Looking at examples like the snake example: https://github.com/ggez/ggez/blob/74ed4c7688cdd03a84c5c98f9c8650ec3947cf07/examples/04_snake.rs
They're creating a new DrawParam instance on every draw call.

  1. I wonder why they do that.
  2. Why can't I (apparently) change the values of the draw_param fields?

I appreciate every bit of help. Thanks in advance!

Gist

problematic ggez pong. GitHub Gist: instantly share code, notes, and snippets.

GitHub

Rust library to create a Good Game Easily. Contribute to ggez/ggez development by creating an account on GitHub.

#

Field in struct not updating?

wanton niche
#

dest takes DrawParam by value (which in this case makes a copy) and returns the updated DrawParam, which means if you don't do anything with the return, nothing happens.

You should be able to just assign it right back.

self.players.0.draw_param = self.players.0.draw_param.dest(self.players.0.pos);
#

dest should have been annotated with #[must_use] so that your original code would cause a warning, but ggez didn't do that.

chrome sonnet
#

Ah okay. So #[must_use] basically specifies that you must use the return value or else "nothing" happens?

wanton niche
#

It's just a way to document that if you aren't doing something with the return value, you're probably using it wrong. Calling the function might not literally do nothing (like for dest, it could panic), but the return is the most important part.

chrome sonnet
#

I think I understand it now.

So if I wanted to use it like I intended in the dest function it should say &mut self instead of self?

wanton niche
#

yeah