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 / Best practices in managing ID's?

Author
Message
Dead Pixel
9
Years of Service
User Offline
Joined: 27th Nov 2014
Location:
Posted: 9th Feb 2015 23:45
Hi all,

Just wondering if some of the more experienced users of AppGameKit could give us newbies a few pointers when it comes to managing ID's and their associated assets\resources?

Obviously when there's only a handful of them it won't be too difficult, but on a larger project it's going to become an issue to keep track of them all in an efficient manner.

From my own limited experience of the language it seems that arrays on their own and in conjunction with types are the way to go, but perhaps there is a bit more to it than just that?

A few source code examples would be appreciated.

Thanks.

[ Coding In BASIC using AppGameKit V2 ]
BatVink
Moderator
20
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 10th Feb 2015 00:34 Edited at: 10th Feb 2015 00:35
I tend to keep IDs with other associated data, in an array made of a user-defined type.

For example:



This is just a simple example made up on the fly, but the concept works well for me. As soon as you put entities into arrays, you can mass-process them in program loops.
Even if I only have one entity of a type (for example the player), I still make a type for it. It is much easier to read your own code when the player's parameters look like:

gPlayer.health
gPlayer.state
etc...

The intellisense also makes coding faster. You type in gPlayer and you get all of your parameters listed.

Of all the parameters listed above, status is most important for me. I program using the principle of a state machine. My main game loop reacts to all of the states, and branches accordingly. The game has a state (INIT, BEGIN, PLAY, END, DEINIT etc) , the player has a state (DEAD, ALIVE etc), even static entities have a state. I introduced this idea in a tutorial here (not my idea, just a tutorial on how to use it!), which can be applied to AGK.


Hope this helps. Remember, this is just my preference. I veered off the original question but the extra info may help others who are staring out from nothing. There are many ways to organise your code, other people will have other ideas, just pick the one that suits your way of thinking.

Quidquid latine dictum sit, altum sonatur
paulrobson
9
Years of Service
User Offline
Joined: 22nd Nov 2014
Location: Norfolk, England
Posted: 10th Feb 2015 09:52
Just to second BatVink here. It's not just a way to do it ; it's the way to do it. I'm not sure there is a better way of organising AppGameKit code.

Any code beyond a few lines gets chaotic if you have globals all over the place and it allows you to write functions which process whole objects e.g.



can be basically encapsulated. Not only does it make code clean, it makes it reusable and it stops lots of bugs and confusion. AppGameKit doesn't do object code really, this approach gets you fairly close to it. The thing you are missing is you cant subclass tEnemy
Dead Pixel
9
Years of Service
User Offline
Joined: 27th Nov 2014
Location:
Posted: 11th Feb 2015 00:24 Edited at: 11th Feb 2015 00:28
OK guys, thanks for claifying things.

I was doing a little bit of experimentation earlier and the following would not compile until I declared my types as global, maybe I'm mistaken but it seems they don't have global scope unless declared explicitly as such. A type is basically a blueprint from which to construct your entities.



What is the relationship, if any, of the string returned from Str(Gradient.Top)) and Str(GetColorRed(Gradient.Top)),etc...? It doesn't seem to reflect the values of the red, green, and blue components of the colour?

Thanks.

[ Coding In BASIC using AppGameKit V2 ]
BatVink
Moderator
20
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 11th Feb 2015 06:51
The type is a template like you say, available anywhere.
But the variable you make from the type must have its scope declared.

Gradient.top is returning the index of the colour, just like the index of a sprite or sound.
Regarding colours, if you set a specific color rather than a random one, do you get what you expect from GetColorRed()? Colours can be confusing once you mix the 3 components. I work with them most days in graphics work I do, and they still confuse the hell out of me. When coding, I always have my graphics app open so I can use the colour sliders to check the RGB values I need.

Quidquid latine dictum sit, altum sonatur
DavidAGK
AGK Developer
10
Years of Service
User Offline
Joined: 1st Jan 2014
Location:
Posted: 11th Feb 2015 19:13
Definitely as BatVink says re IDs. I made the mistake of trying to manage them in my tile map editor. Made the decision to re-code using the method described above and not looked back.

Using Tier 1 AppGameKit V2
Started coding with AMOS (Thanks Francois Lionet)
Dead Pixel
9
Years of Service
User Offline
Joined: 27th Nov 2014
Location:
Posted: 11th Feb 2015 22:25
Quote: "
The type is a template like you say, available anywhere.
But the variable you make from the type must have its scope declared.
"


A subtle distinction that tripped me up. Thanks, BatVink.

Quote: "
if you set a specific color rather than a random one, do you get what you expect from GetColorRed()?
"


Yes, as a test I replaced Random(0, 255) with 75 for the red component and
Str(GetColorRed(Gradient.Top)) gave me back 75.

What colour depth does AppGameKit use? A byte for each of the R,G,B components and a byte for the alpha channel makes it 32 bit - is that correct? 4 bytes per pixel?

And with another bit of experimentation I see we can have nested types which will give us a hierarchy of functionality.

Adding the following to my code.....


global myTest as aTest

type aTest

testGradient as tGradient
variableA as integer
variableB as integer

//Etc....

endtype


.....allows me to use myTest.testGradient.Top = MakeColor(Random(0,255),Random(0,255),Random(0,255))

I can see this nesting of types being very useful, but are there any pitfalls to it?

[ Coding In BASIC using AppGameKit V2 ]
BatVink
Moderator
20
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 12th Feb 2015 10:16 Edited at: 12th Feb 2015 10:17
Quote: "Yes, as a test I replaced Random(0, 255) with 75 for the red component and
Str(GetColorRed(Gradient.Top)) gave me back 75."


I think this highlights that what you see on screen and the value you retrieve are synchronised, but the way RGB works is sometimes confusing. For example in this image below, the blue contains 50% red!



Quote: "What colour depth does AppGameKit use? A byte for each of the R,G,B components and a byte for the alpha channel makes it 32 bit - is that correct? 4 bytes per pixel"
?

Spot on

Quote: "I can see this nesting of types being very useful, but are there any pitfalls to it?"


I haven't found any. I use it to store colours (I have a type for .red, .green, .blue, .alpha) and vectors (.x, .y, .z) amongst others. I've also got arrays in types...in a parent array!

Quidquid latine dictum sit, altum sonatur

Attachments

Login to view attachments

Login to post a reply

Server time is: 2024-03-28 21:14:25
Your offset time is: 2024-03-28 21:14:25