Jump to content

[Fixed]Some unreasonable stuff in EntityScript


Littlefat1213
  • Pending
  • AddComponent
function EntityScript:AddComponent(name)
    local lower_name = string.lower(name)
    if self.lower_components_shadow[lower_name] ~= nil then
        print("component "..name.." already exists on entity "..tostring(self).."!"..debugstack_oneline(3))
    end

    local cmp = LoadComponent(name)
	if not cmp then
	    error("component ".. name .. " does not exist!")
	end

    self:ReplicateComponent(name)

    local loadedcmp = cmp(self)
    self.components[name] = loadedcmp
    self.lower_components_shadow[lower_name] = true

    local postinitfns = ModManager:GetPostInitFns("ComponentPostInit", name)

    for i, fn in ipairs(postinitfns) do
        fn(loadedcmp, self)
    end

    self:RegisterComponentActions(name)

	return loadedcmp
end


function EntityScript:RegisterComponentActions(name)
    local id = ACTION_COMPONENT_IDS[name]
    if id ~= nil then
        table.insert(self.actioncomponents, id)
        if self.actionreplica ~= nil then
            self.actionreplica.actioncomponents:set(self.actioncomponents)
        end
    end
    for modname, idmap in pairs(MOD_ACTION_COMPONENT_IDS) do
        id = idmap[name]
        if id ~= nil then
            if self.modactioncomponents == nil then
                self.modactioncomponents = { [modname] = {} }
            elseif self.modactioncomponents[modname] == nil then
                self.modactioncomponents[modname] = {}
            end
            table.insert(self.modactioncomponents[modname], id)
            if self.actionreplica ~= nil then
                self.actionreplica.modactioncomponents[modname]:set(self.modactioncomponents[modname])
            end
        end
    end
end

in some case, when entity is attempted to add reduplicated component, for example AddComponent("slipperyfeet") for twice,  Entityscript:AddComponent so far just print a warning and make it go on and override the existed one. I'm afraid this could cause to RegisterComponentActions again.

 

  • Get/Clear BufferedAction
function EntityScript:GetBufferedAction()
    return self.bufferedaction or (self.components.locomotor ~= nil and self.components.locomotor.bufferedaction) or nil
end

function EntityScript:ClearBufferedAction()
    if self.bufferedaction ~= nil then
        self.bufferedaction:Fail()
        self.bufferedaction = nil
    end
end

Get function consider the locomotor.bufferedaction, but Clear function not yet


Steps to Reproduce

As Above




User Feedback


I'm not sure if this should be in the beta forum, though I guess most eyes will be here.

And Yeah I'm pretty sure this does result in:

table.insert(self.modactioncomponents[modname], id)

Getting inserted twice for those cases. Everything else should be accounted for though.

I don't think it's a good idea to not assert/error on adding a component more than once? It could lead to some really weird behavior in some cases. At the very least, a return statement should be added right after printing the error maybe if assert/error is too much?

 

I also feel like AddComponent should support sending a different name for the require() call. This would allow fully replacing some vanilla components without having to do some hacks like manually removing a component, adding the new one with a different name and file, and then manually correcting the names for inst.components and inst.lower_components_shadow (which only works with components with no replicas or component actions, which is very limited).

  • Like 1

Share this comment


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...