Modding Help Attackable Switches

Discussion in 'Starbound Modding' started by Bubbline, Feb 3, 2024.

  1. Bubbline

    Bubbline Scruffy Nerf-Herder

    I'm currently making a race mod based on Wolf Link from Nintendo's The Legend of Zelda series. I want to add switches that you can hit with a weapon to toggle their on-and off states, a staple of the series' puzzle solving. I've looked at Starbound's own destructible target switches from the mech training course, hoping to find something there, but those are just always on and are smashable - the "toggle" is just them being destroyed and the power being cut. I want to make a switch you can hit again and again. Would this be possible within Starbound?

    My best guess was to look at how the "smashable" function works, since I assume it's simply "give object hitbox, when object is attacked, run destroy script," so the obvious solution would be to make a similar function but replace "run destroy script" with "toggle switch state," but I haven't been able to find the actual bit of code that defines that behavior to work from. I've done as much digging as possible into what scripts and assets are extractable and I assume it's in an inaccessible part of the source code, or otherwise heavily obfuscated within the game's accessible scripts. Any direction towards how to define a new behavior like that would be incredibly helpful.

    Please be gentle, but also don't be afraid to explain something to me regardless of ostensible simplicity; I'm very much a baby modder, and my knowledge still lies squarely within laymen's understanding.

    ^ The switch I'm trying to make toggle-able via attack instead of interaction.
    Last edited: Feb 5, 2024
  2. rl.starbound

    rl.starbound Phantasmal Quasar

    Interesting problem.

    All objects have some "health". I think it defaults to 1.0 if not specified. Most objects are not smashable, which means that only mining tools, such as the matter manipulator, can apply damage to them. When an object breaks, the default break drop pool is the object. When a smashable object is smashed, the default smash drop pool is, I think, nothing. As you've pointed out, the breakable switches are simply on always, and go off when they're destroyed.

    What you want is to allow weapons to damage an object, but not to allow an object to be destroyed. That way, an object can trigger a Lua function any time it's hit, and you can hit it an infinite number of times. My initial thought is to make an object like the monster Punchy, which has 65535 health and regenerates 100% of health on each tick. However, I'm not sure how to set regeneration on an object, or if it's even possible. Likewise, I'm not even sure if object scripts can hook damage reports, like monsters can, to trigger actions on damage.

    Another idea would be to make a stationary "monster" to be the target, and set it up like Punchy with very high health and infinite regen. And then have the monster send a message (via sendWorldMessage) to an object to turn on or off each time it's hit. You could set it up so that the object is invisible and exists directly underneath the monster, because monsters and objects can occupy the same space. This solution seems inelegant and complicated, but it's the only thing that springs to mind on short notice. (Example of why this will probably go wrong, even if it works, is if you hit it with automatic weapons fire, it may toggle on and off many times in rapid succession. So perhaps you'd want to set it to only trigger once every so many seconds, and ignore high-frequency hits, but then the code gets more complicated.)

    There may be a more elegant solution making use of an API of which I'm not aware.
    Last edited: Feb 26, 2024

Share This Page