Jump to content

Trying to count prefabs with button


Recommended Posts

Ultimately I want a mod that will allow a user input (either a typed string or a mouse click/hover) to select a prefab and then run the c_countprefabs("prefab") command (or something equivalent) on that.

By hacking together code from some other mods and looking some things up I have something that almost works but doesn't.

My modinfo has only a section defining the key to be pressed which is assignable. The modmain consists of

_G = GLOBAL

local function GetKeyFromConfig(config)
    local key = GetModConfigData(config)
    return key and (type(key) == "number" and key or _G[key])
end

local function InGame()
	return ThePlayer and ThePlayer.HUD and not ThePlayer.HUD:HasInputFocus()
end

--I don't know what this InGame() does but it was part of a mod that I partially copied and I don't know if its okay to remove

_G.TheInput:AddKeyUpHandler(GetKeyFromConfig("pcount"), 
	function ()
		local prefab_str = "tentacle" --prefabstring, at the moment I just pass it explicitly just to try and get something working but eventually I want this to be obtained from some user input
		
--[[		
		local x, y, z = _G.ThePlayer.Transform:GetWorldPosition()
		local tab1 = _G.TheSim:FindEntities(x, y, z, 40, prefab_str, nil, nil)
		local amnt = #tab1
		for k,v in pairs(tab1)
		do
		print(tab1[k])
		end
]] --this was an alternate approach that almost worked. the for loop was to check what the heck was in tab1 since it wasn't what I expected. turns out FindEntities was just finding all the nearby entities instead of the prefab_str one I wanted.
    
    
		local amnt = _G.ThePlayer.consolecommands.c_countprefabs(prefab_str,1) --amount, noprint enabled
        --the game doesn't recognize consolecommands and I don't know enough about how things are set up internally to put the correct path here or if its even possible to run c_countprefabs the way I'm trying to
		
    --local amnt = _G.consolecommands.c_countprefabs(prefab_str,1) --amount, noprint enabled
		
		_G.ThePlayer.components.talker:Say("There are "..tostring(amnt).." "..prefab_str)
end)

Now the alternative approach did get a point where some number was being spat out but it was counting the result of FindEntities and FindEntities was making a table of all nearby entities instead of the ones that match prefab_str which is currently set to a fixed value of tentacle (I want this to be determined by user input eventually). Since FindEntities wasn't working (and since I didn't know which radius was most appropriate) I went back to trying to use c_countprefabs but I don't know how to call it from within a mod correctly. This approach just gives me errors saying that consolecommands isn't a correct reference or table. I tried variations of the path like subbing TheWorld in for ThePlayer.

I don't care which approach gets working but it would useful to know how to call a console command like c_countprefabs because I want to write another mod that does something similar to this but uses c_gonext instead.

Okay I found out that I just need to throw out the whole consolecommands part and just have _G.c_countprefabs() and it will work! That's really exciting so the only other thing I need to figure out is how save the string from user input. Any tips appreciated.

Link to comment
Share on other sites

Why not just copy the code from consolecommands.lua ?

function c_countprefabs(prefab, noprint)
    local count = 0
    for k,v in pairs(Ents) do
        if v.prefab == prefab then
            count = count + 1
        end
    end
    if not noprint then
        print("There are ", count, prefab.."s in the world.")
    end
    return count
end

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.

×
  • Create New...