Run script when egg popped

Started by safewayshopper, April 22, 2022, 01:43:17 AM

Previous topic - Next topic

safewayshopper

Hi,

I just started learning how to script for Creeper World 4 to make a silly map idea, but I can't figure out quite how to get it to work.

I want to run a script when a specific egg is popped. I'm pre-populating the map with the egg in the editor with
CreateEgg(V3(64 6 64) <-initData) ->egg
Then I would like to register a function to that egg's Destroyed callback, so I can make something happen once the player pops it.

After looking over the documentation I wasn't able to find any functions in the API that allow this kind of dynamic callback registration.

Next I looked into making the special egg as a CPACK. Since I am not familiar with creating CPACKs, it seems like it would be a lot of work to configure all the custom unit settings to just recreate the existing behavior of the egg. I want my special egg to be indistinguishable to normal eggs in the game. The egg model and textures weren't in the stock model selector, so it seems like I would have to extract those files from the Creeper World game files and import and configure them for my custom unit.

I guess as a different workaround, I could just not include anything on the map that creates eggs, and use a global script to check for the number of eggs on the map every frame, or every few frames, and then trigger my function when there are 0 eggs left. This seems kind of wasteful, and constrains my map design options quite a bit, but I think it would be theoretically possible with the scripting API.

Let me know if you have any advice. I appreciate any efforts to help me create my SPECIAL EGG.


P.S. While messing around, I tried to create an egg with CreateUnit("egg" V3(64 6 64) <-initData) ->egg, instead of using CreateEgg. This resulted in a bug/strange behavior where an "airsacbubble" was created instead of an egg. Not a problem, just thought it was interesting.

knucracker

You should be able to create a global script that monitors for the destruction of your special egg(s).  That global script would call GetUnitDestroyed(<-egg) and then act accordingly when the egg was destroyed.  Essentially, the global script would just be polling for destruction every frame.  So long as it is polling for a relatively small number of things (says hundreds of eggs or less), it won't be a performance concern.

To make it work either the global script needs to create the special egg(s), so it has the references to them.  Or, it needs to be handed the egg references by the scripts that create the eggs.  One way to make the egg references visible across different scripts with with global variables: http://knucklecracker.com/wiki/doku.php?id=4rpl:commands:specialsyntax#global_variables
If you just have one special egg at a time, then you could store that in a global variable and then the global script could reference that.  If you have multiple eggs you want to watch, then you could put them in a list that is a global variable.

So:
CreateEgg(V3(64 6 64) <-initData) ->*egg
Would store the reference globally for a single egg.

The global script would call
GetUnitDestroyed(<-*egg)
to see if the egg was destroyed.  It would then send msg, set some other global var, or directly do whatever you want to do when the special egg is destroyed.

Note that references to object, like <-egg or <-*egg just return integers.  So GetUnitDestroyed will return false if you pass in an 'invalid' reference or an old reference.  That API just takes the number passed in and sees if there is a live object with the UID.  If not, it returns false. So depending on what you do you may need another variable to control whether the global script checks the reference every frame, or you might use a value of 0 in the <-*egg reference to mean "don't check".

safewayshopper

I see; thank you so much for the feedback! I'm excited to try this and finally unleash THE EGG.

The Apocalyptic