Page 1 of 1

Identify components dynamically?

Posted: Sat Nov 15, 2014 1:05 am
by jxjxjf
Sorry to ask this again, but I think I was too specific last time, so I want to ask a much broader question:

Does anyone know how to identify what components an entity holds dynamically? So, for example, to iterate through all of the entities in a map, and then for each entity to get another table with all of the components it holds, and then identify what kind of component it is?

I see GameObject allows retrieving a component by name or to remove all components, but I can't seem to figure out how to identify components without knowing the component name. 99% of the time the name is the class name, but that's not always true. So, does anyone know how this could be done?

Thanks. :)

Re: Identify components dynamically?

Posted: Sat Nov 15, 2014 1:41 am
by Batty
I did this:

Code: Select all

if torch_holder_4:getComponent("clickable") then 
   print("true") else
   print("false")
end

if torch_holder_4:getComponent("platform") then
   print("true") else
   print("false")
end
and I got true, false on the console as expected. So, you could make a table of all the component names then iterate through it for each object checking for the presence of each component. Lua is very good at iterating so it should go fast. :)

I know that's tedious :twisted: so instead, while you're creating the components dynamically, you could store the names of what you create in a table by object id then easily retrieve it when needed.

Re: Identify components dynamically?

Posted: Sat Nov 15, 2014 3:29 am
by QuintinStone
This is part of a script I'm working on:

Code: Select all

componentlist = {"ammoitem", "animation", "beaconfurnacecontroller", "blast", "blindedmonster", "bombitem", "bossfight", "brain", "burningmonster", "button", "camera", "camerashake", "castspell", "chest", "clickable", "cloudspell", "containeritem", "controller", "counter", "crabbrain", "craftpotion", "crowcontroller", "crowernattack", "crystal", "crystalsharditem", "diggingtool", "door", "dynamicobstacle", "earthquake", "entangledmonster", "equipmentitem", "exit", "eyctopusbrain", "firearmattack", "fireelementalbrain", "floortrigger", "fogparams", "fogparticles", "forcefield", "frozenmonster", "goromorgbrain", "goromorgshield", "gravity", "health", "heightmap", "herderbigbrain", "herdersmallbrain", "iceguardianbrain", "icelizardbrain", "iceshards", "itemaction", "item", "itemconstrainbox", "ladder", "lensflare", "lever", "light", "lindwormbrain", "lindwormcharge", "lindwormfly", "lock", "magmagolembrain", "mapgraphics", "mapmarker", "meleeattack", "meleebrain", "mimiccameraanimation", "model", "monsteraction", "monsterattack", "monsterchangealtitude", "monstercharge", "monster", "monsterdropitem", "monstergroup", "monsterjump", "monsterknockback", "monsterlightculler", "monstermoveattack", "monstermove", "monsteroperatedevice", "monsterpickupitem", "monsterstealweapon", "monsterturn", "monsterwarp", "mosquitoswarmbrain", "obstacle", "occluder", "ogrebrain", "particle", "party", "pit", "platform", "poisoncloudattack", "poisonedmonster", "portal", "projectilecollider", "projectile", "projectileimpact", "pullchain", "pushableblock", "pushableblockfloor", "rangedattack", "rangedbrain", "ratlingbossbrain", "reloadfirearm", "ropetool", "runepanel", "script", "scriptcontroller", "scrollitem", "secret", "skeletonarcherbrain", "skeletoncommanderbrain", "sky", "sleepingmonster", "slimebrain", "smallfishcontroller", "socket", "sound", "spawner", "spellscrollitem", "stairs", "statistics", "stonephilosophercontroller", "stunnedmonster", "surface", "swarmbrain", "teleporter", "tentaclebrain", "tentaclehide", "thornwall", "throwattack", "tiledamager", "timer", "tinycrittercontroller", "toadbrain", "torchholdercontroller", "torchitem", "tricksterbrain", "turtlebrain", "twigrootbrain", "uggardianbrain", "uggardianflames", "usableitem", "viperrootbrain", "walltext", "walltrigger", "wargbrain", "watersurface", "watersurfacemesh", "wizardbrain", "xeloroidbrain", "zarchtonbrain "}

function getAllObjectData(obj)
	local text = ""
	local component = nil
	for _, compname in ipairs(componentlist) do
		component = obj:getComponent(compname)
		if component ~= nil then
			text = obj.name .. " Component " .. compname .. " [" .. type(component) .. "]\r\n"
			local scroll = spawn("scroll",party.level, party.x, party.y, party.elevation, party.facing)
			if compname == "monster" then
				text = text .. printMonsterComponent(component, 0)
			elseif compname == "model" then
				text = text .. printModelComponent(component, 0)
			elseif compname == "animation" then
				text = text .. printAnimationComponent(component, 0)
			elseif compname == "brain" then
				text = text .. printBrainComponent(component, 0)
			elseif compname == "light" then
				text = text .. printLightComponent(component, 0)
			else
			end
			scroll.scrollitem:setScrollText(text)
			scroll.scrollitem:setTextAlignment("left")
		end
	end
end

Re: Identify components dynamically?

Posted: Sat Nov 15, 2014 3:35 am
by minmay
that will only work on components with the default name though

Re: Identify components dynamically?

Posted: Sat Nov 15, 2014 3:42 am
by Batty
Hey, look at that, the dirty work has been done already, nice Quintin :)

Re: Identify components dynamically?

Posted: Sat Nov 15, 2014 5:24 am
by jxjxjf
I thought it would have to be something like this. :shock: Was hoping there was some kind of id attached for when the component name didn't match the class, but thanks Quintin for doing so much of the legwork already! :lol:

Re: Identify components dynamically?

Posted: Sat Nov 15, 2014 6:33 am
by QuintinStone
Well, I copy-pasted from the script reference and let regex do the work. ;)

Re: Identify components dynamically?

Posted: Sun Nov 30, 2014 4:58 am
by GoldenShadowGS
This thread solved my problem. Now I know how to filter entities based on the components they have.