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 / Tier 1: Function to find array length ?

Author
Message
allnewmike
11
Years of Service
User Offline
Joined: 28th May 2012
Location:
Posted: 31st May 2012 02:32
I have an array of buttons which I want to iterate through to check for input. I can't seem to find any array functions... I'm guessing there is no way to check the length of an array ? This was easy in Darkbasic.

Does anyone know if there is a way to check the array length, or if there are any plans to add more functionality to arrays ?

Mike
allnewmike
11
Years of Service
User Offline
Joined: 28th May 2012
Location:
Posted: 31st May 2012 02:52
I've just found the GetSpriteHit() command, so I'll use that instead of checking each sprite. I like the options Darkbasic had for manipulating arrays though.
Marl
12
Years of Service
User Offline
Joined: 19th Nov 2011
Location: Bradford, UK
Posted: 31st May 2012 06:22
Surely if you dimmed the array, you know how big it is

I use a UDT with a flag indicating if the item is used, a global variable indicating the top one used and a number of functions to manage the array.

This example is for timers, but the principal is the same


There is a routine which finds free entries.


When a free counter is found, it is compared to the global variable used for the highest in use.



Similarly, when an entry is "deleted", a check is made to see if top counter needs to be moved down.



The SetTopCounter() function scans down the array from the current top, looking for the next used one and marks that as the top.

Then during the game, the array is only parsed as high as the value indicated in the global variable.



It's a bit more work to set up, but it manages itself after that, you simply add and remove entries using the relevant functions and never have to worry about it again.
Hodgey
14
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 31st May 2012 10:59
Quote: "Surely if you dimmed the array, you know how big it is"

That's true however, there are languages (Java for example) that keep track of the size for you while, in AppGameKit, you have to track it yourself. Being able to get the array length is a nice-to-have.

baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 31st May 2012 14:40 Edited at: 31st May 2012 14:42
It would be nice if AppGameKit kept track of the size of an array and had a "for each" loop.

Currently I tend to store the size of an array in the first (zero) slot. It's really simple to keep track of stuff that way and lets me think in a logical way. For example I get easily confused by having an array dim'd at 21 for 22 objects. I'm a little "dim" that way, so here's the solution I use for dynamic arrays:

Create the array:


Insert an object into the array:


Remove an object from the array:


EDIT: I haven't checked that code, give me a moment...

EDIT 2: Seems OK

Rampage
16
Years of Service
User Offline
Joined: 4th Feb 2008
Location: New Zealand
Posted: 31st May 2012 14:43 Edited at: 31st May 2012 23:55
Hodgey is right. If you were to add additional data to an array, an easy way to cycle through each element is like so (in java anyway):

I'm not sure as about this dimming business that AppGameKit Tier 1 uses as a convention, but in C++ you generally don't.
As I use AppGameKit Tier 2, a standard method for finding the array length is to use the sizeof() operator (only in the function that the array is located in though). e.g.


(this would return a value of 5 into the size integer)
...and then proceed to use the size variable as intended...

Hope it helps. Somehow.

Regards,

Max
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 31st May 2012 14:51
I think the main problem is that AppGameKit needs to work the same way on all platforms so you're inheriting the worst case from each native language. It's what stops the language from being really incredible but also impossible to avoid.

Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 31st May 2012 16:56 Edited at: 31st May 2012 16:56
allnewmike, eventually we may see good array handling functions in AGK. It's still growing. Add it as a request for functionality in the google issues page: http://code.google.com/p/agk/issues/list

baxslash, as always, you post great solutions. Your use of the zero position for size is cool. It is also how Perl handles strings when you look at them as arrays of characters. As an old-time C/C++ programmer, even when I dim something 10, I force my coding to use only 0-9. This is for the eventuality that I convert my apps to Tier2. I'd hate to have to look everywhere to change the number base.

Rampage, I wish we could store function names in UDTs in Tier1. At this stage, we cannot even put arrays in them.

I'd be doing straight Tier2 programming, except I really want the ability to port the same code to multiple platforms. I hope to eventually port everything to the appropriate native environments.

