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

Fix gamestate loading spaghetti #37

Open
tatjam opened this issue Oct 20, 2022 · 2 comments
Open

Fix gamestate loading spaghetti #37

tatjam opened this issue Oct 20, 2022 · 2 comments
Labels
code-improvement Improvements to the code base which don't change the game good first issue Good for newcomers

Comments

@tatjam
Copy link
Collaborator

tatjam commented Oct 20, 2022

There's a bit of a circular loading dependency when loading a gamestate, where entities require osp->game_state is already set during loading. Fix this by rearranging when the entities create the lua env (assign osp->universe, etc...) so that the load order makes more sense

@tatjam tatjam added good first issue Good for newcomers code-improvement Improvements to the code base which don't change the game labels Oct 20, 2022
@tigercoding56
Copy link

tigercoding56 commented May 8, 2023

something to attempt over summer break , i see (wishful thinking on my part (i wish i could actually , code without 1000th bugs )) -- where is the loading implemented (what file? )
not that i will actually get anywhere (the most complicated thing i ever did in c++ was a remake of flappy bird for intro to CS )

@tatjam
Copy link
Collaborator Author

tatjam commented May 8, 2023

The loading code right now is a bit of a mess, but you can find the "start of it" here:

game_state->init();

If you follow the different calls you can quickly get to the real loading logic, which is mostly on GameState.cpp

A good example of spaghetti is the fact that osp->game_state (which you would expect is set on the launch_gamestate function!) is in fact set by the GameState itself:

osp->game_state = out;

This is kind of required because otherwise as entities and scenes are created, their lua code would run and that code may attempt to access osp->universe, which would not be set as the Universe belongs to the GameState. This circular dependency means that OSP may not have an universe after until the GameState load function has finished (as that creates the universe), but the GameState load may cause some lua code to run which will access osp.universe and crash the game.

The current solution is to set osp.universe within GameState (while the universe is being populated with entities), but a more elegant solution would be to separate the loading logic from any execution of lua code. For example, moving the Entity constructor lua loading code to an external function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code-improvement Improvements to the code base which don't change the game good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants