GithubHelp home page GithubHelp logo

Comments (5)

leudz avatar leudz commented on June 2, 2024

There isn't any equivalent but nicer version.
World::get could exist but usually get happens in systems where you have views and not World.
(I've added more and more "shortcut methods" to World, at the beginning everything had to go through views explicitly)

If you don't check for the component then you can use indexing vs[*player_id].dirty = true; but it'll panic if the player doesn't have the component.

About performance, views are not iterators. View/ViewMut are basically references to SparseSet, a data-structure close to a Vec. So vs.get is almost like vec.get.

from shipyard.

progressiveCaveman avatar progressiveCaveman commented on June 2, 2024

Thanks for the quick reply. I see what you're saying about the sparseset.

How would this work with a function that returns an EntityID that needs to look for a component? For example, if I wanted to make a get viewshed function:

fn get_viewshed(entity: EntityID) -> Option<ViewShed> {
    self.world.run(|mut vs: ViewMut<Viewshed>| {
        return (&mut vs).get(*entity); //this doesn't work, how can I do this? 
    });
}

Looking further into the documentation, maybe I'm looking for borrow?

from shipyard.

leudz avatar leudz commented on June 2, 2024

If you return an owned type then you can do:

fn get_viewshed(&self, entity: EntityID) -> Option<ViewShed> {
    self.world.run(|mut vs: ViewMut<Viewshed>| {
        (&mut vs).get(*entity).ok().copied() // <- no semi-colon and I'm assuming Viewshed is Copy
    }) // <- no semi-colon
}

And yes you could use borrow instead.

If you want to return a reference to a component then it's basically not possible, you'd need the same kind of types that World::get would use. Something like Ref/RefMut to hold the locks.
View/ViewMut are references but also keep locks to make sure it's not possible to break borrow checking rules.

from shipyard.

progressiveCaveman avatar progressiveCaveman commented on June 2, 2024

Ok, thanks. I am starting to see more of how I can use views in my systems to get the info I need. I didn't see anything in docs about run returning the value of the closure, that might be good to add (Or I didn't read well enough)

from shipyard.

leudz avatar leudz commented on June 2, 2024

World::get is available on master et I added a mention to the return value for run.

from shipyard.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.