Cheers,
Ancient Lady
Ashambles
11
Years of Service
User Offline
Joined: 9th May 2012
Location: Georgia, USA
Posted: 31st May 2012 21:00
It's been a lot more challenging than I expected to shift from an object-oriented mindset with other languages to a more procedural mindset with Tier 1. It's not even classes and objects and such that bother me as much as the lack of any kind of information hiding or encapsulation. This issue with keeping track of an array's length is a good example. Passing everything around in function calls or global variables makes me feel like my underwear is showing

So now I have a reason to try learning C++ again!

Twitter: MuseHill
Rampage
16
Years of Service
User Offline
Joined: 4th Feb 2008
Location: New Zealand
Posted: 31st May 2012 23:58
I never settle for anything not Object Orientated these days.

Just won't do. OOP is gold.

Regards,

Max
JimHawkins
14
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 1st Jun 2012 02:02
@Ashables and Rampage

I'll probably get flamed yet again for saying this, but you're absolutely right in my opinion: Basic is a badly-designed language for anything that's not a toy or a quick prototype. AppGameKit gives some very nice encapsulation of OpenGL but it is not actually necessary to have loads of global arrays etc to use it: use C++ or Pascal. The compiler will then be your helper.

-- Jim
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 1st Jun 2012 11:07
I agree that AppGameKit would benefit from a number of features from OOP languages but unless all of the native platforms can use it too it's going to be pretty hard to squeeze into the basic language.

I do disagree that the basic language is only useful for a toy or quick prototype though and I intend to prove it over the next six months or so as I'll be making quite a few more serious projects. Not a flame, just the intention to prove what AppGameKit Basic is really capable of. Challenge accepted

JimHawkins
14
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 1st Jun 2012 11:26
There's plenty of OOP stuff on all platforms. It's more a case that it need a radical re-design, which is not on!

-- Jim
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 1st Jun 2012 11:35
Quote: "Basic is a badly-designed language for anything that's not a toy or a quick prototype"


I understand why from a programming perspective that you would think this, OOP lets you program like you think - all over the place! Unfortunately OOP also invites and is designed for spaghetti coding.

But for efficiency, procedural programming is king. You only do what you need to do, there's no redundant code make gazillions of checks for something that may never happen. If you have grown up with Procedural languages, you appreciate what OOP is actually doing behind the scenes, but in a manner that is far less in control of the coder.

Inside the brain of OOP:
Did button 1 get focus?
Did button 1 lose focus?
Did button 2 get clicked?
Did button 3 get double-clicked?
Did the mouse get released over button 1?
Did the mouse get pressed over button 1?
Did the mouse hover over button 1?
Did the mouse stop hovering over button 1?
etc
etc
Repeat for all controls
Do something useful
Repeat forever

JimHawkins
14
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 1st Jun 2012 14:56
That is, frankly, complete and utter rubbish.

OOP was designed to remove spaghetti coding. I have zero spaghetti. Basic is the ultimate spaghetti language.

I think you are confusing event-driven systems with Object Orientation. A Windows program does not poll around the entire range of objects - it responds to events, which are essentially interrupts. Whichever way you code it, something is in a tight loop checking for messages from the OS.

OOP is about structure and clarity, and nothing to do with polling.

-- Jim
Marl
12
Years of Service
User Offline
Joined: 19th Nov 2011
Location: Bradford, UK
Posted: 1st Jun 2012 15:18
Quote: "I have zero spaghetti. Basic is the ultimate spaghetti language."

Surely this is down the the programmer.

string.length() vs length(string)

Not much in it for me.

I too am spaghetti free, but that is down to a disciplined programming rather than a nanny programming language

Don't get me wrong, I can code in both, but you choose the tools for the job.

I chose AppGameKit because of what it is designed to do - cross platform, code and go - not the way it does it.

Arguing over which system is better is pointless, AppGameKit is procedural so I use procedural programming.
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 1st Jun 2012 15:22
I'm not really experienced enough in OOP to make an informed comparison. I have been using C# for around 2-3 years now and find it has advantages and disadvantages. I read this quote and find it very true:
Quote: "OOP is to writing a program what going through security is to flying"

Something like that anyway. It often feels like I have to jump through a lot of unnecessary hoops, but it has also made some operations much simpler.

