learn-gdscript icon indicating copy to clipboard operation
learn-gdscript copied to clipboard

Lesson 25 - messed up example code

Open Slayer-SK opened this issue 10 months ago • 0 comments

Using a for loop, you can use the key-value pairs to place units on the board at the start of a game.

func run(): for cell in unit_cells: var unit = unit_cells[cell] add_unit(unit, cell) <--- this is messed up - it must first start with cell, then unit, otherwise it wont work

i used this code example to do the second practice only to have a bug constantly and didnt understand what was going on. my code was pretty much exactly like the solution, except for that tiny detail. i didnt know at that time that it actually is important to first do the coordinates and then the unit type after.

correct code looks more like this:

func run(): for cell in unit_cells: var unit = unit_cells[cell] add_unit(cell, unit)

once i debugged my code, i successfully solved the puzzle:

var units = { Vector2(1, 0): "robot", Vector2(2, 2): "turtle", Vector2(3, 0): "robot", }

func run(): for cell in units: var unit = units[cell] place_unit(cell, unit)

Slayer-SK avatar Mar 11 '25 21:03 Slayer-SK