Skip to content
This repository has been archived by the owner on Jan 2, 2025. It is now read-only.

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

Closed
keisetsu opened this issue Aug 9, 2016 · 3 comments
Closed

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

keisetsu opened this issue Aug 9, 2016 · 3 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.

@tomassedovic
Copy link
Owner

This project has not been maintained for a few years. I'm closing all pull requests and issues before archiving the repo on github. Sorry I hadn't done this sooner!

@tomassedovic tomassedovic closed this as not planned Won't fix, can't repro, duplicate, stale Jan 2, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants