Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / State-Based Programming (SBP)

Author
Message
zenassem
23
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 25th Nov 2011 14:56 Edited at: 25th Nov 2011 23:29
I am trying to create a State-Based Manager based off a C++ version. (I plan on adapting code for a Process Manager as well); but before I move onto that, I need to make sure that I am intepreting the functionality of the C++ State-Manager correctly.

The idea is to be able to process changing states on a "Per Frame" basis. With the ability to wedge-in states like, displaying a message to the user, even during another "In-Game" routine.


The source requires IanM's Matrix1Utils plug-in



So instead of having a long SWITCH-CASE statement and numerous flags, an SBP Stack (Last In, First Out) PUSHes functions, Calls them and POPs them as needed, per frame.

I think I have the gist of it, though my test snippet could have been more thought out and could have demonstrated the functionality in a loop.

If anyone has a better method they have worked out, or can see things I may be overlooking (before I move onto the process manager) please share your insights.

~ZENassem
zenassem
23
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 25th Nov 2011 23:25
*Bump*
I'm sure someone is willing to share their secrets....

~ZENassem
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 26th Nov 2011 01:47
I can see doing something like what you've done for transient objects in-game (bullets, enemy etc), but not for general game state. If you've made the decision that you want to run the function, why not just run the function now rather than add it to a list that runs the function later. You need to take care not to over-use this functionality, as I can guarantee that it's not as efficient as simply calling the function directly.

Here's a 'for instance' that I've played with on and off for a while now: I have a short routine that adds pre-sync and post-sync handlers to run though a list of functions to call - whenever SYNC is called, they get triggered automatically.

Here's the code that adds those handlers to the list:


Once those handlers are added, that list isn't changed. Each function gets called at the appropriate place and in the appropriate order every time SYNC is called.

Each of those handlers in turn could issue their own computed function calls - in the program I took the above code from, the UpdateBulletsEvent function does that to give each bullet its own type and behaviour, but only when it decides that it's correct to do so (in this code, that's when there has been a detectable interval of 1ms or greater since the last time it was called):


However, I haven't shifted everything into the events list - adding bullets is triggered in my main loop.

The choices I would make are:
1. If it doesn't need to be called via CALL FUNCTION, then don't even if you can.
2. If it does need to be called in this way, try to make a static list that will be reused.
3. Only make dynamic calls if there is no other choice.

The further down that list you go, the less efficient your processing will be.


Here's the code for what I've called 'Sync events':


zenassem
23
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 26th Nov 2011 01:57
Understand your points. I'll have to spend a little time going over your code,, but I think you have a better gist of what I was going for without having the material in front of you. From what I have looked at already, you essentially provided the framework of the process-handler that I was going to attempt, except it seems more directed and purposeful.

I had fun in my attempt, but I can see I already went a bit in the wrong direction... I was afraid I was doing that. This will be a big help. Thanks.

~ZENassem

Login to post a reply

Server time is: 2026-07-10 01:58:56
Your offset time is: 2026-07-10 01:58:56