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

Added Sensible Default ggrs::Config type GgrsConfig #73

Merged
merged 2 commits into from
Oct 19, 2023

Conversation

bushrat011899
Copy link
Contributor

@bushrat011899 bushrat011899 commented Oct 19, 2023

Objective

In the box_game example, there's a to-do related to ggrs::Config:

/// TODO: Find a way to hide the state type.

Solution

I've solved this to-do by creating a type GgrsConfig:

#[derive(Debug)]
pub struct GgrsConfig<Input, Address = SocketAddr, State = u8> {
    _phantom: PhantomData<(Input, Address, State)>,
}

impl<Input, Address, State> Config for GgrsConfig<Input, Address, State>
where
    Self: 'static,
    Input: Send + Sync + PartialEq + bytemuck::Pod,
    Address: Send + Sync + Debug + Hash + Eq + Clone,
    State: Send + Sync + Clone,
{
    type Input = Input;
    type State = State;
    type Address = Address;
}

Because GgrsConfig is a concrete type, it is allowed to have defaults for the type parameters Input, State, and Address required by ggrs::Config. As mentioned in the box_game example, u8 for State is already taken as a default (since State is unused in Bevy). I also took the liberty of setting Address to SocketAddr as I believe that is the most common type that will be used by end users. These defaults allow the example game to use:

pub type BoxConfig = GgrsConfig<BoxInput>;

I show a type definition here, as I suspect end users would prefer this option for the sake of brevity. Because this is all done through the addition of a simple zero-sized-type, all existing applications can continue using their custom Config types if they want, with no breaking changes.

Zero-sized type with default parameters matching standard recommendations for use with bevy_ggrs.
Copy link
Collaborator

@johanhelsing johanhelsing left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this. For the extreme_bevy tutorial, it means:

-struct GgrsConfig;
-
-impl ggrs::Config for GgrsConfig {
-    // 4-directions + fire fits easily in a single byte
-    type Input = u8;
-    type State = u8;
-    // Matchbox' WebRtcSocket addresses are called `PeerId`s
-    type Address = PeerId;
-}
+type ExtremeConfig = GgrsConfig<u8, PeerId>;

@gschup gschup self-requested a review October 19, 2023 06:44
Copy link
Owner

@gschup gschup left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great syntactic sugar :)

@gschup gschup merged commit e576f25 into gschup:main Oct 19, 2023
1 check passed
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

Successfully merging this pull request may close these issues.

3 participants