hello, iam having trouble rezising an array to 0

var animFrameMax = 1
var root1 = {
	'Fnum': [0]
}

func _createAnim():
	root1.Fnum.resize(animFrameMax);
	for n in animFrameMax:
		root1.Fnum[n] = {
			#----do-something----
		}

if i set the VAR animFrameMax to ZERO i get an error "invalid get index 0"

does animFrameMax = 1 means iam in the postition [0] of the array ?

The in keyword is used for arrays. You probably want to do this (for integer numbers):

for n in range(animFrameMax):

@cybereality said: The in keyword is used for arrays. You probably want to do this (for integer numbers):

for n in range(animFrameMax):

the result is the same... I have another function, to add a frame instead of creating an anim with x nÂș of frames and i have to set the integer to -1 ( animFrameMax-1 )

func _AddAnimFrame():
	
	root1.Fnum.resize(animFrameMax);
	
	root1.Fnum[animFrameMax-1] = {

	]

if i set it to root1.Fnum[animFrameMax] ...without the -1 error --- "invalid set index 2"

does the array position[0] corresponds to animFrameMax=1 ?? like: animFrameMax = 4 ///// Fnum[3] Fnum[0] -> animFrameMax 1 Fnum[1] -> animFrameMax 2 Fnum[2] -> animFrameMax 3 Fnum[3] -> animFrameMax 4

To be honest, the code you posted in the OP makes no sense to me. What are you trying to do?

@cybereality said: To be honest, the code you posted in the OP makes no sense to me. What are you trying to do?

its a json file, it stores parts like head, arms, legs in the current frame:

each root1.Fnum[frame] contains a position, angle, scale, 9 bones

rootPath = root1.Fnum[frame].head1; rootPath.pos = vector2(etc..., etc...) rootPath.scale = vector2(etc..., etc...) rootPath.angle = etc...

if ( key_right ) frame += 1; if ( key_left ) frame -= 1;

if the array Fnum[0] is Zero and frame = 0 it gives an error "invalid set index '1' "

I don't understand what you're trying to do either.

If you resize an Array to zero, it's empty. The element with index 0 does not exist.

@DaveTheCoder said: I don't understand what you're trying to do either.

If you resize an Array to zero, it's empty. The element with index 0 does not exist.

that means the position of array[0] in var animFrameMax is 1... ( animFrameMax = 1 ) array[animFrameMax ] is array[0]

Are you really not getting additional warning and error messages? Some of your code is not valid GDScript.

Examples:

        root1.Fnum[n] = {
            #----do-something----
        }



        root1.Fnum[animFrameMax-1] = {
        ]

And I strongly recommend using static typing. It's very useful in detecting problems.

@jonSS said: that means the position of array[0] in var animFrameMax is 1... ( animFrameMax = 1 ) array[animFrameMax ] is array[0]

My apologies, if this is too simple, but are you getting confused by the zero-based numbering?

Yeah, basically size and last position are not the same. Size is last position + 1 because it starts at 0. If it has a size of 0, it's a null variable and has no positions.

To make it more confusing, the index -1 references the last element, -2 references the next to last element, etc. :smile:

So, I don't think it's about the index number. It's that the array doesn't exist. I still think the code makes no sense, but this may work.

    var animFrameMax = 1
    var root1 : Dictionary

    func _createAnim():
        root1["Fnum"] = []
        for n in range(animFrameMax):
            var data = {} # some dictionary
            root1["Fnum"].append(data)

@cybereality said: So, I don't think it's about the index number. It's that the array doesn't exist. I still think the code makes no sense, but this may work.

    var animFrameMax = 1
    var root1 : Dictionary

    func _createAnim():
        root1["Fnum"] = []
        for n in range(animFrameMax):
            var data = {} # some dictionary
            root1["Fnum"].append(data)

yes, but you ve valued animFrameMax = 1 that was my question. animFrameMax = 1 equals to array[0] animFrameMax = 0 equals to array[]

i have 3 vars animFrameMax = 10; animFrame = 0; #--loops trew 0,1,2,3,4,5,6,7,8,9 if bigger than animFrameMax -1 equals 0 root1.Fnum[];

