News:

Function Finder  Find all the functions within source code files

Main Menu

PlayBASIC Live: Array Handles (2017-07-08)

Started by kevin, July 10, 2017, 09:20:35 PM

Previous topic - Next topic

kevin

 PlayBASIC Live:  Array Handles (2017-07-08)

    In this video we take a look at the use of handles when dealing with arrays in PlayBASIC.   We start out using integer arrays building a conceptual array in array structure,  which later shows a version using typed arrays..

    One of the many handy uses is of dynamically created arrays / returning array handles, is being able to creating nested structures of arrays.  So you can not only have an array with ing an array, but many levels of that's.. Without having to access it through the parent.  Ie.   Parent(index).Child(index).field   ...  You can just access the Child(index) and it performs the same as accessing the parent in the PlayBASIC virtual machine..   Of course this means there's  some onus on the user to clean up after themselves but it's just the same to cleaning up any media indexes you're using in a type structure for example..  

     http://playbasic.com

   Some commands Used in This Example:  Dim, MakeArray, UnDim, Function , EndFunction, For, Next






   Example #1

PlayBASIC Code: [Select]
      //   This code isn't from the video, rather it's recreated to show
// the same basic ideas


print "RUN THIS PROGRAM IN DEBUG MODE - PRESS F7 TO RUN THIS"


// -------------------------------------------------------------------------
// Create a normal ARRAY
// -------------------------------------------------------------------------
Size = 16

Dim MyIntegerArray(Size)

// Call out fill function to fill it with the value
FillIntArray( MyIntegerArray() , 255 )

ShowArray MyIntegerArray()


// -------------------------------------------------------------------------
// Dynamic Array Example
// -------------------------------------------------------------------------

// declare our array stub to the PlayBASIC,
MakeArray Array()

// Call function to dynamically create a 1D integer array

Handle = MakeIntegerArray(10)
Array() = Handle


// Show the contends of this array
ShowArray(Array())

Sync
waitkey



Function FillIntArray( Me() , FillValue )

for lp =0 to getarrayelements( Me())
Me(lp) = rnd(FillValue)
next

EndFunction


Function MakeIntegerArray( Size)
Dim Me(Size)
FillIntArray( Me() , 123456 )
EndFunction Me()




Function ShowArray(ThisArray())

size=GetArrayElements( ThisArray())
print " Array Size:"+str$(Size)

For lp =0 to Size
s$+= str$( ThisArray(lp))+","
next
print "Array Contents:"+trimright$(s$,",")
print ""
EndFunction






   Example #2 - Integer Arrays within Arrays

PlayBASIC Code: [Select]
      Size = 16
Dim Container(Size)


For lp =1 to Size
Container(lp) = MakeIntegerArray( rndrange(5, 10) )
next


;
MakeArray Array()

For lp =1 to Size
Array()= Container(lp)
if GetArrayStatus(Array())

s$=""
for sizelp=0 to getarrayelements(Array())
s$+= str$( Array(SizeLP) ) + ","
next

print s$

endif
next


// undim cONTAINER ARRAY()
For lp =1 to Size
Array()= Container(lp)
if GetArrayStatus(Array())
unDim Array()
endif
next
#break
UndIM Container()


Sync
waitkey



Function FillIntArray( Me() , FillValue )

for lp =0 to getarrayelements( Me())
Me(lp) = rnd(FillValue)
next

EndFunction


Function MakeIntegerArray( Size)
Dim Me(Size)
FillIntArray( Me() , 123456 )
EndFunction Me()






   Example #3 - Typed Arrays within Arrays

PlayBASIC Code: [Select]
      Type tVector2D
X#,Y#
EndType


Size = 16
Dim MyVectorArrays(Size)


For lp =1 to Size
MyVectorArrays(lp) = MakeVectorArray( rndrange(5, 10) )
next

MakeArray ThisVectorArray().tVector2D

For lp =1 to Size
ThisVectorArray() = MyVectorArrays(lp)
if GetArrayStatus(ThisVectorArray())
s$=""
for sizelp=0 to getarrayelements(ThisVectorArray())
s$+= str$( ThisvectorArray(SizeLP).X ) + str$(ThisvectorArray(SizeLP).Y)+","
next
print s$
endif
next


