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

Pass extra data to Bsp::traverse callback? #227

Open
keisetsu opened this issue Aug 9, 2016 · 2 comments
Open

Pass extra data to Bsp::traverse callback? #227

keisetsu opened this issue Aug 9, 2016 · 2 comments

Comments

@keisetsu
Copy link

keisetsu commented Aug 9, 2016

libtcod seems to have an argument for extra data to be passed to the traverse callback method (used in the python explanation of creating a map with bsp trees), and I see something for it in the c wrapper, but I don't see any way to use it. Is this possible, and if so, how?

@zsparal
Copy link
Collaborator

zsparal commented Aug 9, 2016

We are actually using the data argument to pass the closure itself. You should be able to capture any data you need, as shown in the BSP docs:

let mut counter = 0;
bsp.traverse(TraverseOrder::PreOrder, |node| {
    counter += 1;
    true
});

Here you can use counter as you would the data argument in the C API's case (only safer, because your code is still being checked by the borrow checker).

@keisetsu
Copy link
Author

keisetsu commented Aug 9, 2016

Thanks. I was having a problem using a mutable reference in a function call, but I think I can do something like this:

fn traverse_callback(node: &mut Bsp, map: &mut Map) -> bool {
    ...
    true
} 

let mut map: Map = vec![...];
let bsp: mut Bsp = Bsp::new_with_size(0, 0, 20, 20);
bsp.traverse(TraverseOrder::InvertedLevelOrder, |node| {
                 traverse_node(node, &mut floor);
    });

May be something to go in the documentation. This is probably obvious to experienced rust programmers, but I was expecting to be able to provide extra arguments, then provide a callback function instead of a closure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants