#Using a function from inside an implementation for a struct as a start up system.

12 messages · Page 1 of 1 (latest)

scarlet moss
#

Pretty much what the title says.

fn main() {
App::new()
.add_plugins(DefaultPlugins.set(
WindowPlugin {
primary_window: Some(Window {
title: "NEA".into(),
mode: bevy:🪟:WindowMode::Windowed,
position: bevy:🪟:WindowPosition::At((-15,0).into()),
resolution: (1600.0,922.0).into(),
..Default::default()
}),
..Default::default()
}
).set(ImagePlugin::default_nearest()))
.add_systems(Startup, VerletObject::setup)
.run();
}

#[derive(Component)]
struct Shape;

struct VerletObject {
position_current: Vec2,
position_old: Vec2,
acceleration: Vec2,
}

impl VerletObject {
fn setup(
&mut self,
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
commands.spawn((
PbrBundle {
mesh: meshes.add(shape::UVSphere::default().into()),
material: materials.add(Color::rgb(0.0, 1.0, 0.5).into()),
transform: Transform::from_xyz(
self.position_current[0].into(),
self.position_current[1].into(),
4.0,
),
..default()
},
Shape,
));

    commands.spawn(PointLightBundle {
        point_light: PointLight {
            intensity: 9000.0,
            range: 100.,
            shadows_enabled: false,
            ..default()
        },
        transform: Transform::from_xyz(8.0, 16.0, 8.0),
        ..default()
    });

    commands.spawn(Camera3dBundle {
        transform: Transform::from_xyz(0.0, 6., 12.0).looking_at(Vec3::new(0., 1., 0.), Vec3::Y),
        ..default()
    });
}
#

When trying to build it throws the error: "the trait IntoSystem<(), (), _>` is not implemented for fn item for<'a, 'b, 'c, 'd, 'e> fn(&'a mut VerletObject, bevy::prelude::Commands<'b, 'c>, bevy::prelude::ResMut<'d, bevy::prelude::Assetsbevy::prelude::Mesh>, bevy::prelude::ResMut<'e, bevy::prelude::Assetsbevy::prelude::StandardMaterial>) {VerletObject::setup}"

bleak cloak
#

first of all, your function is a method on VerletObject, but you pass is as a static function
I.e. setup has 4 arguments: self: &mut VerletObject, ...
What does bevy need to put inside the self?

#

There are two solutions:

  1. (probably preferred?) Make a resource, and then you could make a wrapper:
App::new()
  .insert_resource(VelterObject::new())
  .add_systems(wrap_setup);

fn wrap_setup(verlet: ResMut<VerletObject>, args...) {
  verlet.setup(args..);
}

// not sure this would work but you can try
.add_systems(|verlet: ResMut<VerletObject>, args...| verlet.setup(args...));
#
  1. Create new object inside main, and wrap around that:
fn main() {
 let verlet = VerletObject::new();
 App::new().add_systems(|args...| verlet.setup(args...););
}
#

You can notice you're doing something wrong, because you don't ever init VerletObject with something inside your code, so even if bevy somehow would run your function, what self.position_current[0] should equal to? You never init it anywhere

#

What exactly do you want to achieve?

scarlet moss
#

I'm trying to render a 3d circle with the points that the verlet object calculates

#

I could perhaps move the setup function outside verlet object

#

Which was what I was trying to achieve at first

#

But since I am relatively new to rust and bevy I could not figure out how to use the variables from the struct in the function

bleak cloak
#

Make them public, and accept them in a function