View not updating after Update runs #1343
-
Hello, I'm having an issue while trying to make a simple conway's game of life where after running the update logic, the view doesn't redraw. I did some simple checks whether or not the logic works and it does, but the view doesn't want to redraw. Link to the repo is here: cgol Would also like to ask how I could make it after getting the view to update on space, to update every second automatically instead. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
This looks cool!
Here's the diff: diff --git a/main.go b/main.go
index 4bb3ac6..7e41b7d 100644
--- a/main.go
+++ b/main.go
@@ -18,6 +18,7 @@ const (
type gol struct {
grid [ROW][COL]int
buffer [ROW][COL]int
+ step int
}
// Create array that holds field's to check for neighbours
@@ -74,6 +75,7 @@ func (g gol) Life() tea.Model {
}
}
}
+ g.step++
return g
}
@@ -86,8 +88,7 @@ func (g gol) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case "ctrl+c", "q":
return g, tea.Quit
case " ":
- g.Life()
- return g, nil
+ return g.Life(), nil
}
}
return g, nil
@@ -95,7 +96,7 @@ func (g gol) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// Let's draw this all simply
func (g gol) View() string {
- var s string
+ s := fmt.Sprintf("Step: %d\n", g.step)
// Making sure to not draw the empty field around the array
for i := 1; i < ROW-1; i++ {
for j := 1; j < COL-1; j++ { To update every second have a look at |
Beta Was this translation helpful? Give feedback.
This looks cool!
View
actually is being called after pressing space, so there's likely still be an issue in your logic. To demonstrate I added a counter that shows the current step in the game which redraws after pressing space. Also, note that you weren't returning the new model after so I fixed that as well.Here's the diff: