07 Note and Entity Data
In this chapter, we will set up the Note archetype and add the first note entity to the development level.
Note Archetype
First, add Note's shared archetype name below the existing names in guide/lib/archetype_names.py:
python
NOTE = "Note"Then create guide/play/note.py and declare the Note archetype. Its temporary spawn order places it after Stage. Chapter 09 will replace this order with Note's timed spawning logic.
python
from sonolus.script.archetype import PlayArchetype
from guide.lib import archetype_names
class Note(PlayArchetype):
name = archetype_names.NOTE
def spawn_order(self) -> float:
return 0Open guide/play/mode.py. Import Note, then append it to the archetypes list:
python
from guide.play.note import Note
play_mode = PlayMode(
archetypes=[Initialization, Stage, Note],
# ...
)Entity Data
Until now, we only had Initialization and Stage, which should behave the same across all levels.
Notes are different. The first note might occur at 5 seconds in one level and at 2 seconds in another. One level might contain 200 notes while another contains 30.
Level Data is one shared set of values for the whole level. Entity Data belongs to one entity, so each Note can have different values. A level supplies imported fields in Entity Data when it creates each entity. The imported declaration gives the Note archetype a time field containing the note's time in seconds. Replace the existing archetype import in guide/play/note.py, then add the field:
python
from sonolus.script.archetype import PlayArchetype, imported
class Note(PlayArchetype):
# ...
time: float = imported()The Python field name also becomes the imported data name in the level. We can access the value through self.time in Note's callbacks. To test it, add the debug_log import and an update_parallel callback:
python
from sonolus.script.debug import debug_log
class Note(PlayArchetype):
# ...
def update_parallel(self):
debug_log(self.time)Lastly, open guide/level.py. Import Note beside the existing play archetypes. Then add Note(time=2) after Stage() in the entity list:
python
from guide.play.note import Note
level = Level(
# ...
data=LevelData(
bgm_offset=0,
entities=[
Initialization(),
Stage(),
Note(time=2),
],
),
)Note(time=2) creates one level entity that uses the Note archetype and supplies 2 for its imported time field. Reopen the development level and check the debug log. It should print 2 once per frame for this entity.