1

I am trying to build a scene manager that lets you push scenes onto a stack. When each scene is popped off the stack, it is run until stopped and then we repeat.

An example is a menu in a game; which is one scene. When you close it, the game map behind it is another scene.

pub trait Scene {
    fn start(&mut self) {}
    fn update(&mut self) {}
    fn stop(&mut self) {}
    fn is_active(&self) -> bool {
        return false;
    }
}

pub struct SceneManager {
    scenes: Vec<Box<Scene>>,
}

impl SceneManager {
    fn new<T>(scene: T) -> SceneManager
    where
        T: Scene + 'static,
    {
        SceneManager { scenes: vec![Box::new(scene)] }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    struct Sample {
        running: bool,
    }

    impl Scene for Sample {
        fn start(&mut self) {
            self.running = true;
        }

        fn update(&mut self) {
            if self.running {
                self.stop()
            }
        }

        fn stop(&mut self) {
            self.running = false;
        }

        fn is_active(&self) -> bool {
            self.running
        }
    }

    #[test]
    fn test_is_running() {
        let scene_manager = SceneManager::new(Scene);
    }
}

The Scene trait is implemented for some structure that contains some way to tell if that scene is running or not. In this case, a structure called Sample.

You implement the Scene for Sample and then push that scene to the scene manager).

error[E0423]: expected value, found trait `Scene`
  --> src/engine/scene.rs:48:47
   |
48 |         let scene_manager = SceneManager::new(Scene);
   |                                               ^^^^^ not a value

Not exactly sure what to do at this point. How do I get my scene on to the "stack" of scenes? I implemented the new function of SceneManager to take a type where the type matches a Scene definition (if I understood that correctly). This alleviates me of having to specify a specific size and thus allowing me to push it to the heap instead of the stack.

What am I doing wrong and how do I alleviate the problem at hand and what does this even mean?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
TheWebs
  • 12,470
  • 30
  • 107
  • 211
  • You will probably want to initiate it as `let scene_manager = SceneManager::new(Sample{running:false});`. Here Scene is the `name` for a trait, But new function accepts a value that is of type `Scena` – Malice Aug 09 '17 at 04:30
  • 1
    What did you expect `SceneManager::new(Scene)` to **do** in the first place? – Shepmaster Aug 09 '17 at 12:47

1 Answers1

4

Here Scene is the name for a trait, but SceneManager::new accepts a value of type Scene. You will probably want to do this

let scene_manager = SceneManager::new(Sample { running: false }); 
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Malice
  • 1,457
  • 16
  • 32