#Problems with code architecture

1 messages · Page 1 of 1 (latest)

topaz meteor
#

I am using multiple movement types in my game such as platformer and top down and someone here suggested to me to make a main script attached to my rigidbody2d that uses:
func _physics_process(delta: float) -> void:
current_state.run(delta)
and then I change the movement type by changing the current_state variable which is a Node2D.
This however has now gotten really messy, since I also have subtypes in some scripts for example I have a forced movement style where the player moves without input and I want to be able to make it for example follow nodes on a board, but also be able to turn that off and turn on a sequence of a few certain movements. Also running all of this code through a _physics_process doesn't seem logical for something like the sequence of a few certain movements since I'd rather just call it as a function instead once.
How could I clean this code up or go about doing this neater?

Thanks for the help in advance !

atomic hemlock
#

Consider crearing several scripts with a movement mode for each.
So you can swap the character's node and/or script when you need to.

topaz meteor
#

That's what I'm saying, I have that but they're all powered by that physics process and also got subfunctions in certain movetypes like the forced movement

atomic hemlock
#

Each mode will lokely have several states of their own, so it could grow way too big to handle

short sand
#

not sure if i catch what you are saying, but inside run you don't have do anything. when you change a state, you can use like an enter() function and a exit() function which you can override. then for certain states you don't actually want to run all the time, you just do what you need to do when you enter/exit or have some sort of specific event they listen to when you do inputs.

#

it depends on your game also, doing a state machine in a movement parkour game is different then a board game.

topaz meteor
#

It is a party game which consists of a board phase, a roll phase, and topdown and also platformer movement in minigames.
I looked into the enter and exit functions and this does seem like this would solve the only calling something once, but then I would still be calling that enter function using the physics_process in my main? Otherwise I think it might get complicated again having to manually say whether to do a physics_process or a single call from my main script

short sand
#

why can't you just call the run function and then not have it do anything? point is you call it either way, and if doesnt have anything inside then nothing happens.

but yeah, since your game is abnormal you can decide to go different routes.

it seems like you can have one state machine for every super state, like i am now playing topdown mode, platformer, board etc.

within each of those, they can alao have some states of their own - like as a platformer he can have his own completely seperate movement state machine.

so if it were me i would do one state machine for organizing the super states of the game, that mostly only does enter/exit functions to setup between the states, and then within those you can have more smaller specific smaller state machines catered for each movement style(if needed)

topaz meteor
#

Sounds good. I am a bit lost on your not doing anything in the run function though. The run function is being called constantly inside the main script by a _physics_process and the run function is given delta as a parameter. Then it is constantly called right? How would I make it only call it once and be able to still perform _physics_process like movement inside it?

short sand
#

the run function in your current state is different for each state

#

if you want the state to not do anything in the run function just dont put anything in it

topaz meteor
#

but every super state is called from the main by activating their run function. if I leave it empty the state would basically just do nothing forever

short sand
#

well then put a condition there in case you want to leave it, or as i said if you dont want to use that function you can use other functions like _input, some signal, something happening, whatever you want