Sync
waitkey

End



Function MakeVectorArray( Size)

Dim Me(Size) as tVector2D

For lp =0 to Size
Me(lp) = New TVector2D
me(LP).X = 1000 + lp
me(LP).Y = 2000 + lp
next

EndFunction Me().tVector2D







  Video Script




in this video we might look at something a bit

more well; one of those grey areas

that kind of not understood very well and

has to do with types in a rate in

general actually

I'll assume it you know an array is but

I have to explain that stuff you guys

when I go through some of the kind of

quirky things you can do with them so

you might know that you can create

integer arrays of course you know my

Interrail something give it a size it's

a one-dimensional size now kuwait what

next what's actually happening when we

do this is that when the compiler comes

down through the co here it sees the

dimensions data and goes okay we're

creating a new array or we're either

resizing an existing array if this this

keyword here is unique it adds it to our

array it didn't but it checks to see

what kind of type this is as well if you

have no hash after off you know extreme

it becomes as an integer array a hash

would manage a floating point array and

the string is a string array and that's

kind of in keeping with basic basic

vault so it's not that much lead it much

of a change so the question is when at

compile time

all of them pilot knows is that we're

creating an array it doesn't actually

know the size which might be might

surprise you because this this various

value in here can be a literal like this

or some variable you know most variable

called size which somewhere else in the

code you might declare size is 200 that

might be 100 lines above or 500 lines it

doesn't matter where it is get rid of

hash so we actually talked about

I have a sink if we just run this in

deep bag on its might actually not work

that well in yeah I mean I miss that

already lady the bug rabis so the

fashion world would be PV window here in

the Bogle window here

Head Start and you can see the how

variables and man in scope if you click

on the variable you get a look inside it

it's telling us that this is my array

it's currently 200 size it's type as

integer its physical size in bytes is

804 because there's a not too short data

that's and other information tacked on

the front of your information as well

and different types of structures have

more have different size Heather's I

guess the bank here is really really

should be handle which would make more

sense to people I think so it's this

piece of memory here that being

represent representing an El Rey is

stored internally at handle 23 those

handles are how the apply basic runtime

the VM virtual machine keeps track of

all the allocations that your program

makes so you never actually get a

pointer to anything you actually get

handles will query the handles in what

it is and where it is in memory if you

have it access it if we scroll through

we can see the contents of the array at

this point which is empty that's what we

would expect now we can kind of pass

these things around so if I had a

function over here this guy's not going

to be very useful you know it's like get

the ideas out there let's say this is a

fill in to Ray

we give a stub I'll go over this okay so

I'm going to assume that you know what a

function is I want to it explains to you

got our function name and first

parameter here notice we've got I'd call

it me and we're going to close brackets

after it now in PV terms that is

basically an ax raised up at this point

it doesn't know how many dimensions

you're a kind of need but it can glean

that this is going to be an array coming

in it's going to be passed in by Shandor

it's going to be a integer type and

Cortney beyond that there's no idea so

in here we could do things like so I

could run through all here all the

indexes in the array and assign them

some I know some random values of it now

I'll make it tomorrow it'll tell you I

have our fill value one let's call it

that those are the key now I will know

is actually commanded Peavy to do very

task so you would never do this like

this but that's our function so what's

happening is where it can accept an

integer array passed in by handle and

when we pass by handle we're not

actually copying the content of the

array we're just copying for your run

the debugger here hits staff you go to

variables and copying the special handle

number we'll just turned thank you in

the debugger copying that across once

there's a bug in there somewhere

if you look inside beside the in Finnish

sorry the fill in function array

nothing's been actually declared I used

the debugger lets you look at your

entire program all the time by the way

you're not just looking at yes they

actually see what's going on across

things my father's a bit weird but it's

really a kind of state my to be honest

all right let's call the function fill

int array perfect my int array we give

it the double quotes so this means that

we tells TV we we're passing this thing

this entity to rein in this case bites

handle into this function as a parameter

because in there it's got fill value say

children 55 if we run this and debug it

and hit stuff we go to my array now all

our values are filled to 55 but we go to

we have to appear inside my teacher

right here we should be able to see I

didn't

what doesn't refresh that I running

about PB one six five the debugger is

actually from the older version of TV

and they're not quite comparable as yet

to pain but we'll make do so look at

what their state of equal value was when

the this functions exited or the state

of work was and what contents of that

internal rate so I'm talking about this

bit of code here so when we call this

our rate has been passed in and v that

kind of magic number the handles been

passed in just one integer it's

inserted into this local right here and

then we're just operating upon it in

here there's one problem with that

mechanism and that's if you do your try

and read a mention this array yeah and

not pass it back you might get some

strange behavior it's possible for those

for those things to change I can't think

of an example where it would be okay so

I could pass the rain by handle but I

can also do something weird I could do

this I can make it integer array

this is using 1d arrays because I'm too

lazy and what we cut what we caught were

caught just me I guess same thing so

functions have input parameters here and

output parameters go after here you can

have multiple parameters coming out of a

function but most the time you only have

one function coming it if I did deemed

me sighs if I call this function I'm

going to be creating a integer array in

the local scope inside this function and

then I'm going to be returning passing

its handle out to whatever called it

I'll see if it lets me assign that back

to here I just have a variable called

handle a B we make it we'll make it 4096

all size and we'll print the handle to

the screen such an integer but that

integer it's representative of that the

allocation that that array takes inside

of the virtual machine

that's how we can work out if you if you

give it if a handle is good or bad

that looks pretty useful doesn't it

but you could do some pretty weird

things of things with it sky

functionally could make like a stack of

the Ray for example we could use it with

the what's called the the maker the make

array command which is kind of another

unusual command or we going to call us I

just call it array for now yeah make it

ready with dimension dimension

statements these things actually create

a pattern for this array inside the

compiler so the arrangers can't see you

talk about it we know it's name we know

it was what kind of type of data type

that you're a news and how many field

that has coming dimensions at hands and

say this is a one dimensional array will

be used make array we actually we don't

we don't want to give it any of that we

just want to effectively add this name

to look at past local scope and then we

can do this array nichols handle now for

all intensive purposes the rate was

created in here a right here is now this

array so if I if I've dead I better fill

in with something than not I'll garrison

this stuff here let's get rid of that

get rid of thing there I'm just looking

at how we can use make array and access

the contents of it we'll just leave it

all

freaking with it the second so to

mention they're right here or call out

filling filling function or fill it with

I know some belly

one two three four prizes so every cell

inside the array is going to have that

value in it so if I go for loop equals

not to guess all right

element I query that of the array

there's a second parameter therefore

with for what to mention you want

actually quick get for but by default it

gets the first dimension then we go

print array loop you might want to make

a bit smaller

let's make it 16 right so the code is

going to cost function go to mr. rabe

have me integers this is tell you to I

feel that fill it right with every item

in the array with this value and then

return the handle I'm grabbing the

handle here when I'm making a I'm

registering a an array stub is a

compiler using make array then I'm just

assigning that the handle then when you

we were assign we create a skull bike

that it doesn't own the data it looks oh

so if I assigned to another handle to it

you're not destroying anything just

looking at something and then reusing it

for something else there we go

look at the little bugger

so hit stop got a variable my arrays the

redirection array and it's looking at

Bank 23 and when we call make integer

right it should be the handle it came

out no not then cleaned up since the

yeah a few problems with Dobara on this

bill to fix lies I think might want to

do that stuff for what if you wanted an

array of arrays you know if I can donate

dynamically create the right here I

don't have to run through and actually I

don't ever have to run through and you

know do a bunch of thing I don't know

you know a bunch of animations you know

annum one and them two or something and

so on so on you know it's probably

practical to do like that if you've only

got a few of them if you have lots of

them not really no we can make you know

just to demonstrate this a bit further

that we could do this with you know I

can make another right here of 256 and

I'm just a site so I just sign that

straight it's right allow stub they'll

fill the first Iranian and going it here

we are going to lose track of the handle

when we do this when we overwrite the

stub it doesn't undo whatever was there

just actually replace it but the same as

using the integer if I if I copy yeah if

I do something like this where I go you

know i equal the-- whatever was in a

previously is not destroyed it's just

written over I can't actually just get

it back but I yeah

um all right so we can sort of create

arrays inside functions return them do

that kind of stuff I'll leave that bit

there let's say we had you know I'll

call this thing L go container get some

size other size the quarter you know six

day trying to make us nice and obvious

but maybe it's not this thing here this

doesn't make her evil and generate any

execution code every one time like the

way it's just we're just adding us to

the compiler name to the ArrayList in

the compiler at compile time

so otherwise we wouldn't go access thing

it wouldn't know what a ray is so here

let's my table random random range

between either you know 50 and a

thousand values and when we feel that

would do skip or randomized whatever

that fuel value is just so we get some

different data inside the array so this

this thing is going to create an array

and then we're going to store but so we

can keep track of these handles at a

later point you know leave ours I'll

just get rid of effort and then actually

yeah all right so now I'm actually

creating conceptual arrays of an array

don't need to make array there above it

just go about half a second I've got to

see them the debugger has no

understanding of what's going on

it says container it looks at the time I

guess oh this is just a bunch of

integers and look at the numbers in

there they do a PV just integers we

would need like a special array time for

this to be to understand what we're

actually doing what of course what I

could I could do now to go through go

through here actually look at the

content in your eyes so we create the

array last time we were in that and this

function above this time or grab the

handle at this point and write it into

here

now we probably should do something like

yeah get a row status to make sure this

thing actually exists because if we can

pass what anything into this array and

my captain said oh yeah so as a result

of we passed the wrong wrong clean you

and I know we'll run through its eyes

and the array elements all right I'll

just grab a string and stick all the

values in that well do over L that will

do for you so plus equals and convert

the whatever we're reading to a string

and stick on it covertly and then drop

drop this thing here - it's gray have we

gone hung out for the role or that that

lip counter there there we go

and it arrays are too large or make them

smalls like I said I kind of wear C on

spray this on this one valve only view

between five and ten elements long

that's better

so got these we trim them like it like

we've got the inside you know different

sized lists if you wanted to you can

make these all your a functions work on

on this double raised here so once

you've grabbed the handle from where you

stored it you can manipulate them you

know however you want and then if you

want to delete the raise at the end you

can't just kind of just unbe in the

container and it has no idea it just

these container and goes oh that's

integer eight and cleans up that Mary

it won't clean up these arrays will

dynamically create it so you would need

something that that goes through and

does this this task here I'll put this

in a below there so this bit of code

here would what onion container or

capsule song is that yeah get rid of

that okay so then we go again out

alright and then we clean up that Pat

memory and oh do I delete something yeah

probably

so a little bit more work it's involved

in doing this kind of done you know

structure inside that structure kind of

started this is what this and make array

commands for this works with type arrays

less all kind of stuff it's not limited

to just this right in the debugger so if

our application is run some of it the

state a VM case is in a twenty two so we

might put a little break there then

we'll look at this day I put breaks

around those they're actually so it

should things you should see ghost aid

our promazine is holded so we've got 41

allocated handles inside the van if I

hit run again with where in the code are

we with moves past the delete containers

on to the next one safe in a twenty five

factor program and are still running

so with undimmed container and we're

back there might like you bit via just

unknown in there the live here work is

working fine and in itself we're quite

sure if that was working properly

it's hard to tell because actually

handles are allocated doing things in

the background sometimes strings temp

screens Jarrow handles they sit I ran

through the entire loop Rome's entire

execution so you might not see something

but actually that buffer is being used

maybe things like string operations

could generate ten handles for example

all right so they're going with a bit of

a I look at one of those kind of

stranger features you can do and doing

580 will just try wonderful change if we

had a know might take too long actually

our container make array you know I hope

I found a I've got a typed array same

principle by the way I don't we just

have these prefix with time this caught

object

you know character or something like

that

alright my dude and and my dude

character you have a status field have

like an X and the y coordinate like that

I'll just write it using I don't know

okay most games are going to have

probably 20 or 30 fields for what it can

characters you they're doing is used to

say principal so let's change this up a

little bit further let's make a function

so make my my dude array probably not

the best approach estimate I vectors

they should not yeah until you know

vector Tuesday we'll call it

pretty boring socks so these are going

to have two fields to have we'll call it

me yeah me it's an array and it's a type

vector to D which is the so it is

returning a handle of this type here you

need to know that the vector the type

here because the way that Kamala deals

with presence in the future may not

think need that actually I'm going to

mention size as T T vector to D I don't

run through the thing to fill it towards

duck

so I've we allocate this T vector to D

me let caps like a strong enough you

know X we're going to have some I know

thousand twice loop failures in like

that then we'll say the Y is a 2-pound

that this okay let's get rid of now I

get rid of out I'll leave the code there

so I can post it up on the forms later

let's just put it in the bottom so make

sure I put it end at the end of this

thing I'll reuse some of this code yo

krob krob this will make container oh no

my vector arise what's just call it

something different

right so we got to make LJ nice yeah so

I'm free

pretty much rushing it and winging it

all at once not like not a good to the

same thing I have a random number of

elements in here so between whatever

wherever it's five and ten or call sink

and white key just give it a test run

see if we're all right well we are all

right knee right and if I'd know have a

look and make integer there is make

vector all right click on this thing

here oh we didn't like that much at all

with it now and might be some more green

ones in that debugger the more we what

we're thinking - bummer the process is

working I wouldn't worry the code

actually at work it's the debugger it

was looking trying to look at an array

in the existing there's probably no

protection inside it so that's not a

good thing

alright so I have an integer right here

which we're going to store with 16

arrays in this case for whatever reason

just in vector to the array we've made a

vector to the array for the function

it's initialize the array like this

returned it got our handle in here then

we need to access these things out here

you know we need to make an array Oh

Typhon make array Oh Mallory it's pretty

good make array much better

yeah I know sorry because when this

Lecter array same thing it needs to know

that this is a typed array otherwise it

one you want to go access the fields

from it so we need to have it in handle

representation beyond that we can we can

do our thing so I'll just cut and paste

that across my hand keeps clicking the

caps lock key

it's strange out how different you know

this keyboard looks almost exactly the

same as the one I use every day of my

life almost but alright we've got so

rain through I can go back to array a

contain out grabbing the handle putting

it in our make array let's check to see

if a thing exists get a rate status if

it exists what we'll do

I want just perceptive code from the

bottom that similar thing okay this

vector alright so I go this vector array

the loop dot X and

and yeah it's going to y-coordinate from

it and the last character to it oh my

gosh let's try it out yeah so it's

working so good

our first array contains one thousand

and two thousand two the first

coordinate that's initializing the array

events 1 2001 2001 so on any to raise a

different length so the same clean my

dear work for works with arrays this

means you actually have structures that

are nested about ever having to have

dynamic arrays inside types those clean

as that you know granted that that's a

cleaner and easier concept for people to

grasp because you can make an array in

time you want Parsa renders handle you

can pretty much to everyone and it's up

to you to make sure you don't don't lose

those handles they don't break anywhere

that's probably the main thing which I

so trouble try running now go go to

variables each stuff will go to my

vector no felony and we conceal our

vector array in the panel because it's

the name of the array and the type the

data structure we use the type usually

feel different because the purpose

really its 28 28 bytes long it tends to

be well actually the little targets they

picked up and our bank handle is 158

which is the handle of this this actual

array and each type inside the array has

its own handle okay if it has strings

they have their own handle so everything

everything is these handles

so we can keep track of what a resource

is if we just had pointers and it's

actually hard to keep track of those

things whether it's a legal point or not

it's not impossible but handles is just

easier all right they will have to do

with that really sort of trail sort of a

train of thought demonstration I'll put

the code up on the boards and you

picture it and see if you make any sense

out of it and see if you quiet your own

applications thanks for watching and

I'll catch some on time bye