Identify components dynamically?

Ask for help about creating mods and scripts for Grimrock 2 or share your tips, scripts, tools and assets with other modders here. Warning: forum contains spoilers!
Post Reply
User avatar
jxjxjf
Posts: 33
Joined: Fri Oct 24, 2014 6:26 am

Identify components dynamically?

Post 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. :)
Batty
Posts: 509
Joined: Sun Apr 15, 2012 7:04 pm

Re: Identify components dynamically?

Post 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.
User avatar
QuintinStone
Posts: 72
Joined: Sat Nov 01, 2014 9:58 pm

Re: Identify components dynamically?

Post 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
Crypt of Zulfar
minmay
Posts: 2789
Joined: Mon Sep 23, 2013 2:24 am

Re: Identify components dynamically?

Post by minmay »

that will only work on components with the default name though
Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.
Batty
Posts: 509
Joined: Sun Apr 15, 2012 7:04 pm

Re: Identify components dynamically?

Post by Batty »

Hey, look at that, the dirty work has been done already, nice Quintin :)
User avatar
jxjxjf
Posts: 33
Joined: Fri Oct 24, 2014 6:26 am

Re: Identify components dynamically?

Post 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:
User avatar
QuintinStone
Posts: 72
Joined: Sat Nov 01, 2014 9:58 pm

Re: Identify components dynamically?

Post by QuintinStone »

Well, I copy-pasted from the script reference and let regex do the work. ;)
Crypt of Zulfar
GoldenShadowGS
Posts: 168
Joined: Thu Oct 30, 2014 1:56 am

Re: Identify components dynamically?

Post by GoldenShadowGS »

This thread solved my problem. Now I know how to filter entities based on the components they have.
Post Reply