If the only change is from null-source to source-with-monitor, it might be simpler to make two scenes and switch between them by calling
obs_frontend_set_current_scene.
To change the configuration of a Source within a Scene
- Create a Settings object for the Source by calling obs_data_create
- Set the necessary parameters by calling obs_data_set_XXX (where XXX is string, bool, etc)
- Update the Source with the new Settings by calling obs_source_update
To figure out WHICH settings, you can use an editor to view the .json file for your scene collection. For a "monitor capture" Source, it is "monitor_id", but the value is an ugly glob of GUIDs that you will probably need to read from your json after manually editing the Source.
As a simpler example, the script at
https://github.com/OldBaldGeek/OBS-old-bald-scripts/blob/main/SimpleSlides.lua changes the "file" setting of an Image Source to implement a slide show. But the data there is a just a filespec vs your "monitor_id" The sequence of calls and memory releases should be similar, however