Jump to content

[Solved] Find touchstone by id


Recommended Posts

Hey guys

I hope somebody can push me into the right dircection :)

 

I am trying to do stuff with touchstones but I dont seem to be able to "find" the touchstone-object when I only have its id.

I found that touchstonetracker.lua is keeping track for every touchstone a character used and saves its id. But with that id I cant find the actual entity itself without iterating through every element in the world.

 

Is there an index for touchstones that I havent found yet? Or is there another way to find touchstones with their id? (unfortunately the global Ents-table is no help since I touchstonetracker.lua only saves the id but not the GUID of touchstones)

 

If anybody knows other ways or has ideas it would help me a lot :)

Thanks in advance!

 

Greetings from Vienna

Jodli

Edited by Jodlio
Marked title as solved
Link to comment
Share on other sites

local function GetTouchStoneByID(stoneID)
	for k, v in pairs(Ents) do
		if v.GetTouchStoneID and (v:GetTouchStoneID() == stoneID) then
			return v
		end
	end
end

Then save the result somewhere so you don't have to iterate over Ents repeatedly.

Link to comment
Share on other sites

Thanks for your answer!

I was also thinking of a solution like this but that would mean, that my mod would have to iterate through every entity in the world every time I need different touchstone.

I was hoping there would be another less resource-demaning solution :/

If there is no other way I might just do this iteration once for all touchstones when the server starts to just create my own index of touchstones, which would mean an increased startup-time - but at least no cost at running time.

Link to comment
Share on other sites

9 hours ago, Jodlio said:

Thanks for your answer!

I was also thinking of a solution like this but that would mean, that my mod would have to iterate through every entity in the world every time I need different touchstone.

I was hoping there would be another less resource-demaning solution :/

If there is no other way I might just do this iteration once for all touchstones when the server starts to just create my own index of touchstones, which would mean an increased startup-time - but at least no cost at running time.

yes, iterating once at gamestart and saving all the touchstones would be enough. And iterating one time should not increase starting time that much.

Link to comment
Share on other sites

19 hours ago, Jodlio said:

If there is no other way I might just do this iteration once for all touchstones when the server starts to just create my own index of touchstones, which would mean an increased startup-time - but at least no cost at running time.

Looping through Ents is fine.

The c_gonext console command does it and the server doesn't crash.

You are doing it once at startup, nobody notices.

 

If you want a solution that never loops through Ents:

local stones = {}

-- if the stone is removed, delete it from table
local function OnStoneBygone(inst)
	stones[inst.jodmodremovalkey] = nil
end

local function PostOnInit(inst)
	-- get the id
	local id = inst:GetTouchStoneID()
	-- save the stone by ID
	stones[id] = inst
	-- save the ID just in case
	inst.jodmodremovalkey = id
	-- this function runs in rare cases where the stone is removed
	inst:ListenForEvent("onremove", OnStoneBygone)
end

-- run the function AFTER OnInit runs (1 > 0)
-- so that the ID is setup and you can use it
local function StonePost(inst)
	inst:DoTaskInTime(1, PostOnInit)
end

AddPrefabPostInit("resurrectionstone", StonePost)



local function GetTouchStoneByID(stoneID)
	return stones[stoneID]
end

-- access the stones table through player entities
local function StoneAccess(inst)
	inst.GetTouchStoneByID = GetTouchStoneByID
end

AddPlayerPostInit(StoneAccess)

 

Link to comment
Share on other sites

Thank you very much guys!

I tried using a "big" loop for my own touchstone-index and you guys were right. It is not as nearly as bad as I was scared it would be^^

 

Also DarkXero, your other approach looks very good too. Even if I wont use it know (not sure yet) I am sure I can use it on another occasion.

So thank you very much. That helped me a lot!

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