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.

AppGameKit Classic Chat / Best Practices

Author
Message
kaband
20
Years of Service
User Offline
Joined: 22nd May 2003
Location: Chicago
Posted: 10th Sep 2011 01:12
I thought it would be great if some of the more experienced programmers could maybe share some best practices when it comes to programming games. If it directly relates to AppGameKit . . . even better!

I am new to AppGameKit and game programming. I have some background in other languages, but certainly not game related. I'm hoping the more experienced guys might be willing to share.

I have a couple questions could get things started.

1. When is it better to use a function over a subroutine or vice versa?

I prefer functions over subroutines, but maybe I am missing something.

2. When should you delete sprites?

I've just been making them invisible, if I no longer need it displayed on the screen. However, my first game is just cards so there is no real reason for me to delete a sprite.

Any contributions would be greatly appreciated. Thanks
Bursar
15
Years of Service
User Offline
Joined: 17th Sep 2008
Location:
Posted: 10th Sep 2011 01:24
I can't give you a technical answer about routines vs functions, but I always use functions. They just seem neater to me

With regards to deleting sprites, I guess it depends on what you're doing. If you were writing a bullet hell shooter, then there is likely to be quite an overhead in creating and deleting tens of bullet sprites every frame. In this instance I'd create a bunch of sprites at the beginning, and recycle them by making them invisible when they expire and then repositioning and making them visible as I need them again.

But between levels I'd delete all the sprites I'd used to free up memory and create a fresh batch for the next level.
Hodgey
14
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 10th Sep 2011 05:56 Edited at: 10th Sep 2011 05:59
Quote: "1. When is it better to use a function over a subroutine or vice versa? "

If you need a value returned to you based on parameters then I'd say functions are better to use. An example would be a function to get the distance between two sprites. You could have something like:

distance# = Get_Distance (x1,y1,x2,y2)

This wouldn't be too pretty as a subroutine. Try it if you like, create a couple of sprites and get the distances between each of the sprites. I think you'll find the function to be a lot easier.

I, like Bursar, use only functions because they are neater in a sense but are also supported by C++ unlike subroutines used in this sense.

I have also heard users say that functions are called (or executed) faster than subroutines. I can't confirm this however so if someone else would like to?


A good practice is to optimize your code as much as you can i.e make it perform as efficiently as possible. Since joining the App Developers Group I've learnt the importance of optimizing code. It is most likely that your development machine will run any program you write in AppGameKit quite smoothly but what about when you port it to devices like netbooks, iphones etc which are far less powerful?

Let's say you are positioning a sprite based on it's width each loop and it doesn't change size e.g SetSpritePosition( sprite, x + GetSpriteWidth(sprite), y ).
Each loop the GetSpriteWidth() function is called and it has to go through every process necessary to return the sprite's width. What you could do is store the width in a variable before the loop begins and so instead of the program calling the GetSpriteWidth function each loop it calls upon the value of the variable instead (which we can safely say is a lot faster than calling a function). So you would have something like this:
SetSpritePosition(sprite, x + sprite_width, y)
NOTE: this specific example will only work with sprites that don't change size.

That's all I've got for now, happy coding.

kaband
20
Years of Service
User Offline
Joined: 22nd May 2003
Location: Chicago
Posted: 10th Sep 2011 18:36
Thanks guys. This is great stuff and extremely helpful.

Login to post a reply

Server time is: 2024-04-19 20:49:47
Your offset time is: 2024-04-19 20:49:47