C++ SFML | SoloLearn: Learn to code for FREE!

+1

C++ SFML

Trying to sort out a game engine/state machine using enum class. Can't seem to figure it out, I've looked all over the internet btw.

7/6/2020 5:10:27 PM

Olivia

14 Answers

New Answer

+4

are you facing linker's problem?! if yes so tell me about your environment and ide .... leme know it first..

+2

I’m not sure about what you’re trying to do here. let’s take an example of enum go {east, south, west, north} to create a state machine, just create a vector of go vector<go> dir = {east, east, west, north}; //prints 0 0 2 3 is that what you’re looking for?

+2

I understand how enum works, I'm just lost at how to work the different levels/menu stuff into it. I know I can create headers and cpp files but I just can't order it or figure how to link it to work

+2

Again, not sure what you’re asking about declare enum in header.h include header.h in main.cpp access enum from main does this answer your question?

+2

No, I understand the enum itself But where would I declare my game states and how would I link that to my enum class header For example atm I have a menu, a main game and a win page as functions. This is messy and doesn't work if I try to loop back to main. How can I make these into a game engine linked to enum

+2

before you split the functions and classes into multiple header files, does it work with single main.cpp?

+2

Yes except for me wanting to loop win screen to menu

+2

I never made a game myself, so I have very little idea of what you’re talking about but if it doesn’t work in a single main.cpp, then why do you think splitting them will make it work I think there might be something wrong with your game logic

+2

It runs fine in main, but I can't make win screen go to menu screen because of the order of the functions. In a state machine/game engine you should be able to create whatever whenever but I don't know how to create and link it in to the enum class

+2

if you have a function that shows menu screen, let’s call it menu() what happens when you call menu() after win screen appears forget about enum class for a second

+2

I can't because win has to be above main and main has to be above menu

+2

can you move menu to the top of main? if not, why? all functions should be declared before main() this is the root of your problem, not linking multiple files

+1

When I said main I meant main game func. I currently have all 3 states in separate functions. I want to create a state machine/game engine with enum controlling which screen I'm on, game/menu/win ect. I'm using SFML in Visual Studio 2019

0

I have created a system of headers and c++ files that individually work. Except...I cant seem to find a way to switch between the states. I declared my enum in engine which includes header files to Game, Menu,Win. I have separate cpp files for input,draw,update that includes engine. Where and how could I conditionally switch states? Ideally I'd like game to switch to win when score=5 and win to switch to menu when button pressed. But where can I write this that would be able to access those variables.