SyntaxBomb - Indie Coders

Languages & Coding => AppGameKit ( AGK ) => Topic started by: Amon on February 28, 2018, 21:11:29

Title: So, no linked lists in AGK. How do i?
Post by: Amon on February 28, 2018, 21:11:29
manage my entities without using lists? A simple example from someone would be appreciated. ta.


:)
Title: Re: So, no linked lists in AGK. How do i?
Post by: therevills on February 28, 2018, 21:23:09
I'm not sure... but maybe Arrays... urrggg. I'm sure an AGK expert will be along soon :)
Title: Re: So, no linked lists in AGK. How do i?
Post by: Amon on February 28, 2018, 21:39:16
ARRAYSSSSSS.....NOOOOOOOO!!!!!!!
Title: Re: So, no linked lists in AGK. How do i?
Post by: Naughty Alien on March 01, 2018, 00:21:08
..Arrays in AGK are really handy and nicely done..Insert, Remove, etc..all is already there so it saves you from endless iterations..very handy..

https://www.appgamekit.com/documentation/guides/12_array_changes.htm
Title: Re: So, no linked lists in AGK. How do i?
Post by: Qube on March 01, 2018, 00:39:20
Here's a quick working example :


Type tPerson
number As Integer
xPos As Float
yPos As Float
EndType

Global person As tPerson
Global people As tPerson[]

For count = 1 To 10
   person.number = count
   person.xPos = Random2( 0, 100 )
   person.yPos = Random2( 0, 100 )
   people.insert( person )
Next

For thisPerson = people.length To 0 Step -1
   Print people[ thisPerson ].number
   Print people[ thisPerson ].xPos
   Print people[ thisPerson ].yPos
Next

For thisPerson = people.length To 0 Step -1
   If people[ thisPerson ].xPos > 50
      people.remove( thisPerson )
   EndIf
Next
Title: Re: So, no linked lists in AGK. How do i?
Post by: Amon on March 01, 2018, 01:19:35
That's exactly what I was looking for Qube. Thanks m8.

:)
Title: Re: So, no linked lists in AGK. How do i?
Post by: Derron on March 01, 2018, 07:01:32
@ qube
So arrays are 1-based in AGK? And for loops stop before reaching the to-value?

I think you should start at arr.length-1 ... Or stop at 1 but subtract 1 when accessing the index ;-)

Bye
Ron
Title: Re: So, no linked lists in AGK. How do i?
Post by: Qube on March 01, 2018, 07:44:01
The example I gave always returns the right results so I've just assumed that's the right method. If it was wrong then I would of thought it would of thrown an out of bounds error. I do find that iterating backwards works best if your loop contains removal of elements.
Title: Re: So, no linked lists in AGK. How do i?
Post by: Derron on March 01, 2018, 07:55:35
If that works that I think this is a flaw in the language...


For count = 1 To 10
  'you create 1,2,3,4,5,6,7,8,9,10 -- 10 entries
  'agk does not fill arrays "index based" but similar to maps so "insert(15)"
  'does not increase array size to be of length 15
  '-> array has a length of 10 now
Next

For thisPerson = people.length To 0 Step -1
  'you go from 10 to 9, 8, 7, 6, 5, 4, 3, 2, 1 - and maybe also "0" (in BlitzMax you would at least)
  'you then access the array - people[10 to 0]
  '-> you access 10 - or 11 array entries if "to" means "inclusive to"
Next


Hope my comments explain what is wrong with your code - at least in my opinion. The first loop should be OK, the second for loop is accessing one entry too much. Imho it should segfault when trying to access the "0" person's property (as the person does not exist).

bye
Ron
Title: Re: So, no linked lists in AGK. How do i?
Post by: MikeHart on March 01, 2018, 08:23:02
Arrays in AGK are ZERO based.

But...

when you define an array with a given size like myArray[10]

The 10 is the highest index, so you get 11 entries.

But #2...

array.Length() reports back the highest index, so your actual size of an array is length + 1

Many AGK developers people ignore index 0 and start with 1 so that .Length then makes more sense to them.
Title: Re: So, no linked lists in AGK. How do i?
Post by: Derron on March 01, 2018, 08:43:34
> array.Length() reports back the highest index, so your actual size of an array is length + 1

Then this is literally wrong. "length" is commonly used to return the amount of entries (if like a linked list) or the size of an array (if used as a "pure array").


Qube did:
- insert 10 elements to the array. The arrays highest key (not index...imho) is "10" then. right?
- I could access them via people[1] to people[10]. right?
- starts another loop, iterating from key "10" down to key "0" (so 10,9,8...2,1,0(!)). right?
- - accesses people[10]. right?
- - accesses people[0]. right?

but people[0] was never defined.

It does not depend on whether the arrays are "zero/one"-based or if you have a linked list. Qube adds 10 items, but iterates over 11 - at least I understand it that way.
If "to" was inclusive (1 to 5 = 1,2,3,4,5) and not exclusive (1 to 5 = 1,2,3,4) then already the insert of the persons would only insert 9 persons (1,2,3,...8,9) and the iteration would try to access 10 persons (10,9...3,2,1).


bye
Ron
Title: Re: So, no linked lists in AGK. How do i?
Post by: TomToad on March 01, 2018, 09:14:45
Qube's code inserts 10 elements.  The highest element is index 9.  people.length returns 9.  Index goes from 0 to 9.  An empty array has a length of -1. Don't know why he iterates backwards, doing for thisPerson = 0 to people.length works just as well (but in opposite order).

Array indexing in AGK is one of the few things that don't make sense to me.  Maybe it is to remain consistent with DBPro so people coming from there can adapt easier?
Title: Re: So, no linked lists in AGK. How do i?
Post by: Derron on March 01, 2018, 09:52:21
Just to get that right:

Code (AppGameKit) Select

For count = 1 To 10
   person.number = count
   person.xPos = Random2( 0, 100 )
   person.yPos = Random2( 0, 100 )
   people.insert( person )
Next


contains then:
people[0].number = 1
people[1].number = 2
...
people[9].number =10

people.length is "9" as "9" was the highest element used during insert. If one did people.Insert(person, count+5) it would have been "14" - while there would have still been 10 entries in it?


Code (AppGameKit) Select

For thisPerson = people.length To 0 Step -1
   Print people[ thisPerson ].number
   Print people[ thisPerson ].xPos
   Print people[ thisPerson ].yPos
Next


So that does:
print people[9].number
print people[8].number
...
print people[0].number


In that case this will work. But it will fail once you use an offset in the insert() command. Seems you need to use "people.find(thisPerson)" **1 in that case. Seems to differ a bit from what I am used to. Surely something error prone :-)


**1
Code (AGK) Select

myArray as integer[5] = [3,4,1,5,2,6]
myArray.sort() // array is now [1,2,3,4,5,6]
index = myArray.find(4) // will return "3" as array indexes start at 0

Source: https://www.appgamekit.com/documentation/guides/12_array_changes.htm


@ TomToad
Thanks. Think that explains what happened.

bye
Ron
Title: Re: So, no linked lists in AGK. How do i?
Post by: Amon on March 01, 2018, 16:24:20

Global DeviceWidth as integer
Global DeviceHeight as integer



SetWindowSize( GetDeviceWidth(), GetDeviceHeight(), 0 )
SetWindowAllowResize( 0 ) // allow the user to resize the window


// set display properties
SetVirtualResolution( 540, 960 ) // doesn't have to match the window
SetOrientationAllowed( 1, 0, 0, 0 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts

Global GreyCube
Global RedCube
Global OrangeCube
Global YellowCube
Global GreenCube
Global BlueCube
Global PurpleCube
Global PinkCube


RedCube = LoadImage("Red.png")
OrangeCube = LoadImage("Orange.png")
YellowCube = LoadImage("Yellow.png")
GreenCube = LoadImage("Green.png")
BlueCube = LoadImage("Blue.png")
PurpleCube = LoadImage("Purple.png")
PinkCube = LoadImage("Pink.png")
GreyCube = LoadImage("Grey.png")

Global GameArray as integer [8,12]

file = OpenToRead("level1.txt")
level$ =  ReadLine(file)
CloseFile(file)

local X = 0
local y = 0

For iter=1 to Len(level$)
if x = 8
x = 0
y = y + 1
endif
GameArray[x,y] = val((Mid(level$, iter, 1)))
x = x + 1
next

for x = 1 to 8
for y = 1 to 12
if GameArray[x,y] = 1
gCube as TGreyCube
CreateSprite(GreyCube)
gCube.x = x * 64
gCube.y = y * 64
GCubeStore.insert(gCube)
endif
next
next

for tgc = 0 to GCubeStore.length-1
SetSpritePosition(GreyCube, gCube.x, gCube.y)
next

do


    Print( ScreenFPS() )


    Sync()
loop

Type TGreyCube
x as float
y as float
EndType
Global GCubeStore as TGreyCube[]


I'm trying to read numbers from a level file and place the cubes in the array. Then display them in their positions. It only shows 2 cubes though.
Title: Re: So, no linked lists in AGK. How do i?
Post by: Qube on March 01, 2018, 19:24:27
Quote from: TomToad on March 01, 2018, 09:14:45Don't know why he iterates backwards, doing for thisPerson = 0 to people.length works just as well (but in opposite order).
Because if you are iterating forward and remove an element then it's crash out with an out of bounds error. That's why I iterate backwards when removing :)

Quote from: DerronIn that case this will work. But it will fail once you use an offset in the insert() command. Seems you need to use "people.find(thisPerson)" **1 in that case. Seems to differ a bit from what I am used to. Surely something error prone :-)
iterate backwards if you plan on removing element and iterate forward if you are planning on inserting elements.

*EDIT* Just to clarify that I would only do that if I needed to go through all the elements and take action.

Quote from: MikeHartMany AGK developers people ignore index 0 and start with 1 so that .Length then makes more sense to them.
I don't :P - In my example it assigns 10 people and person 1 is array element 0. I can understand why they would start from element 1 but I always prefer to start at 0 with arrays.
Title: Re: So, no linked lists in AGK. How do i?
Post by: MikeHart on March 01, 2018, 22:13:37
Quote from: Qube on March 01, 2018, 19:24:27
I don't :P - In my example it assigns 10 people and person 1 is array element 0. I can understand why they would start from element 1 but I always prefer to start at 0 with arrays.


Same here.
Title: Re: So, no linked lists in AGK. How do i?
Post by: Derron on March 01, 2018, 23:31:26
> iterate backwards if you plan on removing element and iterate forward if you are planning on inserting elements.

This wasn't what I was talking about - I was talking about the "offsets" you create if you manually assign them different keys (or "index numbers") during myarr.insert(obj, key). In that case the iterating would no longer work - at least this is how I interpreted the documentation. Did not have it installed as ... ok, it costs money? ... wanted to write I haven't had the motivation to try it out but ... it costs money so it is nothing I would choose now (while I understand that people want to earn things for a living).

bye
Ron
Title: Re: So, no linked lists in AGK. How do i?
Post by: Naughty Alien on March 01, 2018, 23:37:26
..its amazing how some things, relatively simple, easy turn in to quantum mechanics discussions here...you gotta love this forum.. :)
Title: Re: So, no linked lists in AGK. How do i?
Post by: Qube on March 02, 2018, 00:09:16
Quote..its amazing how some things, relatively simple, easy turn in to quantum mechanics discussions here...you gotta love this forum..
I'm sure we can squeeze at least 10 pages about arrays our of this :P

QuoteThis wasn't what I was talking about - I was talking about the "offsets" you create if you manually assign them different keys (or "index numbers") during myarr.insert(obj, key). In that case the iterating would no longer work - at least this is how I interpreted the documentation.
It's myarr.insert( index, value ) :P and if you need to go through them all and insert anywhere / change anything then just do it in something like a While / Wend loop starting from 0. It's not hard to interate and add at the same time.

For example if your going through all the elements and need to add new ones at the end then simply


howMany = myArray.length

For count = 0 To HowMany
   If myArray[ count ] = 1 Then myArray.insert( 2 )
Next


But if you wanted to iterate and insert at your current location then simply


howMany = myArray.length
howManyAdded = 0
atIndex = 0

While atIndex < howMany + howManyAdded
   If myArray[ atIndex ] = 1
      myArray[ atIndex ].insert ( atIndex, 2 )
      Inc howManyAdded, 1
      Inc atIndex, 2 // skips the newly inserted item and goes to the next one
   Else
      Inc atIndex, 1
   EndIf
Wend


Easy peasy, lemon squeezy ;D
Title: Re: So, no linked lists in AGK. How do i?
Post by: Steve Elliott on March 02, 2018, 10:30:52
Quote
..its amazing how some things, relatively simple, easy turn in to quantum mechanics discussions here...you gotta love this forum.. :)

My thoughts exactly.  Some people have very strong opinions here.  Maybe instead go work on a project...Oh look a SyntaxBomb Game Competition!  ;D
Title: Re: So, no linked lists in AGK. How do i?
Post by: Derron on March 02, 2018, 10:54:44
I just wanted to avoid a potential error/flaw to get proposed as a solution to someone who asked a question.

Dunno why this is marked as "strong opinion" or "quantum [..] discussions". I think the thread evolved into something answering the whole thing in more detail then expected - imho a good thing.

bye
Ron
Title: Re: So, no linked lists in AGK. How do i?
Post by: Amon on March 03, 2018, 22:13:47
+1 on Derron's last post.
Title: Re: So, no linked lists in AGK. How do i?
Post by: Rick Nasher on March 03, 2018, 23:38:22
Quote from: Derron on March 01, 2018, 23:31:26
.. Did not have it installed as ... ok, it costs money? ... wanted to write I haven't had the motivation to try it out but ... it costs money so it is nothing I would choose now (while I understand that people want to earn things for a living).

*Free limited trial: https://www.appgamekit.com/trial
Title: Re: So, no linked lists in AGK. How do i?
Post by: Derron on March 04, 2018, 07:23:57
Thanks Rick.

Is the IDE on Windows also based on Geany? (on Linux it starts a rebranded "based on Geany 1.24" variant of geany). As Geany is GPL some sources need to be provided too - at least they do for some stuff. Found the part for functions definitions (TagManager) but still looking for the Debugger-stuff. For my geany I made a BlitzMax-Module only being activated when focusing a .bmx file (it then adds compilation options + buttons to the menu) but it still misses the tooltips for functions (params), some kind of autocomplete and the debugger stuff.
At least they (AGK creators) chooses a fast and nice scintilla-editor as base.


bye
Ron 
Title: Re: So, no linked lists in AGK. How do i?
Post by: Rick Nasher on March 04, 2018, 08:46:44
[post moved]
Title: Re: So, no linked lists in AGK. How do i?
Post by: Derron on March 04, 2018, 08:59:30
As the other things are System-based widgets. Maybe you could edit your Editor/share/geany.gtkrc.windows and append some stuff to manipulate widget appearance but I never tried, so dunno if this would be successful or not.

Edit: having duckduckgo'd for "geany.gtkrc.windows dark theme" I found this:

geany with a dark theme (and German localization - which the AGK dudes removed somehow as I got only partial texts translated, even the most ones translated on my Geany are English on theirs)
(https://www.syntaxbomb.com/proxy.php?request=http%3A%2F%2Fwww.henrykoch.de%2Fwp-content%2Fuploads%2F2017%2F01%2FBildschirmfoto-vom-2017-01-07-144638.png&hash=5be36951ccf606223b082e66f037d32913aff728)
http://www.henrykoch.de/en/geany-editor-in-ubuntu-configure-a-dark-colored-theme



bye
Ron
Title: Re: So, no linked lists in AGK. How do i?
Post by: Derron on March 04, 2018, 09:06:24
You could then just create a _custom_ ressource file - so you keep the bright one for day, and the dark for the night.

Create a launch_editor_dark.bat and put something in like
set GTK2_RC_FILES=PATH\TO\YOUR\gtkrc
Geany64.exe

This will set the GTK2_RC_FILES environment variable for just this call (so only this execution).

PS: Maybe Qube could split our posts into a new thread - from your "quoting me" phost about the trial up to this - to avoid derailing that thread.

bye
Ron
Title: Re: So, no linked lists in AGK. How do i?
Post by: Rick Nasher on March 04, 2018, 10:48:04
[Post moved]
Title: Re: So, no linked lists in AGK. How do i?
Post by: Derron on March 04, 2018, 11:18:55
(https://www.syntaxbomb.com/proxy.php?request=http%3A%2F%2Fi.imgur.com%2FEdozX2S.png&hash=0576c75b1b9e5bdd8d75c60e2cd49b663c399938)

To do this: Follow the instructions in
C:\Program Files\The Game Creators\AGK2Trial\Tier 1\Editor\Changing IDE Theme.txt

The above screenshot is using
include "../share/themes/Nodoka-Midnight/gtk-2.0/gtkrc"

(line 41 of the geany.gtkrc file)

You can even download new ones from www.gnome-looks.org and other websites - should be for GTK 2.x then.


bye
Ron
Title: Re: So, no linked lists in AGK. How do i?
Post by: Rick Nasher on March 04, 2018, 12:13:09
@ Derron
As Qube apparently is busy / didn't notice our request to clean up this thread, I've copied our posts below, replaced my posts here by [Post moved] and created a new thread for it here:

https://www.syntaxbomb.com/index.php/topic,4232.new.html#new (https://www.syntaxbomb.com/index.php/topic,4232.new.html#new)

Perhaps you can also take away your posts from below to tidy up this thread a bit?
Title: Re: So, no linked lists in AGK. How do i?
Post by: playniax on March 07, 2018, 07:58:01
playing with AGK Tier 1 right now and I can't seem to find a way to compare elements in an array?!

Example that doesn't work but shows what I am trying to do:

tiles As TTile[8]

tile As TTile
tiles[4].insert(tile)

for i = 0 to tiles.Length
if tiles[i]=tile Then do something...
next i


Who is bright enough?
Title: Re: So, no linked lists in AGK. How do i?
Post by: therevills on March 07, 2018, 08:13:01
My guess its because you are not comparing objects you will need to compare attributes:

if tiles[i].x=tile.x And tiles[i].x=tile.y And tiles[i].id=tile.id  Then do something...
next i


But I havent coded in AGK...
Title: Re: So, no linked lists in AGK. How do i?
Post by: MikeHart on March 07, 2018, 08:16:24
Exactly. In AGK you need to get rid of any OBJECT thinking. Give each tile an index. Compare this.
Title: Re: So, no linked lists in AGK. How do i?
Post by: playniax on March 07, 2018, 09:18:24
I understand what you are saying and I got creative by giving the tile a unique id when it is in the tilemap ( I need to have unique identifiers in my case ) but I am very keen on performance and this just adds more complexity.

I still want to know if it was possible because at the end of the day it is still an object.

So far enjoying AGK and will do some stress testing for sure.
Title: Re: So, no linked lists in AGK. How do i?
Post by: Derron on March 07, 2018, 09:35:33
@ therevills
if the .id was unique, I would avoid doing .x and .y property testing - should shape of some cpu cycles :-)

@ playniax
As there are no indices in AGK arrays - but "keys", you cannot do a "0 to x" check. It is more kind of a BlitzMax TMap - so "ValueForKey()" rather than TList's "ValueAtIndex()".
Dunno how AGK handles the whole thing, but unique IDs sound like a big hashmap storing all the stuff. But this would increase lookup time - so maybe they do it for each "type"?

Everything I read here about AGK makes me think it is more compareable to the way Blitz3D and also BlitzMax are exposing convenience functions (namespace polluting "CreatePixmap()"  compared to "new TPixmap.Create()"). You have a long list of commands without any hierarchy.
But I am pretty sure somebody thought about all this before coding, so there must be a benefit in using that "id" stuff compared to object handles.


bye
Ron
Title: Re: So, no linked lists in AGK. How do i?
Post by: therevills on March 07, 2018, 21:19:50
Quote from: Derron on March 07, 2018, 09:35:33
@ therevills
if the .id was unique, I would avoid doing .x and .y property testing - should shape of some cpu cycles :-)

Yeah I know, I was just "guessing" on the solution and showing an example :)