Weird Behavior In Code

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By tiernich
:warning: Old Version Published before Godot 3 was released.

So, its simple.

i walk with my character until enter in an area2D

When this happens i do this

on area enter:
    SomeList.append(SomeArray)

this Array is defined in the begining of the code, like this:

SomeArray = ["bla1", "bla2, "bla3"]

Meanwhile, in fixed_process( delta )

i check some things, and in a particular case i execute this line of code:

OtherList.remove(0) #always remove first index

Now the weird thing happens. When this line is executed, SomeArray(It dosent even is in fixed_process it is in a totally diferent part of the script, like i said, in enter area signal) has the first item removed!(SomeArray = ["bla2, “bla3”]). Why this happen?

Things to consider:

  • The line: OtherList.remove(0) is causing the problem, when indented, SomeArray stay the same. Its actually very easy to discover this because its a very simple script. 53 lines only

  • I just have this script, no other script in hole project and just one node with this script…

  • I dont have any SomeArray.remove(0) in any part of the code.

  • I just use SomeArray one time. in an append method. How can that line alterate this Array???

  • An workaround is forget about the array and use:

    SomeList.append(["bla1", "bla2", bla3"])

Something to do with references? i.e. if you assign a reference to another, then the ‘other’ will change the 'original. Haven’t examined your post too much though, but it sounds like “I assigned this to other, then changed other, and this is changed”. Sorry if I’m off target. Maybe post a whole script if it’s so short?

duke_meister | 2016-04-19 00:42

Thanks for the answer, but its not the case, posting the whole code:

extends RigidBody2D

var Status = "Guard"
var Weapon = "Pistol"
var action_with_gun = ["warning", "point", "shoot"]

var Agents = {
    agents = [],
    actions = [],
    agents_sequence = []
}

func _ready():
	set_fixed_process( true )

func _fixed_process( delta ):
    if Status == "Guard":
	    if Agents.agents_sequence != []:
		    var Index=Agents.agents.find(Agents.agents_sequence[0])
		    if Agents.actions[Index] != []:
			    if Agents.actions[Index][0] == "warning":
				    Agents.actions[Index].remove(0)
			    elif Agents.actions[Index][0] == "point":
				    Agents.actions[Index].remove(0)
			    elif Agents.actions[Index][0] == "shoot":
				    Agents.actions[Index].remove(0)

func Hearing( body ): # entering aread2d
    if Agents.agents.find(body) == -1:
        Agents.agents.append(body)
            if Weapon == "Pistol":
                Agents.actions.append(action_with_gun) 
                print(action_with_gun) # here should be allways ["warning", "point",
                "shoot"] but is ["point", "shoot"] and later ["shoot"] and finally: []
    Agents.agents_sequence.append(body)

tiernich | 2016-04-19 00:59

What is body? I still think it might be related to references. You’re appending a reference to action_with_gun to your Agents.actions array, then manipulating it. Maybe try creating a copy of it before appending it. Also what happens if you make action_with_gun a const?

duke_meister | 2016-04-19 01:21

body is the object entering the area2d - _on_body_enter signal. i will try const, one minute

tiernich | 2016-04-19 01:34

If you make that array a const (which it should be, if your intention is that it shouldn’t change) you’ll see it doesn’t change. Maybe it’s a clue as to what you need to do.

duke_meister | 2016-04-19 01:44

I think you’re right. Setting to const make the piece of code bellow repeat forever(the remove(0) dont work), but the array still the same, the problem is my code logic haha

if Agents.actions[Index][comment5-0] == "warning":
    Agents.actions[Index].remove(0)

i will study more about arrays, references and stuff :slight_smile: for now the workaround mentioned before is solving my problem

really thanks for your time!

tiernich | 2016-04-19 01:48

how should i proceed with the Answer thing? You can post: “Answer in the comments” and i will choose as best.

tiernich | 2016-04-19 01:56

:bust_in_silhouette: Reply From: duke_meister

I would check up on references; this is the default in Godot. i.e. ‘copying’ arrays around will not make copies, but references or aliases to the original.

Also look into using ‘const’ when appropriate, like with your action_with_gun array. That alone can pinpoint many bugs.

On top of that I think there might be logic problems with your code. Look into Arrays more, and Arrays of Arrays, and make sure your code is doing what you think it is.

Sorry this isn’t a definitive answer, but if anyone adds such an answer, choose that :slight_smile:

Writing decent AI for my game it’s proving to be quite difficult, but i’m loving the process :DDD

tiernich | 2016-04-19 02:14

Yeah it’s a lot of fun for sure and can be frustrating to get ‘roadblocks’, and sometimes you’re not sure if it’s a bug in the product (being an alpha/beta) or it could be because the documentation is not so great…

duke_meister | 2016-04-19 02:54