How to Disable a Script in Defold Without Removing Other Components

I want to disable a script attached to a game object while keeping the other components like the mesh intact.

Basically, I just want to stop the update function from running. I don’t want to keep checking a boolean every frame inside update and returning early.

function update(self, dt)
    if closed then return end

Is there a clean way to completely disable the script component without affecting the rest of the game object?

No. You need to manually check this in update.

2 Likes

Can I remove the script in runtime? @britzl

I would probably do a separate game object for this script and spawn it, when it’s needed (as a child to this parent go with other components) and delete it, when it’s not needed.

One check question - you can’t make this logic happen just in init and not update? Even using timers maybe?

Visually, the two bombs are actually the same. One of them is thrown and has full bomb behavior (exploding, scaling animation, update logic, etc.). The other one is just used to represent the bomb being held in the character’s hand (FPS view), meaning it only needs the mesh and material for visual purposes.

So logically they are different, but visually they are identical.

I’m trying to avoid creating an extra game object just to remove or disable one script component. In the future, there will be many items in the game, possibly even procedurally generated items (like in Diablo-style systems), so I’m thinking in a more scalable way. That’s why I was considering using some kind of template-based game object approach instead.

About your question: I don’t think the logic can live only in init, because the thrown bomb needs continuous checks (like timers, scaling over time, explosion trigger, etc.). Timers could replace some parts of update, but not everything. The held bomb, on the other hand, doesn’t need any of that logic at all it’s purely visual.

2 Likes

It sounds like a tiny state machine to me. You may not need the update method at all depending on what the bomb does (how it moves, etc.). Idle state does nothing (holding it), when throwing switch to active state and call whatever functions you need for the throwing, dead/end state can trigger explosion animations and etc. IMO don’t focus on “how to remove the script”. The script can be there doing nothing while in idle state. I hope it makes sense.

2 Likes

Or create a generic “handheld item” which just handle sprite/collisison via an object properties. So you have 1 game object for ALL your items. Then when you need to actually throw it, replace it with the scripted specific gameobject.

But as stated, with a little state machine and just importing different modules in your scripted item, you should already have just one gameobject for ALL items…

3 Likes