i want to increase the animation max number of frames +1, frist i have to add animFrameMax +=1, ( it was =10 now its 11 ) then resize the array to animFrameMax... then insert the +1 frame at the end of the array, witch is animFrameMax -1,

func _AddAnimFrame():
	
	root1.Fnum.resize(animFrameMax);
	
	root1.Fnum[animFrameMax-1] = {
	"pt1": { "fm": dt1_Fm, "pos":dt1_Po, "pz": dt1_Z, "pa": dt1_A, "up1": dt1_Up1, "up2": dt1_Up2, "up3": dt1_Up3, "md1": dt1_Md1, "md2": dt1_Md2, "md3": dt1_Md3, "dn1": dt1_Dn1, "dn2": dt1_Dn2, "dn3": dt1_Dn3 },
	"pt2": { "fm": dt2_Fm, "pos":dt2_Po, "pz": dt2_Z, "pa": dt2_A, "up1": dt2_Up1, "up2": dt2_Up2, "up3": dt2_Up3, "md1": dt2_Md1, "md2": dt2_Md2, "md3": dt2_Md3, "dn1": dt2_Dn1, "dn2": dt2_Dn2, "dn3": dt2_Dn3 },
	"pt3": { "fm": dt3_Fm, "pos":dt3_Po, "pz": dt3_Z, "pa": dt3_A, "up1": dt3_Up1, "up2": dt3_Up2, "up3": dt3_Up3, "md1": dt3_Md1, "md2": dt3_Md2, "md3": dt3_Md3, "dn1": dt3_Dn1, "dn2": dt3_Dn2, "dn3": dt3_Dn3 },
	"pt4": { "fm": dt4_Fm, "pos":dt4_Po, "pz": dt4_Z, "pa": dt4_A, "up1": dt4_Up1, "up2": dt4_Up2, "up3": dt4_Up3, "md1": dt4_Md1, "md2": dt4_Md2, "md3": dt4_Md3, "dn1": dt4_Dn1, "dn2": dt4_Dn2, "dn3": dt4_Dn3 },
	"pt5": { "fm": dt5_Fm, "pos":dt5_Po, "pz": dt5_Z, "pa": dt5_A, "up1": dt5_Up1, "up2": dt5_Up2, "up3": dt5_Up3, "md1": dt5_Md1, "md2": dt5_Md2, "md3": dt5_Md3, "dn1": dt5_Dn1, "dn2": dt5_Dn2, "dn3": dt5_Dn3 },
	"pt6": { "fm": dt6_Fm, "pos":dt6_Po, "pz": dt6_Z, "pa": dt6_A, "up1": dt6_Up1, "up2": dt6_Up2, "up3": dt6_Up3, "md1": dt6_Md1, "md2": dt6_Md2, "md3": dt6_Md3, "dn1": dt6_Dn1, "dn2": dt6_Dn2, "dn3": dt6_Dn3 },
	"pt7": { "fm": dt7_Fm, "pos":dt7_Po, "pz": dt7_Z, "pa": dt7_A, "up1": dt7_Up1, "up2": dt7_Up2, "up3": dt7_Up3, "md1": dt7_Md1, "md2": dt7_Md2, "md3": dt7_Md3, "dn1": dt7_Dn1, "dn2": dt7_Dn2, "dn3": dt7_Dn3 },
	"pt8": { "fm": dt8_Fm, "pos":dt8_Po, "pz": dt8_Z, "pa": dt8_A, "up1": dt8_Up1, "up2": dt8_Up2, "up3": dt8_Up3, "md1": dt8_Md1, "md2": dt8_Md2, "md3": dt8_Md3, "dn1": dt8_Dn1, "dn2": dt8_Dn2, "dn3": dt8_Dn3 },
	"pt9": { "fm": dt9_Fm, "pos":dt9_Po, "pz": dt9_Z, "pa": dt9_A, "up1": dt9_Up1, "up2": dt9_Up2, "up3": dt9_Up3, "md1": dt9_Md1, "md2": dt9_Md2, "md3": dt9_Md3, "dn1": dt9_Dn1, "dn2": dt9_Dn2, "dn3": dt9_Dn3 },
	"pt10": { "fm": dt10_Fm, "pos":dt10_Po, "pz": dt10_Z, "pa": dt10_A, "up1": dt10_Up1, "up2": dt10_Up2, "up3": dt10_Up3, "md1": dt10_Md1, "md2": dt10_Md2, "md3": dt10_Md3, "dn1": dt10_Dn1, "dn2": dt10_Dn2, "dn3": dt10_Dn3 },
	"pt11": { "fm": dt11_Fm, "pos":dt11_Po, "pz": dt11_Z, "pa": dt11_A, "up1": dt11_Up1, "up2": dt11_Up2, "up3": dt11_Up3, "md1": dt11_Md1, "md2": dt11_Md2, "md3": dt11_Md3, "dn1": dt11_Dn1, "dn2": dt11_Dn2, "dn3": dt11_Dn3 },
	"pt12": { "fm": dt12_Fm, "pos":dt12_Po, "pz": dt12_Z, "pa": dt12_A, "up1": dt12_Up1, "up2": dt12_Up2, "up3": dt12_Up3, "md1": dt12_Md1, "md2": dt12_Md2, "md3": dt12_Md3, "dn1": dt12_Dn1, "dn2": dt12_Dn2, "dn3": dt12_Dn3 },
	"pt13": { "fm": dt13_Fm, "pos":dt13_Po, "pz": dt13_Z, "pa": dt13_A, "up1": dt13_Up1, "up2": dt13_Up2, "up3": dt13_Up3, "md1": dt13_Md1, "md2": dt13_Md2, "md3": dt13_Md3, "dn1": dt13_Dn1, "dn2": dt13_Dn2, "dn3": dt13_Dn3 },
	"pt14": { "fm": dt14_Fm, "pos":dt14_Po, "pz": dt14_Z, "pa": dt14_A, "up1": dt14_Up1, "up2": dt14_Up2, "up3": dt14_Up3, "md1": dt14_Md1, "md2": dt14_Md2, "md3": dt14_Md3, "dn1": dt14_Dn1, "dn2": dt14_Dn2, "dn3": dt14_Dn3 },
	"pt15": { "fm": dt15_Fm, "pos":dt15_Po, "pz": dt15_Z, "pa": dt15_A, "up1": dt15_Up1, "up2": dt15_Up2, "up3": dt15_Up3, "md1": dt15_Md1, "md2": dt15_Md2, "md3": dt15_Md3, "dn1": dt15_Dn1, "dn2": dt15_Dn2, "dn3": dt15_Dn3 },
	}

