Open hexstr1p opened 6 years ago
Thanks for the question.
First off - if I recall correctly, Day 4 had two coding sessions, so make sure to look at the repo after the Part 2 commit. I don't remember how I left things after part 1, but they might have been incomplete.
Looking at the repo for Day 4 Part 2, it looks like the components are created in dark.c at lines 98-103. Here, we're creating new structs for each of the components and then associating them to the game object using addComponentToGameObject().
Inside that function, in game.c, we're grabbing a pointer to the proper slot in the components array and then copying the information passed in the parameter to the record in the global array.
To illustrate, let's look at how we add a Position component (lines 81-87 in game.c): First, we're grabbing a reference to a Position struct that is already allocated and stored in the global positionComps array. For simplicity's sake, the index of the position component for a given game object is equal to that object's id. (line 81) Once we have a pointer to that struct, we copy the info from the component passed into the function into the struct in the global array. (lines 82-85) Finally, we add a reference to the component to the game object's components array, which will make it easier to get to that data from the GameObject. (line 87).
By using the pointer we get on line 81 to access the structure in the global array, we can store values in that structure.
I hope this explanation makes sense. Let me know if you have any other questions.
Okay, this is odd then. I've stepped through each line and in the addComponentToGameObject()
just now. The positionComps
at index 1 is being updated like it should and holds to values of pos
which are objectId = 1, x = 25, y = 25. Once it exits the function and goes back to dark.c continuing on line 100. My breakpoint sees that positionComps[1]
has then been cleared. Could this be because the pos
pointer in addComponentToGameObject
is destroyed once that function is exited? If that's the case that could one solution be to make the pos
pointer global? Then we would need to actually copy the values or it into the positionComps
at the correct index instead of simply storing a reference to pos
.
That sounds strange. The pointer being deallocated would have no effect on the data that it was pointing to, so making the pointer global would have no effect.
I wonder if the debugger is misreporting the contents of the global array. Try printf calls to dump the contents of that array element to the terminal once you've returned to dark.c.
I've been debugging this for a couple of days now, and I can't figure out how you got the loop in
renderScreen
to draw anything. The three global_variablesgameObjects
,positionComps
andvisibilityComps
are supposedly updated to hold the latest created GameComponent of its respective type, but when you create one of those components they are never added to the array. Take line 79 and 81 of dark.c for example. Those components are created, and then added to the player GameObject, but never added to the global_variable arrays. Even in theaddComponentToGameObject
function they are only interacted with as a reference. A reference filled with nothing. Yet in the for loop inrenderScreen
you are able to check each of those arrays and grab values out of them. In the earlier render test of the Day 4 video whenplayer
was a global variable you never accessed it's position through thegameObjects
array, but fromgetComponentForGameObject
from the player itself. How in the world did you get that for loop to work? I've used two IDEs to step through it and the values are always null or garbage. Please assist me in understanding this conundrum.