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 / [SOLVED] What is the syntax for inserting values into a multi dimentional array

Author
Message
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 22nd Jan 2019 03:08
Array is defined as
MyArray as int[-1, -1]

I have tried
MyArray.insert(value)
MyArray[].insert(value)
MyArray[-1].insert(value)

The author of this post has marked a post as an answer.

Go to answer

blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 22nd Jan 2019 04:36
This post has been marked by the post author as the answer.
For future reference you insert a row at a time (As far as i can see) .So you need to do something like this;

My2DArray as integer[-1, -1]
MyRow as integer[3] = [ 1, 2, 3, 4 ]

My2DArray.insert(MyRow) // Inserts MyRow[] at My2DArray[0] so My2DArray[0,0] = 1, My2DArray[0,1] = 2 etc

Golelorn
7
Years of Service
User Offline
Joined: 20th Nov 2016
Location:
Posted: 23rd Jan 2019 23:11 Edited at: 23rd Jan 2019 23:12
Nevermind. I see.

Very cool. Thanks.
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 24th Jan 2019 00:40
I wasn't sure how to do that myself but instead I have always chosen the json method
where box might be a type of _box and boxes as array of _box

doing it with my method above seems a little excessive when the type only has one element
ie _box has id but nothing else. but your method is much neater when this is the case
fubar
Golelorn
7
Years of Service
User Offline
Joined: 20th Nov 2016
Location:
Posted: 24th Jan 2019 01:03
I did the same, since I had no idea how to insert into a multidimensional array.

Its probably not even worth worrying about any difference in memory usage, is it?
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 24th Jan 2019 01:10
As an aside: How would you guys setup a tree structure?
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 24th Jan 2019 01:46
linked lists and the like are ideal for this because they point back to the previous node etc
here is an article which may help
https://webdocs.cs.ualberta.ca/~holte/T26/tree-as-array.html
fubar
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 24th Jan 2019 01:49 Edited at: 24th Jan 2019 05:38
but not sure we can add new dimensions to an array on the fly which ideally is what you want to do
its really a job for pointer arithmetic so not sure how in tier1


ideally something like

type _nodetype
id as integer
prevNode as integer
nextNode as integer
endtype

type _nodes
node as _nodetype[2]
endtype

nodes as _nodes[]

but you need to be able to add nodes to each nodetype which logically is a mess

but it may be able to be achieved with memblocks if you setup what is needed for each node with its pairing two nodes
with space left to store the values in memory you want ie 3 integers. Then whenever you wanted to add nodes to the
tree you would have to create a new memblock calculating its new size and then add the values. A bit like how linked lists
work but in c its done by the use of pointers. The beauty is if you know the id its very fast to search through the tree to
find other data

Why do you need tree data?
fubar
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 24th Jan 2019 06:41 Edited at: 24th Jan 2019 06:49
thinking about it again as a 1 dimensional array

nodes as integer[]

rem 1 node
row as integer[0]
nodes.insert((row)

rem next row 2 nodes
row as integer[1]
nodes.insert(row)

rem next node 4 nodes
row as integer[3]
nodes.insert(row)

8 nodes etc etc

and a bit of math and you could find the right node
you would just need to know what row number and which offset
I think the same could be done similarly for arrays of types
fubar
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 24th Jan 2019 06:57
That's very interesting
Maybe if node was something like

type child
offset as integer
count as integer
endtype

type node
value as integer // Or whatever you want to store
child as child
parent as integer
endtype

So child.offset would point to the element in the row where the child elements start and child.count would be the number of children
parent would point to the element that is the parent of this node

So you could move down the tree and up the tree
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 24th Jan 2019 07:04 Edited at: 24th Jan 2019 07:05
yep just the number of children you add would go up exponentially for each row as I cant see how else you
could add them its quite achievable that way with AGK. In c its extremely fast as prev and next are pointers
to nodes, so traversal is extremely fast. but I think in AppGameKit it need to be a fixed number of children for each
row. That way it just might work

My tree drawing algorthym is similar to how filling the nodes would be achieved (on my useful snippets page)
fubar

Login to post a reply

Server time is: 2024-11-05 18:56:49
Your offset time is: 2024-11-05 18:56:49