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 / Trying to understand a Empty Array

Author
Message
Game_Code_here
3
Years of Service
User Offline
Joined: 2nd Jun 2020
Location:
Posted: 23rd Oct 2021 18:58 Edited at: 23rd Oct 2021 18:59
So I am setting up a Array with no numbers in any slots as I am using a way to create coins and rocks .

But I'm just trying to understand App Game kit and how it handles arrays so please forgive me if this sounds first grade.

So If I create a Empty Array How do I get the numbers of the sprites I create from the system and do they change every time the program is run?

Creating the Array, See it is Empty? So when I try to do anything I get warning errors as the Array is 0 and your dement ions are this.




SO I try something like this



So I have no numbers to say what coins are what for the Array.

I am just trying to learn how and why it works, there is no get sprite id and there should be for people like me lol.

I can print out the sprite id after its created but that means I have to print and record every programed sprite number I create to position them and or set them up how I want.

I looked and found stuff like .Find and other stuff but am lost in this language for a lot of Array stuff.
Heavens
8
Years of Service
User Offline
Joined: 10th May 2015
Location:
Posted: 23rd Oct 2021 21:11 Edited at: 23rd Oct 2021 21:12
Think I understand what you are asking, maybe this will help

Game_Code_here
3
Years of Service
User Offline
Joined: 2nd Jun 2020
Location:
Posted: 23rd Oct 2021 22:02
Heavens

I do believe this is what I am looking for but I'm trying to understand it is all.

My code works well, but I don't understand it.

I'm trying to make sprites but have full access to them after being created.

Thanks for your help I will take it and study it.
Game_Code_here
3
Years of Service
User Offline
Joined: 2nd Jun 2020
Location:
Posted: 23rd Oct 2021 22:10
I believe it is this that I'm looking for and makes sense but still does not give me a way to position each sprite by number, but I'm learning.

For i = 0 to CollectedCoins.Length
Heavens
8
Years of Service
User Offline
Joined: 10th May 2015
Location:
Posted: 23rd Oct 2021 22:51 Edited at: 23rd Oct 2021 22:54
You just need to loop over the array



CollectedCoins[i].ID is the number of the sprite, so you can access them by looping the array as above or directly

Game_Code_here
3
Years of Service
User Offline
Joined: 2nd Jun 2020
Location:
Posted: 23rd Oct 2021 23:24

I believe this is a way here, I am starting to understand better thank you.

But do I have to declare them as this before or no?

Heavens
8
Years of Service
User Offline
Joined: 10th May 2015
Location:
Posted: 23rd Oct 2021 23:30
This code from my first post fills the array with coins



Then you can loop over or directly access them, be careful with direct access as if you try and access an array element that doesn't exist the program will crash, that's why loop access is better as the for loop 'For i = 0 to CollectedCoins.Length' will never allow you to go beyond the array length
Game_Code_here
3
Years of Service
User Offline
Joined: 2nd Jun 2020
Location:
Posted: 24th Oct 2021 00:06
Heavens

Ok, I am just directly accessing them in my loading and only if I have to, But I see how Making most everything loaded with a array speeds up my program and makes it run a lot better.

I read that arrays store info into memory bits making the pc or device run faster, I guess this is true.

Thank you for teaching me what I misunderstood.
PartTimeCoder
AGK Tool Maker
8
Years of Service
User Offline
Joined: 9th Mar 2015
Location: London UK
Posted: 24th Oct 2021 02:11
@Heavens, your code is exactly how I go about it, an empty type array and use .insert with a temp type, never failed me yet

Quote: "be careful with direct access as if you try and access an array element that doesn't exist the program will crash, that's why loop access is better as the for loop 'For i = 0 to CollectedCoins.Length' will never allow you to go beyond the array length
"


Yes, but sometimes you don't want/need a loop, in this case you need to check the index you are passing in


Also bear in mind that loops are expensive if you can access your sprite data by NOT using a loop and use .find() you get direct access to the item you need without the expensive loop (.find() is much faster than a loop especially on large arrays)

your ID slot is the first slot (.find() works on the first type item), so for example when you have a collision the physics functions returned the sprite id, to find that id in your array



index will be -1 if the ID not found else it returns the index of the sprite in the array, now you have access to the full sprite data



Arrays can be tricky to master but its well worth spending the time to understand them as they open up a whole new realm of possibility's and code management and readability

This page has lots of array info, bookmark it

Quote: "My code works well, but I don't understand it."


LOL, the only thing more frustrating than not knowing why code does NOT work is not knowing why it DOES work








Open Source plugins
Cl - DnD Plugin
Buy Me A Coffee
Virtual Nomad
Moderator
17
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 24th Oct 2021 02:23 Edited at: 24th Oct 2021 02:34
1 point to add to make sure you understand:

...while the thread's title is "Trying to understand a Empty Array"
from your first post:

you are not creating an "empty" array.

with AppGameKit, arrays begin with index 0 so if you followed that line of code above with

it would print "0". ie, you actually have 1 entry (0).

If you declared your array that way & added sprites via .insert per heavens' example above, followed later by


you'd error at the start with "Sprite Does Not Exist" because CollectedCoins[0].ID is 0 (invalid sprite ID)

so, note heavens'

if it was followed by

it would, indeed, be "empty" and return "-1"
Game_Code_here
3
Years of Service
User Offline
Joined: 2nd Jun 2020
Location:
Posted: 24th Oct 2021 04:47 Edited at: 24th Oct 2021 04:48
Ok, so I am trying to set up a way to make coins with a array so I can use a timer to delete each coin.