There... this time ive posted the whole function...

i dont know... but phyton seems to have to have some pre defined vars. the for loop for example has (n)

# loop for n = 0 to 7
for n in 8:
    print(n)

https://gdscript.com/tutorials/looping/

i dont know... but phyton could ve have a variable that it was equal to the array... and this confusions would be avoided...

godot has the same bug, In the texture animation, you add 3 frames and it has 2... 0,1,2

but, its better to stay that way or the code could become more confusing

And at the risk of derailing the thread, let me rant for a moment about zero-based counting.

My favorite language, apart from python/gdscript, is lua. Two of the things I like about lua are "everything is a table" and one-based "arrays".

I've been programming for more than forty years, so I'm comfortable with zero-based counting, and back when I started, it made sense. You asked the system for a chunk of memory, and got a pointer to it. When you wanted the first element of your array, you used the pointer (+0). That was fine for how you accessed arrays back then.

But no one does that anymore. You (almost) never see someone manipulating a raw chunk of memory, because that leads to out-of-bounds errors and other security nightmares. You always use a class or a language structure that handles all the referencing for you. So why do we still use zero-based counting? Tradition (i.e. bad habits).

People don't normally count from zero. Why would you count something that isn't there? We had advanced mathematics before anyone even thought of the concept of zero. And yet, programmers still use zero-based math, and sometimes even get annoyed at new programmers who get confused by it, because we don't remember how dumb it seemed to us back then.

/rant off

There are ten numbers in a decimal system, and zero is technically the first number. So it makes perfect sense.

I think it's who do you want to get angry, a bunch of newcomers that don't really care or a bunch of programmers who have been doing it that way for decades. Plus if people want to learn another language it would just be more confusing for them. When we used the old for loops you always had to set the top range as size-1, so you got used to it right away. Now we have for var in arrayname so it's not so plane.

@duane said: People don't normally count from zero. People also don't normally do anything amazing with their life.