Posted: 23rd Oct 2021 18:58
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.

+ Code Snippet
type CoinType
    ID 		as integer
    x 		as float
    y 		as float
    speed	as float
    angle 	as float
    Delete   as integer
    Timers   as integer
endtype
 
 
global CollectedCoins as CoinType[0]	// create list which holds all coins



SO I try something like this

+ Code Snippet
CollectedCoins[ThisCoin].Timers=	CollectedCoins[ThisCoin].Timers+1


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.
Posted: 23rd Oct 2021 21:11
Think I understand what you are asking, maybe this will help

+ Code Snippet
type CoinType
    ID      as integer
    x       as float
    y       as float
    speed   as float
    angle   as float
    Delete   as integer
    Timers   as integer
endtype

global CollectedCoins as CoinType[] 

Function AddCoin(X, Y, Speed, Angle#, Delete, Timers)
	TempCoinType As CoinType //Create a temp type of the type we want to fill
	
	TempCoinType.ID = CreateSprite(0)
	TempCoinType.X = X
	TempCoinType.Y = Y
	TempCoinType.Speed = Speed
	TempCoinType.Angle = Angle#
	TempCoinType.Delete = Delete
	TempCoinType.TImers = Timers
	
	SetSpritePosition(TempCoinType.ID, TempCoinType.X, TempCoinType.Y)
	
	CollectedCoins.Insert(TempCoinType) //Insert our temp type in to the main type
EndFunction

AddCoin(8, 50, 10, 1.57, 1, 0)
AddCoin(50, 50, 15, 9.12, 0, 0)
AddCoin(50, 1, 20, 6.38, 1, 1)

Do
	Print(CollectedCoins.Length) //How many items does my array hold
	For i = 0 to CollectedCoins.Length //Loop over the length of the array
		Print(GetSpriteX(CollectedCoins[i].ID)) //Print the X of each sprite held in the array
	Next i
	Sync()
Loop
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.
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
Posted: 23rd Oct 2021 22:51
You just need to loop over the array

+ Code Snippet
For i = 0 to CollectedCoins.Length //Loop over the length of the array
	SetSpritePosition(CollectedCoins[i].ID, Random(0, 100), Random(0, 100))) //Set each sprite position
Next i


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

+ Code Snippet
SetSpritePosition(CollectedCoins[0].ID, 45, 67)) //Position sprite 0
SetSpritePosition(CollectedCoins[1].ID, 89, 12)) //Position sprite 1
SetSpritePosition(CollectedCoins[2].ID, 18, 67)) //Position sprite 2
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?

+ Code Snippet
SetSpritePosition(CollectedCoins[0].ID, 45, 67)) //Position sprite 0
SetSpritePosition(CollectedCoins[1].ID, 89, 12)) //Position sprite 1
SetSpritePosition(CollectedCoins[2].ID, 18, 67)) //Position sprite 2
Posted: 23rd Oct 2021 23:30
This code from my first post fills the array with coins

+ Code Snippet
 Function AddCoin(X, Y, Speed, Angle#, Delete, Timers)
    TempCoinType As CoinType //Create a temp type of the type we want to fill
     
    TempCoinType.ID = CreateSprite(0)
    TempCoinType.X = X
    TempCoinType.Y = Y
    TempCoinType.Speed = Speed
    TempCoinType.Angle = Angle#
    TempCoinType.Delete = Delete
    TempCoinType.TImers = Timers
     
    SetSpritePosition(TempCoinType.ID, TempCoinType.X, TempCoinType.Y)
     
    CollectedCoins.Insert(TempCoinType) //Insert our temp type in to the main type
EndFunction
 
AddCoin(8, 50, 10, 1.57, 1, 0)
AddCoin(50, 50, 15, 9.12, 0, 0)
AddCoin(50, 1, 20, 6.38, 1, 1) 


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
Posted: 24th Oct 2021 0: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.
Posted: 24th Oct 2021 2: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

"


Yes, but sometimes you don't want/need a loop, in this case you need to check the index you are passing in
+ Code Snippet
if index>-1 and index<= myArray.length


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

+ Code Snippet
index = myArray.find(spriteID)


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

+ Code Snippet
index = myArray.find(spriteID)
if index <>-1
    x = myArray[index].x
    y = myArray[index].y
    // etc 
    // etc
endif


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

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
Posted: 24th Oct 2021 2:23
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:
+ Code Snippet
global CollectedCoins as CoinType[0]    // create list which holds all coins

you are not creating an "empty" array.

with AppGameKit, arrays begin with index 0 so if you followed that line of code above with
+ Code Snippet
Print(CollectedCoins.Length)

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
+ Code Snippet
For i = 0 to CollectedCoins.Length //Loop over the length of the array
    SetSpritePosition(CollectedCoins[i].ID, Random(0, 100), Random(0, 100))) //Set each sprite position
Next i


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