The idea of using an "object" is not so different to using a "type" with some fairly handy additional functionality, but I certainly haven't seen enough of a difference in the languages to warrant the amount of hype OOP seems to get.

The longest plugins I've written in C# aren't more than three or four thousand lines and so far I haven't experienced my code turning to spaghetti. They tend to end up quite similar (generally) to how I code in basic, clever syntax aside.

Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 1st Jun 2012 16:10
BatVink, you are thinking of the event driven stuff done in many windows type apps.

With basic, I have to manually check to see if any control I have placed on the display is clicked on every sync cycle if I want to catch it. So, I'm doing all those checks deliberately.

I've done a lot of procedural OOP over the years. OOP allows you to load things into nice bubbles that handle the world all by themselves.

I've also worked with a lot of event driven stuff (satellite world demands it).

As Marl says, it comes down to the programmer.

I'm sort of unusual in a programmer. I don't have a preferred computer, OS or language. While I don't do people languages well, I learn computer languages relatively easily (but I can't tell you what day, month or year it is half the time and don't ask me to point left or right). So, I'll use whatever tool happens to be the one available and appropriate for the situation.

I'm currently doing my game in Tier1 because of the flexibility of porting it to all the platforms.

Once I get my first game out, I might try converting it to Tier2. And, having done that, I might have the basis for future games ready in Tier2. I'll be curious to see the performance differences (if any).

And I hate spaghetti code. I've cleaned up enough such code from other programmers to give me nightmares for years.

Cheers,
Ancient Lady
JimHawkins
14
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 1st Jun 2012 16:33
I'm not so besotted with OOP that I want to marry it, but it does provide things that can make large projects more clearly structured and much easier to maintain. Most of all, encapsulation, constructors and destructors, and, of course, private and public functions, inheritance, and overriding.

Personally, I think C++ is a ghastly mess created by Soustrup as a kind of messy addition to C. C# is much better, probably because it was designed by Anders Hejlsberg, the main architect of Turbo Pascal and Delphi.

Maybe we should have a language discussion topic - just for fun!

I do see a lot of posted and example code that's extremely repetitive and confusing, and this could be eliminated with some object design. Why write it three hundred times if you could write it once?

-- Jim
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 1st Jun 2012 16:44
Quote: "Why write it three hundred times if you could write it once?"


Boy do I agree with that sentiment. I have a set of PHP classes I use in all the sites I do for clients and it makes some things so much easier to code when you can just plop in something that already works.

I have one Tier1 functions file that I'm likely to use in all projects. I just wish that you could define a global in an include file and have it recognized, at least, within that file. Hey, maybe I should put that in the googles list?

Cheers,
Ancient Lady
Marl
12
Years of Service
User Offline
Joined: 19th Nov 2011
Location: Bradford, UK
Posted: 1st Jun 2012 16:49
Quote: " I just wish that you could define a global in an include file and have it recognized, at least, within that file."

Why can't you?

I do this all the time.
JimHawkins
14
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 1st Jun 2012 16:59
Apart from the tedium of writing lots of repetitive code, if you discover that a particular line of code is wrong you have to go and modify it everywhere. We've all done this. You always miss one. If it's nicely concentrated in a little bit of functionality, one fix fixes everything. Maybe!

-- Jim
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 1st Jun 2012 17:00
I think what you have to remember Jim is that a lot of people who visit these forums (including myself) are not trained programmers and don't have the years of experience that you have. It takes time to be able to program neat, non-repetitive code and I have to stop myself from doing it a LOT.

All I am trying to say is that you can't judge what a language is capable of based on a few hobbyist code examples.

Looking at the examples of OOP:
Encapsulation is just a way of hiding local variables and methods that you don't want to be seen or used outside a particular object. That's not something I see as much of an advantage in a relatively simple program like a game.

Constructors and destructors could be emulated using UDT's and a simple function in basic. I know they wouldn't have quite the same range of uses but it's a pretty basic concept, and again unnecessary in a simple program.

Private and public functions like encapsulation are really handy for stopping something from being used out of context but isn't that the programmer's responsibility? I think I know which bits if my own game do what.

