Jump to content

Attempt to index upvalue 'tracker'


Recommended Posts

Can anyone help me? I'm trying to run this code but for some reason it's not working

I get this error 

[00:01:36]: [string "../mods/Wheeler/scripts/prefabs/wheeler_tra..."]:166: attempt to index upvalue 'tracker' (a nil value)

And this is my function

local function ActivateTracking(inst)
    local owner = inst.components.inventoryitem.owner
	local tracker = inst.components.inventory:GetItemInSlot(1) --I set this
	
    local function update_item()
        local closer_item = TrackNext(inst, inst.tracked_item.prefab)
        if closer_item ~= inst.tracked_item then
            inst.tracked_item = closer_item
        end
    end

    if inst.tracked_item then
		print("Checkpoint1")

        if not inst.arrow then
            inst.arrow = SpawnPrefab("wheeler_tracker_arrow")
            owner:AddChild(inst.arrow)
        end
        
        if inst.arrow_rotation_update == nil then
			print("Checkpoint2")
            inst.arrow_rotation_update = inst:DoPeriodicTask(0, function() 
                if (inst.tracked_item and inst.tracked_item:IsInLimbo()) or not CanGiveLoot(inst.tracked_item, tracker.prefab) then --I use the variable tracker here
					print("Checkpoint3")
                    inst.tracked_item = nil
                end

                if inst.tracked_item == nil or not inst.tracked_item:IsValid() then
					print("Checkpoint4")
                    DeactivateTracking(inst)
                    inst.tracked_item = TrackNext(inst, tracker.prefab ) --And here
                    ActivateTracking(inst)
                else
                    inst.arrow:UpdateRotation(inst.tracked_item.Transform:GetWorldPosition())
                end
            end)
        end

        --[[if inst.distance_update == nil then
            inst.distance_update = inst:DoPeriodicTask(5, function() 
                if inst.tracked_item then
                    update_item()
                end
            end)
        end]]

    else
        owner.components.talker:Say(GetString(ThePlayer.prefab, "ANNOUNCE_NOTHING_FOUND"))
    end
end

I'm not sure what to do, here's the entire prefab file if you'd like to look at it

Thank you for reading!

 

wheeler_tracker.lua

Link to comment
Share on other sites

Hmm, it sounds like an issue with the order of things being called, or that something happens to the item so it is no longer in the slot or something.

If you're completely unsure about how this could happen, save, and then put unique print-statements everywhere. Print which functions and if-statements are entered, what important values are e.g. print("tracker is nil: "..tostring(tracker == nil)). I can almost guarantee you, that something is being called at a time you don't want it to.

Link to comment
Share on other sites

15 minutes ago, Ultroman said:

Hmm, it sounds like an issue with the order of things being called, or that something happens to the item so it is no longer in the slot or something.

If you're completely unsure about how this could happen, save, and then put unique print-statements everywhere. Print which functions and if-statements are entered, what important values are e.g. print("tracker is nil: "..tostring(tracker == nil)). I can almost guarantee you, that something is being called at a time you don't want it to.

Thank you, I will come back to you later if I need more help.

Link to comment
Share on other sites

1 hour ago, Ultroman said:

snip

[00:02:25]: ERROR! SOMEHOW THE ITEM RECEIVED BECAME NIL, PLEASE REPORT	
[00:02:25]: stack traceback:
	../mods/Wheeler/scripts/widgets/specialslot.lua:33 in (method) OnItemGet (Lua) <30-44>
	../mods/Wheeler/scripts/prefabs/wheeler_tracker.lua:200 in (field) fn (Lua) <197-207>
	scripts/scheduler.lua:177 in (method) OnTick (Lua) <155-207>
	scripts/scheduler.lua:371 in (global) RunScheduler (Lua) <369-377>
	scripts/update.lua:170 in () ? (Lua) <149-228>	

Interesting, I found this. This originates from the specialslotwidget.

I'm not sure if this is part of the problem though? I will keep trying.

specialslot.lua

Link to comment
Share on other sites

On 5/11/2019 at 8:46 PM, Ultroman said:

[snip]

Sorry for bothering you again, But could there be other alternatives to writing my code?
 

if inst.arrow_rotation_update == nil then
                inst.arrow_rotation_update = inst:DoPeriodicTask(0, function() 

                    if inst.tracked_item and (inst.tracked_item:IsInLimbo() or not CanGiveLoot(inst.tracked_item, inst.components.inventory:GetItemInSlot(1).prefab)) then
                        inst.tracked_item = nil
                    end

                    if inst.tracked_item == nil or not inst.tracked_item:IsValid() then
                        DeactivateTracking(inst)
                        inst.tracked_item = TrackNext(inst, inst.components.inventory:GetItemInSlot(1).prefab)
                        ActivateTracking(inst)
                    else
                        inst.arrow:UpdateRotation(inst.tracked_item.Transform:GetWorldPosition())
                    end
                end)
            end

Basically, My equippable item has a slot, and the game needs to find out the prefab in the slot. GetItemInSlot clearly isn't working, so i'm wondering if theres possibly another way I could try? Thanks for reading.

Link to comment
Share on other sites

Wait, what? Your item has a slot...on it? In it? I think we need to take two steps back, and you give me the full run-down on what your mod is, which things are in play (character, item, etc.) and exactly what functionality you want, and how you are trying to do it right now. This sounds like a complicated use-case with an issue I can't pin-point without knowing how all of this is tied together. I know it'll probably take some time to write up the details, but I'm thoroughly confused as to what I'm looking at.

Link to comment
Share on other sites

17 hours ago, Ultroman said:

snip

Sorry for not explaining in greater detail, i'm porting Wheeler over from hamlet and i'm attempting to do her Navigadget item, This item has a slot, 1 slot to put items in. Whatever you put in the Navigadget's slot, it will track the closest one in the world and lead you to it. What I need to do here. Is get the name of the prefab IN the slot that the player has put in.

Link to comment
Share on other sites

3 minutes ago, Ultroman said:

Ah, so your prefab has an inventory component on it? And you can open its inventory, which shows you a slot where you can put an item?

Yep, And once you put an item into that slot. A compass like circle will spawn under you pointing to the nearest item depending on whatever you put in the slot.

Link to comment
Share on other sites

I think I see it now. This line in onequip:

owner.HUD.controls.inv:GetSpecialSlot("wheeler_tracker"):OnItemGet(inst.components.inventory:GetItemInSlot(1))

For this line, you do not check whether GetItemInSlot(1) returns nil, so if there is no item in the item-slot, then you are passing nil to OnItemGet().

Just after this you check whether GetItemInSlot(1) is nil here:

	    if inst.components.inventory:GetItemInSlot(1) then
    	    inst.tracked_item = TrackNext(inst, inst.components.inventory:GetItemInSlot(1).prefab)
        	ActivateTracking(inst)
	    end

 

Link to comment
Share on other sites

17 hours ago, Ultroman said:

[snip]

Thank you for everything, I actually redid the entire prefab because the inventory component just wasn't working that well, buggy and it would delete items when put in. So I used the container component and it works wonders! For anyone else that sees this, I don't see any why'd you use the inventory component instead of the container component. Don't make the same mistake as me!

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