But when I did it makes a unlimited amount then my app crashes.



Virtual Nomad
Moderator
17
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 24th Oct 2021 06:07 Edited at: 24th Oct 2021 06:13
what is:

supposed to do? otherwise, the code is incomplete/not enough to go on.

but:


Quote: "it makes a unlimited amount "

wrap the above in whatever your max # of sprites is. ie,


otherwise, you're getting a little cryptic/hard to follow again
Game_Code_here
3
Years of Service
User Offline
Joined: 2nd Jun 2020
Location:
Posted: 24th Oct 2021 06:25
Quote: "otherwise, you're getting a little cryptic/hard to follow again "


I don't think so, I believe you just answered my question.

I will play with what you showed me and do what I can from experimenting.

I believe this is teaching me what the power of arrays do but unless you know how to use them it gets very confusing sometimes.

Thank you.
PartTimeCoder
AGK Tool Maker
8
Years of Service
User Offline
Joined: 9th Mar 2015
Location: London UK
Posted: 24th Oct 2021 16:41 Edited at: 24th Oct 2021 16:42
The best approach is to have a bunch of simple test projects (like the ones Nomad posts) and learn something new in each small project, keep it targeted, after a while you have a bunch of projects full of working code you can call upon.

You could be forgiven for getting confused over arrays, as far as AppGameKit goes its about the most complex thing in there, I learned arrays in Lua and oh boy when I first started it blew my mind, more recently with C++ HashLists and Vector arrays ... still not quite there on those yet.

Bear in mind you are not setting the array x,y (is this test code?)



add in there
All_Coins .x=x
All_Coins .y=y

Quote: "But when I did it makes a unlimited amount then my app crashes."


LOL, yeah, does your PC have unlimited memory? (or am I misunderstood, again)

As Nomad pointed out you are declaring the array with 1 item

GLOBAL CollectedCoins as CoinType[0]

and then using insert, if you run a loop on this array and index the ID field of item 1(0) without first checking for a valid ID (GetSpriteExists) then it will error out with a invalid sprite id (always minus 1 remember that, its called zero based indexing)

Could this be what was causing your error?
Open Source plugins
Cl - DnD Plugin
Buy Me A Coffee
Phaelax
DBPro Master
20
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 24th Oct 2021 17:04
To be honest, AppGameKit is a terrible language to learn about arrays the first time.

Actually empty, returns -1:
global CollectedCoins as integer[]

Looks empty, returns a length of 0, but actually contains 1 element at index 0:
global CollectedCoins as integer[0]

The true size of the array is actually length+1, because length is actually returning the last or highest index of the array.
For example, here length returns 4 as defined but you technically have 5 elements because AppGameKit has allocated 0 through 4 inclusively. Any other programming language would have created indices 0 to 3.
global CollectedCoins as integer[4]

So when I use arrays, I treat them as I would in other languages, having a indices from 0 to length-1. Yes that means I will always have 1 unused element but its a perfectly acceptable trade off for my sanity.
Tiled TMX Importer V.2
XML Parser V.2
Base64 Encoder/Decoder
Purple Token - Free online hi-score database
Legend of Zelda

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
PartTimeCoder
AGK Tool Maker
8
Years of Service
User Offline
Joined: 9th Mar 2015
Location: London UK
Posted: 24th Oct 2021 17:21
Yes but that just invites bugs down the line not to mention waisted memory, I know, its negligible but I am pedantic! lol

AGK does buck traditional norms with the array length thing and it caught me off guard at first, I agree its not the best way to learn arrays but if you already know what you are doing then it shouldn't be an issue and can actually be quite helpful, I spend hours bug tracking in PureBasic to find I missed a -1 somewhere in my code, at lease in AppGameKit this issue does not arise.
Open Source plugins
Cl - DnD Plugin
Buy Me A Coffee
Phaelax
DBPro Master
20
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 24th Oct 2021 17:59
Quote: "I know, its negligible but I am pedantic! lol"

I'm typically the same way. Like, arghhh you're wasting 4 bits of ram! BITS! I think that comes from growing up with computers at a time when ram was a luxury.
Tiled TMX Importer V.2
XML Parser V.2
Base64 Encoder/Decoder
Purple Token - Free online hi-score database
Legend of Zelda

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
Game_Code_here
3
Years of Service
User Offline
Joined: 2nd Jun 2020
Location:
Posted: 24th Oct 2021 18:53 Edited at: 24th Oct 2021 18:55
Quote: "Could this be what was causing your error?"


Well no lol, Ok so I set up another type and used this and it works fine. What I was doing is using one already used type for the array in some other unused function I had listed. The XY is set up from loading the sprite advent and the x and y are recorded there. it works good and I'm learning a lot.

Quote: "To be honest, AppGameKit is a terrible language to learn about arrays the first time."


Well In C++ And C I learned all about Arrays but did not pay attention to them and or never really used them.

Quote: "arghhh you're wasting 4 bits of ram! "


Hay we can use 4 bits of ram to hold one animated sprite, it matters.
PartTimeCoder
AGK Tool Maker
8
Years of Service
User Offline
Joined: 9th Mar 2015
Location: London UK
Posted: 24th Oct 2021 18:56
Yup, I remember inserting 2x256MB ram cards that cost a stupid amount of cash in a homebuilt PC feeling like a king, woooa, feel the power lol
Open Source plugins
Cl - DnD Plugin
Buy Me A Coffee

Login to post a reply

Server time is: 2023-12-09 08:36:00
Your offset time is: 2023-12-09 08:36:00