Overriding functions would be great. There are many uses for this and it would be a handy thing to have but how often is it needed?

All of these have uses (mainly in large projects where multiple programmers are working on the same code) but does the lack of them ruin a language and make it useful only for playing around with? I think that in some cases it improves a programmers skills to not have all these tools to hand. To force the programmer to think how these tools work and find their own way to implement them.

JimHawkins
14
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 1st Jun 2012 18:05
Fair enough. I won't bang on about it.

Think about this: you have an adventure game with 500 rooms, four hundred NPCs and a loads of levels. Yo want your code-base to be as simple as possible. You want your NPCs to know how to move, fight, hide, die. Making classes with inheritance makes this much, much easier to control.

-- Jim
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 1st Jun 2012 18:20
Quote: "Making classes with inheritance makes this much, much easier to control."

Very true. I guess scale is the key factor.

BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 2nd Jun 2012 15:35
Quote: "That is, frankly, complete and utter rubbish."

I am not talking from one side of the fence, I code in everything from IBM RPG (predates Basic by 20 years) to Java. I can say from experience that you can shoot yourself in the foot with any language, but when you do it in OOP, it has a tendency to ricochet to the brain.

Quote: " it responds to events, which are essentially interrupts"

...and every interrupt must be polled for its state, an overhead. It is acknowledged that interrupts degrade quickly with quantity, to the point of stalling (you can't fit any work in because there is too much to poll), unless you add additional handling,another overhead.

Quote: "OOP is about structure and clarity, and nothing to do with polling"

...for the programmer, which is my point. For the system, it's an overhead.

Quote: "With basic, I have to manually check to see if any control I have placed on the display is clicked on every sync cycle if I want to catch it. So, I'm doing all those checks deliberately"

I agree entirely. So when a entity doesn't need checking, you ignore it, you have control over what you do and don't check. In essence you switch your polling of that entity off.


Quote: "Apart from the tedium of writing lots of repetitive code, if you discover that a particular line of code is wrong you have to go and modify it everywhere. We've all done this. You always miss one. If it's nicely concentrated in a little bit of functionality, one fix fixes everything. Maybe!"


I agree, but it's true of all languages. I have to maintain some Java code written by a development team that repeats the same code, and same mistakes across multiple classes,and multiple times within each class. It's down to the programmer again.

Quote: "Constructors and destructors could be emulated using UDT's and a simple function in basic. I know they wouldn't have quite the same range of uses but it's a pretty basic concept, and again unnecessary in a simple program.

you have an adventure game with 500 rooms, four hundred NPCs and a loads of levels. Yo want your code-base to be as simple as possible. You want your NPCs to know how to move, fight, hide, die. Making classes with inheritance makes this much, much easier to control.
"

Inheritance is easier, agreed. But I have to add that this and this isn't that difficult.


I think my comment on Spaghetti code has overwhelmed the point of my post, and my use of a button to illustrate interrupts/polling taken out of context (a button is a class, and it's easy to visualise). My post was simply to refute:
Quote: "Basic is a badly-designed language for anything that's not a toy or a quick prototype"


All languages have their good and bad points, I use the full range, and sometimes I have the luxury of selecting which one I want for a particular task. I'm using .NET and it's OOP elements for an issue-monitoring tool right now which I built from scratch...speed was of the essence.

If you sit a Tier 1 and a Tier 2 programmer side by side, they'll win/lose the programming battle based on the task at hand. And don't forget, the FPSC engine is DarkBASIC Pro, and Evechron Mercenaries is DB Classic!


Quote: "Maybe we should have a language discussion topic - just for fun!"

I like that idea!

In summary, I'm not disagreeing with the good things about OOP, just don't dismiss highly efficient procedural languages, which are still running most of our world due to their stability and robustness


Finally a real quirky one for you. IBM's RPG ILE IV is a procedural language, yet it includes Dynamic Binding which is a core element of OOP. But C++ doesn't have Dynamic binding. The reason why? Because early adopters of C++ decided that it was too far removed from the procedural model they understood!

Login to post a reply

Server time is: 2024-05-03 01:20:12
Your offset time is: 2024-05-03 01:20:12