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.

Newcomers AppGameKit Corner / Code Structure

Author
Message
kaband
20
Years of Service
User Offline
Joined: 22nd May 2003
Location: Chicago
Posted: 16th Feb 2018 22:25
Hi!

I'm full of questions today. What is the best practice when structuring code? I use Tier 1 and try stay away from GOSUB AND GOTO. But as my program becomes more and more complex, I start to get a bit lost. I use functions in the main loop, but often times those functions refer to other functions split across multiple files based on subject - score, player,input. It can take me a bit to find the function I'm looking for when trying to troubleshoot as I click the tab for each file looking for the defined function. Feels like I'm missing some basic principles. My made up methodology doesn't scale very well. Any advice all you experienced guys?

Thx!
hoyoyo80
7
Years of Service
User Offline
Joined: 11th May 2016
Location:
Posted: 16th Feb 2018 23:19
I also not good on programming, i only exposed to programming on basic base game engine. I ever finish tic tac toe and sokobon game and one simple database for my own work.

For me, when i do code, i always make a flow chart before that...hahaha...so i know the flow of my program.

And as for function,maybe naming the function with its specific purpose with comment and put related function into this function.

E.g playercontrol() and inside it will playercontrol related thing or function.
Blendman
10
Years of Service
User Offline
Joined: 17th Feb 2014
Location: Arkeos
Posted: 17th Feb 2018 09:33
Hi

I create game with lots of lines of codes.
So, I use a lot of include files.

I have a file named include.agc, in whixh i include my files :
#includefile "player.agc"
#includefile "mob.agc"
...

I try to create the functions with the filename in its name.
Eg :
in player.agc, i can have :
Initplayer (), resetplayer (), moveplayer (), eventplayer ()....

In mob.agc (= monsters) : initmob (), addmob (), deletemob (), movemob (), eventmob ()

Eventplayer () or eventmob () is the general function I put in the main loop of the level screen for exemple.

For some utilities, i have a file name util.agc, with :
Min (), max (), math functions, ...

Text function : text.agc

Menu functions : menu.agc

And so...


So my loop (in screengame.agc) is in general :



I hope this can help.
AGK2 tier1 - http://www.dracaena-studio.com
basicFanatic
6
Years of Service
User Offline
Joined: 7th Jun 2017
Location:
Posted: 17th Feb 2018 11:53
Just realized I wrote 430 lines of code without a single function!
IronManhood
9
Years of Service
User Offline
Joined: 6th Feb 2015
Location: US
Posted: 17th Feb 2018 13:38
I try to use "Go to Tag Definition" and "Find Usage" when I can to find the function or variable I'm looking for.
kaband
20
Years of Service
User Offline
Joined: 22nd May 2003
Location: Chicago
Posted: 17th Feb 2018 15:08
Thx guys these are all useful recommendations. I think changing my naming convention for my functions to include the filename in it (per blendman) even if its a few letters will help, flowchart it (per hoyoyo80) as well as Go to Tag definition (per IronManHood) - which I didnt even know existed and should have. Great stuff, thx.
Dybing
12
Years of Service
User Offline
Joined: 12th Sep 2011
Location: Bergen, Norway
Posted: 17th Feb 2018 15:14
I organize into different files depending on function of code.

so for instance everything that have to do with writing text to screen is in a file called 'text.agc', everything to do with user input is in a file called 'input.agc' and so on. Then I use descriptive naming for all functions and datatypes/variables. So say I got a function to place the game logo on screen, it's aptly called 'placeLogo()' which would be in the 'output.agc' file which handles all the nuts and bolts of handling sprites.

Also, code is organized into a hierarchy - or rather the files are. 'main.agc' handles startup and exit. Under that is 'views.agc' which handles the logic of all the different views. You know, menues, game screen and so on. On these two levels there are hardly any calls to built in AppGameKit commands. These two levels are all about composition and orchestration, not doing the legwork. That is handled in the other files.

With this hierarchy, one should keep dataflow moving only up and down, not laterally. So processed data moves up, data to be processed moves down. So say you got a player-sprite, in 'views.agc' you got a function called gameLoop, in this you have a datastructure keeping track of the state of the player and playersprite. In the loop first thing is to get the input, store this in the datastructure, then call a function to modify the data depending on state, receive datastructure back. Then finally send the datastructure to the function that actually draws the sprite. Up and down, and only once in a blue moon where it is more expedient and provide some tangible advantage laterally.

Login to post a reply

Server time is: 2024-04-20 06:22:16
Your offset time is: 2024-04-20 06:22:16