Jump to content

variable 'OnTurnOn' is not declared


Recommended Posts

Alright, so I've been working on a character, and I'm working on implimenting a custom prototyper into this character. I've got most of the issues ironed out so far, such as the tech tree, recipe tab, animation, but I run into a problem when I use the custom prototyper to prototype an item. I go to prototype the item, which it does actually make, and give to me, but when the Onactivate function finishes what it is doing, it tries to run the OnTurnOn function, which itself is giving me this error:

[00:01:17]: [string "../mods/Max Robat/scripts/prefabs/cavemachi..."]:42: variable 'OnTurnOn' is not declared

The parts I think might help solve this are as follows:

	    local function doneact(inst)
        inst._activetask = nil
        if not inst:HasTag("burnt") then
            if inst.components.prototyper.on then
                OnTurnOn(inst)
            else
                OnTurnOff(inst)
            end
        end
    end

local function OnTurnOn(inst)
	if inst.AnimState:IsCurrentAnimation("proximity_loop") then
		--NOTE: push again even if already playing, in case an idle was also pushed
		inst.AnimState:PushAnimation("proximity_loop", true)
	else
		inst.AnimState:PlayAnimation("proximity_loop", true)
	end
	if not inst.SoundEmitter:PlayingSound("idlesound") then
		inst.SoundEmitter:PlaySound("dontstarve/common/ancienttable_LP", "idlesound")
	end
end
 
local function OnTurnOff(inst)
	inst.AnimState:PushAnimation("idle_full")
end

    local function onactivate(inst)
        if not inst:HasTag("burnt") then
            inst.AnimState:PlayAnimation("use")
            inst.AnimState:PushAnimation("idle_full", false)
            if not inst.SoundEmitter:PlayingSound("sound") then
                inst.SoundEmitter:PlaySound("dontstarve/common/ancienttable_craft", "sound")
            end
            inst._activecount = inst._activecount + 1
            inst:DoTaskInTime(1.5, doonact)
            if inst._activetask ~= nil then
                inst._activetask:Cancel()
            end
            inst._activetask = inst:DoTaskInTime(inst.AnimState:GetCurrentAnimationLength() + 2 * FRAMES, doneact)
        end
    end

And this is in the master:

	inst:AddComponent("prototyper")
	inst.components.prototyper.trees = TUNING.PROTOTYPER_TREES.MAXTAB_ONE
	inst.components.prototyper.onturnon = OnTurnOn
	inst.components.prototyper.onturnoff = OnTurnOff
	inst.components.prototyper.onactivate = onactivate

I'm really not sure what I'm missing. Any help would be greatly appreciated

Link to comment
Share on other sites

All of the code I posted was from the prototyper prefab I'm working on. I assume the part I'm using incorrectly is these:

inst.components.prototyper.onturnon = OnTurnOn
	inst.components.prototyper.onturnoff = OnTurnOff
	inst.components.prototyper.onactivate = onactivate

which when I remove them, the machine does prototype the item, no errors, but the station itself doesn't push an animation anymore. I'm not sure what I need to adjust to do so. Thanks again for the assistance.

Link to comment
Share on other sites

  • Developer

Maybe I misunderstood.

From what I understand the line

inst.components.prototyper.onturnoff = OnTurnOff

is in a different file than the function OnTurnOff itself. Because OnTurnOff is defined as a local function, that line would not be able to 'see' the function ("local" tells the lua engine that it's not visible outside the file it's defined in).

You have to make the function either not local, or make another way to access it from outside that file, or move it the file that uses it. Without the complete code it's hard to tell you what the best approach would be.

 

 

Link to comment
Share on other sites

Alright cool. That actually helped me solve the issue. What i was specifically doing was making a new prototyper for a character. The prototyper itself is almost identical to the science machine, but part of it was just giving me trouble. I removed "local" from the OnTurnOff and OnTurnOn functions. after that, the animations were working, and no crashes or other errors from that. Thank you so much. Here is the prefab after I fixed the issues, just for reference.

cavemachine.lua

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
 Share

×
  • Create New...