![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | GreyMiller |
I’ve got into a weird situation, I have no idea how this happens…
(initially i was trying to generate a curved road of variable width, but I’ve shortened the code as much as possible). I’ve created a class, the problem doesn’t happen when I use a simple int. The problem is in “for” loop in generate
function:
extends Node
class Pathpt:
var wid: int
var points = [] #array of class Pathpt
const leng = 4
var lastpt = Pathpt.new()
func _ready():
#just initializing stuff, nothing much important here
points.resize(leng)
for i in range(leng):
points[i] = Pathpt.new()
lastpt.wid = rand_range(50, 150)
generate(lastpt)
func generate(startpoint): #startpoint should be of class Pathpt
points[0] = startpoint
for i in range(1, leng):
print(i, ", ", points[0].wid)
points[i].wid = rand_range(50, 150)
print(i, ", ", points[0].wid)
lastpt = points[leng - 1]
func _GenBtn():
print("generate")
generate(lastpt)
Note that I print only the 0th element of array which should not be changed in the loop at all, so I expected to see the same numbers until I press “generate” button (connected to _GenBtn
function). What I actually get is:
1, 112
1, 112
2, 112
2, 112
3, 112
3, 112
generate
1, 81
1, 81
2, 81
2, 81
3, 81
3, 125
generate
1, 125
1, 125
2, 125
2, 125
3, 125
3, 86
So except for the first time when function is called from _ready()
, the 0th element changes on the last run of “for” loop. According to the way my full code works, the 0th element becomes equal to the last one as soon as the last is calculated.
When I remove the string lastpt = points[leng - 1]
the problem disappears, but moving it to _GenBtn
function does not help. Most surprisingly, neither does even replacing it with the following:
var arrdup = points.duplicate()
lastpt = arrdup[leng - 1]
I just don’t understand how this happens. Any solutions to this?