About statemachine or fsm to manage some gamelogic or game object

Hello About game state manage and change game state to manage different game logic and display I have a question I have asked similar questions before, and I used a relatively simple method based on the answers of the helpers.

the easy way is use many define boolan vars or string like called ‘serve_state’ to ‘play_state’ but the project become bigger and bigger.this way is hard to manage because use too many state string ,also no start()/exit() some situation need manager by myself.

so I ask ai and ai say use a fsm creat a statemachine lua module and base class and some different state lua scipts like serve_state.lua and play_state.lua , then use statemachine to change and I also don’t know if I use this way and how to according different state to manager every gameobject in collection.

Is it a good practice/good methods? and guys what method to manage game state?

I don’t know and very confuse=.=

Is any about the defold fsm exemple?thanks guys

my last practice proj code

-- main/game_state.lua
local M = {}

M.selected_player = 1     -- 存储玩家选择的索引 (1: 普通, 2: 大, 3: 小)
M.game_state = 'title_state'    -- 游戏默认状态
M.player_score = 0              -- 游戏结算分数
M.level = 1

function M.get_state()
    return M.game_state
end

function M.set_state(new_state)
    M.game_state = new_state
end
 -- ball.script
and show some my practice code use defold about the game state question like this ↓
function update(self, dt)
    if game_manager.get_state() == 'serve_state' then
        local paddle_pos = go.get_position(paddle_id)
        paddle_pos.y = go.get_position().y
        go.set_position(paddle_pos)
    elseif game_manager.get_state() == 'play_state' then
        local pos = go.get_position()
        pos = pos + self.current_speed * self.direction * dt
        go.set_position(pos)

        -- 限制球的最大速度
        self.current_speed = lume.clamp(self.current_speed, self.init_speed, 1100)

        -- 检测球是否死亡
        if pos.y < 0 then ball_dead(self) end
        -- 死亡便重新发球
        if self.dead then reset(self) end
        -- 检测是否无法继续游戏
        if self.hp <= 0 then
            msg.post('.', 'disable')
        end

        if self.hp == 0 then
            player_dead(self)
        end
    end

    -- === 调试模块 ===
    -- debugger(self)
    -- print(self.current_speed)
end

I do not see anything really wrong with that approach. One issue might be that you use strings to represent the state and this might open up to issues with typos.

Thank you for your answer! Yes, I have encountered problems caused by spelling errors. I think this is relatively simple state management. Could you please tell me what methods are used for more advanced, mature, or formal projects? Also, how do I get started studying and using the Lua FSM library? Are there any guidelines to guide me in understanding and researching it myself?

Will you develop a visual state machine for animated objects later? I think it might be more beneficial for those who enjoy artistic expression when creating complex animations. Just curious. =。=

They are very simple, just one of them: https://github.com/kyleconroy/lua-state-machine

2 Likes