Skip to content

Lesson 25 - messed up example code #1060

Open
@Slayer-SK

Description

@Slayer-SK

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    contentIssues with the lessons, practices, including their code examples

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions