Godot Version
4.6.1
Question
i have a hidden object game. There is only one screen but i swap out the screen contents for each level. It looks like this:
- Level (master window with generic buttons, displays, etc. that hosts all the levels)
- LevelData (data for a specific level, such as the art and array of HiddenObject)
- HiddenObject (glorified texture buttons)
The code (in Level) to load a specific LevelDatait looks like:
var play_area : LevelData
play_area = packed_scene.instantiate() as LevelData
%PlayArea.add_child(play_area)
hidden_objects = %PlayArea/LevelData/HiddenObjects.get_children()
When you click on an object it runs HiddenObject::react_to_being_discovered() which, among other things, does emit_signal(“discovered”, name). This is handled by Level::_on_hidden_object_discovered().
All of that works. Here’s what i’m trying to change: There is one specific level (LevelData; i should rename these to be less confusing) where i want the reaction to be different. Rather than play a sound and fade out they should interact with another object on that LevelData. So for Levels A, B and D they get the default “object found” reaction but on level C they get something else. And i’m not sure how to wire that up.
My best guess so far: Make a new class inherited from LevelData just for that level (CLevelData) and have Level call that.
if play_area.has_special_find_reaction:
play_area._on_hidden_object_discovered(object_name)
It would require (i think) LevelData to have a blank method with the right name and then have the inherited class override it. That works but it means creating a couple more classes. i’m wondering if there’s a more elegant way to do this. Maybe export a variable that takes a script that has custom processing code or something. Not sure. It just feels like there’s a better way.
Could you give an example for the interaction with another object? What should happen?
Right now I have only 2 untested ideas.
The first one is to spawn a scene when the level is completed that does the things when the level is complete. And level C spawns a different scene.
The other is to create different resources that each do different things. One for screen fade or sound. One for doing things with an object. And them assign them to each level to run them after the level is completed.
But I’m not sure if either of them is really an good option. 
Sure, i love talking about my game!
This is a hidden object game. Level (maybe i should call it LevelContainer) has the list of items to find, magnifying glass, etc. When you load it you tell it the name of a Level to load. That Level has pictures of objects to find and an array of HiddenObject. When you click on an object (find it) the object fades out and plays a sound. That’s the default behavior.
One one specific level i want different behavior. There is a superhero in the sky. When you click on an object the superhero fires lasers at the object. Might be nice to add a particle effect and a different sound but so far it’s just the lasers.
The interaction doesn’t end the level. If you have to find five objects you’ll see this laser attack five times before winning the level and moving on.
So far this is the only special interaction but i’ll probably thing of more. Different sounds, different particle effects, that sort of thing.
At the moment the HiddenObject signals the Levelwhich plays the sound, updates the score, removes the name from the list and checks if you won. Those last 3 use objects on Level so have to go there. HiddenObject disables itself, fades out and queue_free()s itself.
2 Likes
This looks really nice, I love it! 
Unfortunately I’m old and have to go to bed now. 
I find this “problem” interesting and will think about it in bed.
The first thing that popped into my mind was a signal that emits when an object has been found. This might be useful for other things later and the laser could connect to it and fire on the found object.
That green guy hiding in the background somehow looks familiar 
1 Like
This may be the best approach, extending scripts is a good way to get custom behavior built on top of other functionality. Your extended scripts shouldn’t need a class_name for each and can extend a script without a class_name if that’s what you are worried about with “creating a couple more classes”
If this did work it would have the same draw back of requiring more scripts per custom behavior. It could work with variables pointing to functions, but functions cannot be meaningfully exported (outside of tool script buttons).
I still may be missing the larger scope of this, but I think it’s worth trying your first idea and if you run into anything let us know!
Maintain some modality flags in HiddenObject and let it pass a reference to itself as an argument to the discovered signal. Let Level check modality flags and act accordingly when it receives the signal.