It prints different value when it shouldn't be different (SOLVED)

Description
I’ve tried to make a simple ability system with works with manager system.
but when i print out the value from 2 different places,each skillname is different.

formation
Manager.lua has a global value that contains a table of each skill_parent object.
skill_parent.lua is a parent of all of skill.

skill_parent has name value and update function and skill_parent.update function which just prints out the name of the object.

I made 3 skill_parent objects with a different name(TEST_SKILL, TEST_SKILL2, TEST_SKILL3) and copied into Manager.lua.

Problem

When I launch the game, firstly Ability.script makes 3 different Skill_parent objects and stores into Manager.lua.
then call update function something like this

--Ability.script
function update(self, dt) 
	for i,skill_parent in ipairs(Manager.SkillTable) do
		print("called from Ability.script :i am "..skill_parent.skill_name .. " and called at number " .. i)
		skill_parent.update(dt,i)
	end
end
--Skill_parent.lua
function instance.update(dt,bn)
	print("called from Skill_parent:i am "..instance.skill_name .. " and called at number " .. bn)
end

but skill_name value is different between Ability.script update and skill_parent update.
DEBUG:SCRIPT: called from Ability.script :i am TEST_SKILL and called at number 1 DEBUG:SCRIPT: called from Ability.script :i am TEST_SKILL2 and called at number 2 DEBUG:SCRIPT: called from Ability.script :i am TEST_SKILL3 and called at number 3 DEBUG:SCRIPT: called from Skill_parent:i am TEST_SKILL3 and called at number 1 DEBUG:SCRIPT: called from Skill_parent:i am TEST_SKILL3 and called at number 2 DEBUG:SCRIPT: called from Skill_parent:i am TEST_SKILL3 and called at number 3

Am I doing wrong? or is this just an another bug?

Version
Defold Editor 2.0 with 64bit windows 10.

By looking at your code, it seems to me that “instance” is the module table of “skill_parent.lua”. That table is the same table for all calling code (effectively a singleton), and if you store data there (e.g. skill_name), it will be overwritten.

Oh really?

it sounds like it works when I make them separately, but i don’t know how to rewrite this…
I never thought singleton is possible in lua.

Additional Information
this is PPrint of Manager.SkillTable.

Sorry, It worked as intended.

All I had to do is change

skill_parent.update(dt,i)

to

skill_parent:update(dt,i)

and then

function instance.update(dt,bn)
	print("called from Skill_parent:i am "..instance.skill_name .. " and called at number " .. bn)
end

to

function instance.update(self,dt,bn)
	print("called from Skill_parent:i am "..self.skill_name .. " and called at number " .. bn)
end

And it all works fine. Sorry for timewasting.

3 Likes