Jump to content

Is it possible to disable callback action?


DrMasik

Recommended Posts

Sorry for poor English

 

Is it possible to disable callback actions?

Example: Callback "onDamage". I want to disable damage to entity for some reason.

In some games, the status of the callback function is checked. And if the return status != true, the action not applied.

Link to comment
Share on other sites

Hi.

 

You can use this to turn entity invincible. Shield still can be damaged, but durability will not. You can use this in the "update" function when some conditions are met. And you will need to turn it off (false) once you're done.

Entity():invincible = true

 

Avorion doesn't have the feature that allows to cancel an event.

Link to comment
Share on other sites

Hi.

 

You can use this to turn entity invincible. Shield still can be damaged, but durability will not. You can use this in the "update" function when some conditions are met. And you will need to turn it off (false) once you're done.

Entity():invincible = true

 

Avorion doesn't have the feature that allows to cancel an event.

 

Thank you. But I'm used "onDamage" as example. I'm needed to disable player to repair ship on some sectors or disable damage from some type of damage and etc.

Link to comment
Share on other sites

Well, in theory this could work:

callback onDamaged(objectIndex, amount, inflictor, damageType) - fires before the damage is dealt

 

But this callback:

callback onBlockDamaged(objectIndex, blockIndex, inflictorId, damage, damageType) - fires after damage is dealt, but before block is destroyed.

 

So you can try to heal the ship immediately after taking damage. It may produce an error, because i'm not sure what is "blockIndex" in this context. It may be an index of the block or type index.

function MyModNamespace.initialize()
    Entity():registerCallback("onBlockDamaged", "onBlockDamaged")
end

function MyModNamespace.onBlockDamaged(objectIndex, blockIndex, inflictorId, damage, damageType)
    local entity = Entity(objectIndex)
    entity:heal(damage, blockIndex, vec3(), entity.factionIndex) 
end

Link to comment
Share on other sites

Inserting a quick question here,  your code mean on that the

function MyModNamespace.onBlockDamaged(objectIndex, blockIndex, inflictorId, damage, damageType)
    local entity = Entity(objectIndex)
    entity:heal(damage, blockIndex, vec3(), entity.factionIndex) 
end

overwrite the original callback ?  How do you know (or where) the original content of the function  ?

 

Link to comment
Share on other sites

No, it's not overwriting. There is a difference between predefined functions (such as initialize, update, secure..) and callbacks.

You just need to add predefined function and it will be detected and called by the game.

 

But when it comes to callbacks, you need to register them item via:

Entity():registerCallback(string callbackName, string functionName) 

 

And actually you can use any function name, for example:

function MyModNamespace.initialize()
    Entity():registerCallback("onBlockDamaged", "onSomeBlockSuddenlyDamaged")
end

function MyModNamespace.onSomeBlockSuddenlyDamaged(objectIndex, blockIndex, inflictorId, damage, damageType)
    local entity = Entity(objectIndex)
    entity:heal(damage, blockIndex, vec3(), entity.factionIndex) 
end

Link to comment
Share on other sites

Well, in theory this could work:

callback onDamaged(objectIndex, amount, inflictor, damageType) - fires before the damage is dealt

 

But this callback:

callback onBlockDamaged(objectIndex, blockIndex, inflictorId, damage, damageType) - fires after damage is dealt, but before block is destroyed.

 

So you can try to heal the ship immediately after taking damage. It may produce an error, because i'm not sure what is "blockIndex" in this context. It may be an index of the block or type index.

function MyModNamespace.initialize()
    Entity():registerCallback("onBlockDamaged", "onBlockDamaged")
end

function MyModNamespace.onBlockDamaged(objectIndex, blockIndex, inflictorId, damage, damageType)
    local entity = Entity(objectIndex)
    entity:heal(damage, blockIndex, vec3(), entity.factionIndex) 
end

 

I was try it with onCollision callback. But it is does not worked if damage more than ship health

Link to comment
Share on other sites

Well, if you want to disable some type of damage in certain sectors (collision for example), I think you can use this code. But keep in mind that it's a dark magic and anything could happen :)

Few important moments:

  • You will still see as your health decreases. I guess it's a visual bug because in reality you take no damage. Health bar will return to the normal state in few seconds.
  • While "invincible = true", you take no damage from any weapon. In theory, if you press against an asteroid while enemies shoot at you, you will receive less damage.

 

-- namespace TestMod
TestMod = {}

local collisionCounter = 0

function TestMod.initialize()
    Entity():registerCallback("onDamaged", "onDamaged")
end

function TestMod.onDamaged(objectIndex, amount, inflictor, damageType)
    if damageType == DamageType.Collision then
        Entity().invincible = true
        collisionCounter = collisionCounter + 1 -- each time collision happens we increase counter
        deferredCallback(0, "undoInvincibility") -- and plan to call function that will undo invincibility
    end
end

function TestMod.undoInvincibility()
    -- we need counter to ensure that we undo invincibility after all collisions happened
    collisionCounter = collisionCounter - 1
    if collisionCounter == 0 then
        Entity().invincible = false
    end
end

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...