Chocrates

Chocrates

Hands-on Rust: What does movers.iter_mut(ecs) do: Chapter 7

@herbert
Again using epub so don’t know the exact page number.

In this snipped of code, what does the movers.iter_mut(ecs) line do in the following code?
I think that movers is a list of tuples that we stored in the ECS and grabbed from the query.
I think that the iter_mut gives us a mutable iterator on the list of tuples.
What I don’t understand is why we pass in the ecs object to the iter_mut. I would think that it would be movers.iter_mut()

​ 	​use​ ​crate​::​prelude​::*;
​ 	
​ 	#[system]
​ 	#[write_component(Point)]
​ 	#[read_component(MovingRandomly)]
​​①​	​pub​ ​fn​ ​random_move​(ecs: &​mut​ SubWorld, ​#​[resource] map: &Map) {
​​②​	    ​let​ ​mut​ movers = <(&​mut​ Point, &MovingRandomly)>::​query​();
​ 	    movers
​ 	        ​.iter_mut​(ecs)
​ 	        ​.for_each​(|(pos, _)| {
​ 	            ​let​ ​mut​ rng = ​RandomNumberGenerator​::​new​();
​​③​	            ​let​ destination = ​match​ rng​.range​(0, 4) {
​ 	                0 ​=>​ ​Point​::​new​(​-​1, 0),
​ 	                1 ​=>​ ​Point​::​new​(1, 0),
​ 	                2 ​=>​ ​Point​::​new​(0, ​-​1),
​ 	                _ ​=>​ ​Point​::​new​(0, 1),
​ 	            } + *pos;
​ 	
​​④​	            ​if​ map​.can_enter_tile​(destination) {
​​⑤​	                *pos = destination;
​ 	            }
​ 	        }
​ 	    );
​ 	}
​①​

First Post!

herbert

herbert

Author of Hands-on Rust

Sorry for the slow reply, I’ve been at home caring for a baby with daycare plague.

This is a quirk of how Legion works. When we build the query structure in-function, Legion has no way of knowing which world we mean. We’ve explicitly requested a SubWorld (with access to Point and MovingRandomly) - so we know that we need that world (the ecs variable), but the query structure itself doesn’t know that. So we have to tell it, as a parameter to iter_mut.

If I were revising it, I’d probably put the query in the system parameters (with automatic wiring up of the subworld). I’ll make a note for a potential future second edition.

Thanks!

Where Next?

Popular Pragmatic Bookshelf topics Top

HarryDeveloper
Hi @venkats, It has been mentioned in the description of ‘Supervisory Job’ title that 2 things as mentioned below result in the same eff...
New
AleksandrKudashkin
On the page xv there is an instruction to run bin/setup from the main folder. I downloaded the source code today (12/03/21) and can’t see...
New
cro
I am working on the “Your Turn” for chapter one and building out the restart button talked about on page 27. It recommends looking into ...
New
Chrichton
Dear Sophie. I tried to do the “Authorization” exercise and have two questions: When trying to plug in an email-service, I found the ...
New
patoncrispy
I’m new to Rust and am using this book to learn more as well as to feed my interest in game dev. I’ve just finished the flappy dragon exa...
New
adamwoolhether
Is there any place where we can discuss the solutions to some of the exercises? I can figure most of them out, but am having trouble with...
New
hazardco
On page 78 the following code appears: &lt;%= link_to ‘Destroy’, product, class: ‘hover:underline’, method: :delete, data: { confirm...
New
akraut
The markup used to display the uploaded image results in a Phoenix.LiveView.HTMLTokenizer.ParseError error. lib/pento_web/live/product_l...
New
tkhobbes
After some hassle, I was able to finally run bin/setup, now I have started the rails server but I get this error message right when I vis...
New
New

Other popular topics Top

ohm
Which, if any, games do you play? On what platform? I just bought (and completed) Minecraft Dungeons for my Nintendo Switch. Other than ...
New
Exadra37
Please tell us what is your preferred monitor setup for programming(not gaming) and why you have chosen it. Does your monitor have eye p...
New
PragmaticBookshelf
Design and develop sophisticated 2D games that are as much fun to make as they are to play. From particle effects and pathfinding to soci...
New
AstonJ
Curious to know which languages and frameworks you’re all thinking about learning next :upside_down_face: Perhaps if there’s enough peop...
New
AstonJ
This looks like a stunning keycap set :orange_heart: A LEGENDARY KEYBOARD LIVES ON When you bought an Apple Macintosh computer in the e...
New
AstonJ
In case anyone else is wondering why Ruby 3 doesn’t show when you do asdf list-all ruby :man_facepalming: do this first: asdf plugin-upd...
New
gagan7995
API 4 Path: /user/following/ Method: GET Description: Returns the list of all names of people whom the user follows Response [ { ...
New
foxtrottwist
A few weeks ago I started using Warp a terminal written in rust. Though in it’s current state of development there are a few caveats (tab...
New
New
PragmaticBookshelf
Author Spotlight: Karl Stolley @karlstolley Logic! Rhetoric! Prag! Wow, what a combination. In this spotlight, we sit down with Karl ...
New

Sub Categories: