hello and welcome to another episode in the GT script fundamental tutorial
    你好,欢迎来到本集 GT 脚本基础教程

    series in this episode we’ll be taking a look at arrays in memory address arrays
    系列的另一集,我们将看看内存地址中的数组数组

    are a collection of items stored at contiguous memory locations we know how
    是存储在连续内存位置的项目的集合,我们知道

    to manipulate and use arrays but let’s go ahead and take a look at how arrays
    如何操作和使用数组,但是让我们开始吧 向前看一下数组

    behave in memory so in Godot arrays are objects that are stored in memory the
    在内存中的行为,因此在 Godot 中,数组是存储在内存中的对象,要记住的

    key thing to keep in mind is that arrays are just objects now this is a basic
    关键是数组现在只是对象,这是一个基本的

    single dimension array as you can see here we’ve declared a variable and we’ve
    单维数组,如您在此处看到的我们’ 已经声明了一个变量,并且我们已经

    assigned it an array I like to call these simple arrays because they’re just
    为它分配了一个数组 我喜欢将这些简单数组称为这些简单数组,因为

    literal values before we begin I need to go over some shortcuts I’m going to be
    在我们开始之前它们只是文字值 我需要回顾一下我将

    doing in this slideshow this is because I just want to focus on the memory
    在本幻灯片中使用的一些快捷方式 这是因为我 只想

    address location over the whole picture but to preface when you create an empty
    关注整个图片的内存地址位置,但是在创建一个空变量时作为前言,

    variable what you’re really doing is you’re taking that variable and you’re
    您真正要做的是获取该变量并将

    pointing it to an area and memory address and this is what it would look
    其指向一个区域和内存地址 这就是

    like you would have a type in this case array and you would have a reference
    你在这种情况下会有一个类型数组的样子,你会有一个引用

    count and it’s one because one item is pointing to it now let’s say you do
    计数,它是一个,因为一个项目指向它现在假设你做

    variable another array is equal to empty array well you’re really pointing to
    变量另一个数组等于空数组你’ re 真的指向

    this object so it’s reference count goes up to two that’s basically the basics of
    这个对象,所以它的引用计数上升到两个,这基本上

    how it looks like in memory now another shortcut I’m gonna do in this video is
    是它在内存中的样子现在另一个快捷方式我要在这个视频中做的

    the following I’m just going to put an integer inside a block to represent how
    是下面我只是把一个整数放在一个里面 块来表示

    it behaves in memory but in reality what we’re really doing is when we do have an
    它在内存中的行为,但实际上我们真正在做的是,当我们确实有一个

    index we’re actually just pointing to another object in memory that of course
    索引时,我们实际上只是指向内存中的另一个对象,当然它

    keeps count of its reference or reference count so when we go ahead and
    会保持其引用或引用计数的计数,所以当我们去的时候 前进并

    add so when we go ahead and add a value in this case 1 what we’re really doing
    添加所以当我们继续并在这种情况下添加一个值 1 我们真正在做的

    is we’re pointing to that same object in memory and we’re increasing the
    是我们指向内存中的同一个对象并且我们将

    reference count by 1 and of course when we go ahead and change that value we’re
    引用计数增加 1 当然当我们继续时 并改变我们

    just pointing to a different object in memory and changing its value adding a
    只是指出的那个值 指向内存中的不同对象并更改其值,添加

    reference count of 1 and decreasing the reference count of the other now this
    引用计数 1 并减少另一个引用计数现在这一

    episode we only really care about the reference count or rather the location
    集我们只关心引用计数,或者更确切地说

    and address memory of the array rather than the address memory of the values so
    是数组的位置和地址内存而不是地址 记住这些值,所以

    that’s all I want to do is just preface but we’re not gonna pay any
    这就是我想做的只是序言,但我们不会对此给予任何

    attention to this all we care about is when I do short hands it’s really
    关注,我们关心的是当我做短手时,它确实

    representing this but even when I do shorthand our main focus for this
    代表了这一点,但即使我做速记时,我们的主要关注点也是如此

    episode is just the memory address location of the array and not the memory
    episode 只是数组的内存地址位置,而不是值的内存

    address locations of the values this is what a simple array or rather our my
    地址位置这是一个简单的数组,或者更确切地说,我们的 my

    array variable would look like in memory as you can see here we have our variable
    数组变量在内存中的样子,正如您在此处看到的,我们有我们的变量

    my array and it’s actually pointing to somewhere in our memory address keep in
    my 数组,它实际上是 指向我们内存地址中的某个地方请

    mind this is a basic simplification of how arrays behave in memory but
    记住,这是对数组在内存中的行为方式的基本简化,但

    nonetheless this is how it would normally behave variable points to a
    尽管如此,这通常是变量指向某个

    location memory address we have our indexes 0 1 2 and we have our values 1 2
    位置的行为方式 我们有我们的索引 0 1 2 并且我们有我们的值 1 2

    3 keep in mind that location memory address starts at the beginning of your
    3 请记住,位置内存地址从数组的开头开始

    array now let’s go ahead and take a look at what happens when we try to assign
    现在让我们继续看看当我们尝试分配该变量时会发生什么

    that variable my array into a different variable in this case our new variable
    在这种情况下,将我的数组放入不同的变量中,当我们使用 print 语句将其打印到控制台时,我们的新变量

    copy array will be assigned the values from my array when we print this out
    复制数组将被分配我数组中的值,

    into console using the print statement it will return 1 2 & 3
    它将

    same thing for copy array 1 2 & 3 but when you assign an array to another
    为复制数组 1 2 和 3 返回 1 2 和 3 相同的东西,但是当您将数组分配给另一个

    variable how does that look like in memory well as you can see here we have
    变量时,它在内存中的外观如何,您可以在此处看到我们有

    a variable copy array and it actually references the same array in memory
    一个变量复制数组,它实际上在内存

    location as the my array the specific reasons for this can get really deep and
    位置引用了与我的数组相同的数组,具体原因可能会变得非常深刻

    it is considered its own topic however to keep things simple just remember when
    它被认为是它自己的主题,但是为了简单起见,请记住,当

    you do direct manipulations such as copy array equals my array keep in mind we’re
    您进行直接操作时,例如复制数组等于我的数组请记住,我们

    not using a duplicate function what you’re really doing is you’re saying
    没有使用重复函数您真正在做的是 说

    wherever my variable my array is referencing in this case location 100
    我的变量在这种情况下我的数组引用的位置 100

    assigned that reference location to the variable copier what this means is that
    将该引用位置分配给变量 copier 这意味着

    if we were to change a value let’s say the value at index 0 which is 1 if we
    如果我们要更改一个值,假设索引 0 处的值是 1 如果

    were to change 1 into 100 what will happen is that copy array and my array
    我们要将 1 更改为 100 将发生的情况是,复制数组和我的数组

    will have their indexes changed to a hundred because they’re both pointing
    的索引将更改为100,因为它们都

    to the same memory address location so to take a look at that we have our
    指向相同的内存地址位置,所以看看我们有我们的

    assignment like in our previous slide my array at index 0 is equal to a hundred
    分配,就像我们在上一张幻灯片中我的数组在 索引 0 等于 100

    again notice how we’re using my array and we’re assigning it a hundred and
    再次注意我们如何使用我的数组并且我们将其分配为 100 并

    notice how when we’re printing copy array a different variable name it will
    注意当我们打印副本数组时如何使用不同的变量名它

    also change its value to a hundred even though my array and copy array both
    也会将其值更改为 100,即使我的 数组和复制数组都

    started with the values one two and three again this is because copy array
    以值一二和三开始,这是因为复制数组

    and my array both point to the same reference in address memory and so
    和我的数组都指向地址存储器中的相同引用,因此

    changing the value of one will actually change the value of the other now let’s
    更改一的值实际上会更改 t 的值 现在让我们

    go ahead and take a look at how memory behaves with a 2d array and all 2d array
    继续看看内存在二维数组中的行为方式,所有二维数组

    is is an array inside an array which can also be referred to as a sub array so
    都是数组中的一个数组,也可以称为子数组,因此

    our variable 2d array has the value 0 and then a sub array with the values 1
    我们的变量二维数组的值为 0,然后

    and 2 when we create our variable 2d array and assign it a value what we do
    当我们创建变量 2d数组并为其分配一个值时,具有值 1 和 2 的子数组我们所做的

    is we reference a location in memory address and as you can see here our
    是我们引用内存地址中的一个位置,正如您在此处看到的,我们

    index at 0 has the value 0 but notice how our value at index 1 is this
    在 0 处的索引具有值 0,但请注意我们的 索引 1 处的值

    reference in memory address that’s because this is how arrays behave in
    是内存地址中的此引用,这是因为这是

    memory for GT script when we create a sub array what we’re really doing is
    GT 脚本在创建子数组时数组在内存中的行为方式我们真正要做的是

    we’re calling a reference to another location and address memory and then
    调用对另一个位置和地址内存的引用,然后

    from there we continue our values in this case index 0 is 1 and index 1 is 2
    从 在这种情况下,我们继续我们的值,索引 0 是 1,索引 1 是 2,

    so you can see how this works if we want to call our sub array we call 2d array
    所以如果我们要调用子数组,我们将调用 2d 数组

    index 1 and then index 0 for the value 1 or index 1 for the value of 2 now if we
    索引 1,然后索引 0 表示值 1或索引 1,您可以看到这是如何工作的 如果我们

    take this 2d array and we assign it to a new variable in this case we’re calling
    把这个二维数组赋值给一个 2 现在的值在这种情况下,新变量我们调用

    our new variable copy 2d this is what it looks like what we’re doing is we’re
    我们的新变量 copy 2d 这就是看起来我们正在做的是我们

    taking the reference to location address memory we’re assigning that location to
    正在引用位置地址内存我们今天将该位置分配给

    our copy today’s so they both point to not only the first section of the array
    我们的副本所以他们都指向不仅是数组的第一部分

    but also the sub array so if I were to change a value here instead of 1 we turn
    ,还有子数组,所以如果我要在这里更改一个值而不是 1,我们会将

    this into a hundred what happens is when we do a print statement to either copy
    其变为 100,当我们执行打印语句以复制

    to and even 2d array it’s gonna show zero
    到甚至是 2d 数组时会发生 将显示零

    new value and then - because we’re referencing the same location address
    新值然后 - 因为我们为两个变量引用相同的位置地址

    memory for both variables now this is where the duplicate function comes into
    内存现在这是重复函数在您创建变量时多次发挥作用的地方,

    play many times when you create a variable you do not want to share the
    您不想

    values between two different variables the whole point of variables in most
    在两个不同的变量之间共享值在大多数情况下,变量的全部意义

    cases is basically isolation isolation meaning when you change one variable you
    基本上是隔离隔离,这意味着当您更改一个变量时,您

    should not be affecting other variables so the duplicate function comes with two
    不应该影响其他变量,因此重复功能有两个

    choices you can do a shadow copy or you can do a deep copy now if you do a
    选择,您可以进行影子复制 o r如果你做一个浅拷贝,你现在可以做一个深拷贝,

    shallow copy and keep in mind that shallow copies only makes a copy of the
    并记住浅拷贝只复制

    surface of your array this means that all sub arrays will still point to a
    你的数组的表面,这意味着所有子数组仍然指向

    reference in memory basically the same reference that your copied array is
    内存中的引用,基本上相同的引用 你复制的数组

    pointing to now a deep copy will make a copy of everything including sub arrays
    指向现在一个深拷贝将复制包括子数组在内的所有内容,

    so let’s go ahead and take a look at what that would look like in address
    所以让我们继续看看地址

    memory here’s an example of shallow copy we have a variable with a basic array
    存储器中的内容这是一个浅拷贝的例子我们有一个带有基本数组的变量

    and then we use the arrays built-in method called duplicate and we pass in
    然后我们使用名为 duplicate 的数组内置方法,我们

    the value of false by passing the boolean value false we are telling this
    通过传递布尔值 false 来传递 false 的值,我们告诉这个

    method to make a shallow copy of our basic array and then pass that in to
    方法对我们的基本数组进行浅拷贝,然后

    copy array when you use a shallow copy what you’re saying to the system the
    在你使用时将其传递给复制数组 使用浅拷贝你对系统说的

    compiler is that you want to take just a shallow copy of the array and put it
    编译器是你只想获取数组的浅拷贝并将其

    into a different location in memory address my array has an array at
    放入内存地址中的不同位置我的数组在

    location memory address 100 by doing a shallow copy you say variable copy array
    位置内存地址 100 b 处有一个数组 y 做一个浅拷贝,你说变量拷贝数组

    take the values from this location and then put that in a different memory
    从这个位置获取值,然后把它放在

    address location we can point to instead so as you can see here we have our
    我们可以指向的不同内存地址位置,所以你可以在这里看到

    location at 200 now if we change the values of my array we will not affect
    ,如果我们改变值,我们现在的位置是 200我的数组我们不会影响

    copy array and if we change a value and copy array we will not affect my array
    复制数组,如果我们改变一个值并复制数组我们不会影响我的数组

    let’s go ahead and take a look at what a shallow copy looks like when you’re
    让我们继续看看当你处理子数组时浅拷贝是什么样子的,你会

    dealing with sub arrays so as you notice here we have a sub array one two sub
    注意到这里 我们有一个子数组 1 2 子

    array with three four and we’re also passing the false value letting the
    数组和 3 4 并且我们还传递了 false 值,让

    compiler know that we want to do a shallow copy of my array and pass that
    编译器知道我们想要对我的数组做一个浅拷贝并将其传递

    into copy array so we have my array one two three four and it creates it sub
    给复制数组,所以我们有我的数组 12 3 4,它

    in location memory address 100 the values in index 0 and 1 are 1 & 2 and
    在位置内存地址 100 中创建它,索引 0 和 1 中的值是 1 和 2,

    just as we would expect our value in index 2 is just a reference to another
    正如我们期望索引 2 中的值只是对另一个

    address memory location where we continue the sub array index 0 3 & index
    地址内存位置的引用,我们继续子数组索引 0 3 & index

    1 4 so when we duplicate a rather when we when we do a shallow copy notice
    1 4 所以当我们 d当我们进行浅拷贝时复制 a 而当我们进行浅拷贝时注意

    we’re using the false value and our duplicate function when we do a shallow
    我们使用的是假值和我们的重复函数,当

    copy and we pass it into a new variable this is what it looks like so when we do
    我们将它传递给一个新变量时,这就是它的样子,所以当我们

    a shallow copy notice how we copy everything in the first layer we we take
    进行浅拷贝时 请注意我们如何在第一层复制所有内容我们获取

    everything and we basically duplicate it we create another location in 300 and
    所有内容并基本上复制它我们在 300 中创建另一个位置并

    notice how we have 1 & 2 separated from location 100 when we change indexes 0 n
    注意当我们更改数组中的索引 0 n 1 时我们如何将 1 和 2 与位置 100 分开

    1 in my array we do not affect copy arrays values and vice versa however we
    我们不影响 复制数组值,反之亦然,但是我们

    also copied my arrays reference to memory address location for its sub
    还将我的数组引用复制到其子数组的内存地址位置,

    array so as you can see here our sub arrays are both pointing to location 200
    因此您可以在此处看到我们的子数组都指向位置 200

    and if you were to change a value in this sub array it will affect how my
    ,如果您要更改此子数组中的值,它将 影响我的

    array works and it affects how copy array works so just keep that in mind
    数组的工作方式,它会影响复制数组的工作方式,所以请记住,

    you are copying everything including the reference to location and address memory
    您正在复制所有内容,包括对位置和地址内存的引用

    now let’s go ahead and do a deep copy so we have the same values in my array but
    现在让我们继续进行深度复制,以便我们在我的 数组,但

    this time instead of passing the fossil in we’re passing the true value in to
    这次不是将化石传递给我们,而是将真实值传递给

    our duplicate method and by doing this we’re telling the compiler to do a
    我们的重复方法,通过这样做,我们告诉编译器进行

    basically deep copy take all the values take all the sub arrays and give them
    基本的深度复制获取所有值获取所有子数组并给它们

    their own separate memory address location so again when we create my
    他们自己单独的内存地址位置,所以当我们再次创建我的

    array we’re creating it in memory address location and we’re creating a
    数组时,我们在内存地址位置创建它,我们

    sub array in a different memory address location let’s go ahead and take a look
    在不同的内存地址位置创建一个子数组让我们继续

    at a deep copy and how it looks like in memory address so when we use the
    看看深拷贝以及它是如何 看起来像在内存地址中,所以当我们使用

    duplicate method and pass in a true boolean what we’re telling the compiler
    重复方法并传入一个真正的布尔值时,我们告诉编译

    is we want to do a deep copy to my array and then we want to pass that value in
    器我们想要对我的数组进行深度复制,然后我们想要将该值传递

    to copy array so what that does is we look at the memory address for my array
    给复制数组,所以什么 那就是我们查看我的数组的内存地址,

    and we say ok let’s copy this and put it into memory address 300 but when you do
    然后我们说好的,让我们复制它并将其放入内存地址 300 但是当您

    the copy you also look at its references and then you go to the references and
    进行复制时,您还会查看它的引用,然后您转到引用

    you say hey let’s go ahead and take the sub-array and we’ll add it to a new
    并说嘿让我们 来吧,把潜艇 -array 我们会将它添加

    address in memory location where we will then pass a new reference to our copy
    到内存位置中的一个新地址,然后我们将向我们的复制数组传递一个新引用,

    array basically the array we want to do a deep copy for we pass the reference
    基本上是我们想要进行深度复制的数组,因为我们传递引用

    value or the new reference to memory address location to the new array so
    值或对内存地址的新引用位置到新数组,

    that’s how we get the value 400 there’s a lot going on but just know that at the
    这就是我们如何获得值 400有很多事情发生,但只知道在

    end of the day this is what it looks like in memory address so now when we
    一天结束时,这就是它在内存地址中的样子,所以现在当我们

    change the sub-array of copy array we will not affect my arrays sub array
    更改复制数组的子数组时,我们不会影响我的数组子数组,因为我的数组中的子数组

    because the sub array in my array is pointing or rather referencing an
    指向或引用

    address location that is not the address location our copy array is pointing to
    的地址位置不是我们的副本数组指向的地址位置,

    so in this case we’re pointing to 200 copy raised pointing to 400 when we do
    所以在这种情况下,我们指向 200复制指向 400 时 我们做

    this copy we get the same exact values if we were to compare them for equality
    这个副本,如果我们要比较它们是否相等,我们会得到相同的确切值,

    they would be equal but if we were to compare them to their memory address
    但如果我们要将它们与它们的内存地址位置进行比较,

    location they’ll come back false because everything in the deep copy is pointing
    它们会返回 false,因为深拷贝中的所有内容都

    to a different address in memory location versus our original array well
    指向一个 不同的添加 在内存位置与我们的原始数组相比,

    I hope you learned a lot today thank you to everyone who has subscribed to my
    我希望你今天学到了很多,感谢所有订阅我

    channel and thank you for clicking the like button there is no github project
    频道的人,感谢你点击喜欢按钮

    for this episode so I hope to see you in the next episode have a wonderful day
    这一集没有 github 项目,所以我希望在 下一集有美好的一天