so, note heavens'
+ Code Snippet
global CollectedCoins as CoinType[] 

if it was followed by
+ Code Snippet
print (CollectedCoins.Length)

it would, indeed, be "empty" and return "-1"
Posted: 24th Oct 2021 4:47
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.

+ Code Snippet
type CoinType
    ID 		as integer
    x 		as float
    y 		as float
    speed	as float
    angle 	as float
    Delete   as integer
    Timers   as integer
endtype
 
 
GLOBAL CollectedCoins as CoinType[0]	// create list which holds all coins


+ Code Snippet
Function SpawnCoin(Spr)
	
	All_Coins as CoinType
	
    All_Coins.ID  = CreateSprite(coins_1)
    x = GetSpriteXByOffset(Spr) :   y = GetSpriteY(Spr)
    SetSpritePositionByOffset(All_Coins.ID, x,y-5)
    SetSpriteGroup(All_Coins.ID,-10)
    SetSpritePhysicsOn(All_Coins.ID ,2)
    SetSpriteShapeCircle(All_Coins.ID,0,0,8)
    SetSpritePhysicsVelocity(All_Coins.ID ,0,-10.0)
    SetSpriteSize(All_Coins.ID,5,5)
    SetSpriteAnimation(All_Coins.ID ,128,128,6)
    PlaySprite(All_Coins.ID,10, 1, 1, 6)
    All_Coins.Timers =1
    CollectedCoins .InsertSorted(All_Coins )
EndFunction
Posted: 24th Oct 2021 6:07
what is:
+ Code Snippet
All_Coins.Timers =1

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

but:
+ Code Snippet
SetErrorMode(2)
SetWindowSize( 1280,720, 0 )
SetWindowAllowResize( 1 )

// set display properties
SetVirtualResolution( 1280,720)
SetOrientationAllowed( 1, 1, 1, 1 )
SetSyncRate( 30, 0 ) 
SetScissor( 0,0,0,0 )
UseNewDefaultFonts( 1 ) 

red = MakeColor(255,0,0)
DrawBox(0,0,32,32,red,red,red,red,1)
GLOBAL BoxImg	:	BoxImg = GetImage(0,0,32,32)

Type Data
	ID, StartTime#, Duration#
EndType

GLOBAL Sprites as Data []

Toggle = 1 `turn the sprite generator on/off

do

    If GetRawKeyState(27) then Exit    

	If GetPointerPressed() then Toggle = -Toggle
		
	If Last# + 0.2 <= Timer() and Toggle = 1
		MakeSprite()
		Last# = Timer()
	Endif

	DoSprites()
	
	Print(Toggle)
	Print(Sprites.Length)

    Sync()
loop

Function MakeSprite()
	This as Data
		This.ID = CreateSprite(BoxImg)
		This.StartTime# = Timer()
		This.Duration# = Random(5,10) `seconds
		SetSpritePositionByOffset(This.ID,Random(100,1100), Random(100,600))
		SetSpriteAngle(This.ID, Random(0,359))
		Sprites.Insert(This)
EndFunction

Function DoSprites()
	//If you're .Removing, count in reverse, else it will skip indices
	For x = Sprites.Length to 0 Step -1
		If Sprites[x].StartTime# + Sprites[x].Duration# <= Timer()
			DeleteSprite(Sprites[x].ID)
			Sprites.Remove(x)
		EndIf
	Next x
EndFunction


it makes a unlimited amount

wrap the above in whatever your max # of sprites is. ie,
+ Code Snippet
If Last# + 0.2 <= Timer() and Sprites.Length < Max
...
EndIf


otherwise, you're getting a little cryptic/hard to follow again
Posted: 24th Oct 2021 6:25
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.
Posted: 24th Oct 2021 16:41
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?)

+ Code Snippet
Function SpawnCoin(Spr)
     
    All_Coins as CoinType
     
    All_Coins.ID  = CreateSprite(coins_1)
    x = GetSpriteXByOffset(Spr) :   y = GetSpriteY(Spr)
    SetSpritePositionByOffset(All_Coins.ID, x,y-5)
    SetSpriteGroup(All_Coins.ID,-10)
    SetSpritePhysicsOn(All_Coins.ID ,2)
    SetSpriteShapeCircle(All_Coins.ID,0,0,8)
    SetSpritePhysicsVelocity(All_Coins.ID ,0,-10.0)
    SetSpriteSize(All_Coins.ID,5,5)
    SetSpriteAnimation(All_Coins.ID ,128,128,6)
    PlaySprite(All_Coins.ID,10, 1, 1, 6)
    All_Coins.Timers =1
    CollectedCoins .InsertSorted(All_Coins )
EndFunction


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

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?
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.
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.
Posted: 24th Oct 2021 17:59
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.
Posted: 24th Oct 2021 18:53
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.

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.

arghhh you're wasting 4 bits of ram!


Hay we can use 4 bits of ram to hold one animated sprite, it matters.
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