liuheniw Posted March 20, 2024 Share Posted March 20, 2024 AddClassPostConstruct("widgets/hoverer",function(self) local old_SetString = self.text.SetString self.text.SetString = function(text,str) local target = GLOBAL.TheInput:GetWorldEntityUnderMouse() if target and target.components then print("+++++++++++++++++++++++++++++"..target.prefab.."+++++++++++++++++++++++++++++") for name,component in pairs(target.components) do count = count + 1 print("=====No."..count.."=====") print("name:"..name) -- print("component:"..tostring(component)) end end return old_SetString(text,str) end end) As shown above, I attempted to modify the setString method of the widgets/hoverer in modmain.lua to dynamically retrieve some text description information. However, the target obtained through GLOBAL.TheInput:GetWorldEntityUnderMouse() fails to retrieve its all components. I tried studying the code of a mod named 'insight', which successfully retrieves the "target" using the same method in hoverer.lua and then parses the "target" in modmain.lua. I modified his code by adding print logs and found that it can print all components of the "target". However, when I copied his code into my own project, I couldn't achieve the same result. Is there anyone kind enough to help me? Link to comment https://forums.kleientertainment.com/forums/topic/155109-helpim-having-trouble-with-theinputgetworldentityundermouse/ Share on other sites More sharing options...
_zwb Posted March 21, 2024 Share Posted March 21, 2024 The hoverer widget is running on client side, you don't have access to all components on the client side. Link to comment https://forums.kleientertainment.com/forums/topic/155109-helpim-having-trouble-with-theinputgetworldentityundermouse/#findComment-1706551 Share on other sites More sharing options...
liuheniw Posted March 21, 2024 Author Share Posted March 21, 2024 7 minutes ago, _zwb said: 悬停小部件在客户端运行,您无权访问客户端的所有组件。 Thank you for your response. I have already resolved this issue in my own way, although it may be a bit sneaky. The code is as follows: --支持的随从列表 GLOBAL.followers_enum = { ["tallbird"] = "高脚鸟", ["lightninggoat"] = "伏特羊", ["powder_monkey"] = "火药猴", ["warg"] = "座狼", } --鼠标悬浮在物品上显示信息(仅地面) AddClassPostConstruct("widgets/hoverer",function(self) local old_SetString = self.text.SetString self.text.SetString = function(text,str) local target = GLOBAL.TheInput:GetHUDEntityUnderMouse() if target ~= nil then target = target.widget ~= nil and target.widget.parent ~= nil and target.widget.parent.item else target = GLOBAL.TheInput:GetWorldEntityUnderMouse() end --------随从信息显示-------- local followers_enum = GLOBAL.followers_enum --仅处理拴绳信息 if target and target.prefab == "leash" then for follower, des in pairs(followers_enum) do if target:HasTag("leash_"..follower) then str = str .. "\n当前随从:" .. des end end end --------随从信息显示-------- return old_SetString(text,str) end end) ------------------------------------显示物品栏拴绳信息--------------------------------------- -- 导入游戏中的库 local ItemTile = GLOBAL.require "widgets/itemtile" local OldGDS = ItemTile.GetDescriptionString --修改方法定义,将随从信息塞进去 function ItemTile:GetDescriptionString() -- 获取原来的描述字符串 local oldstr = OldGDS(self) local str = "" local entity = GLOBAL.Ents[self.item.GUID] --此方法可以获取当前鼠标所选目标的entity local followers_enum = GLOBAL.followers_enum --仅处理拴绳信息 if entity and entity.prefab == "leash" then for follower, des in pairs(followers_enum) do if entity:HasTag("leash_"..follower) then str = "\n当前随从:"..des end end end --这段方法用来将随从信息插入到名字的下一行 local new_string = "" local split_index = string.find(oldstr, "\n") if split_index then local before_split = string.sub(oldstr, 1, split_index - 1) local after_split = string.sub(oldstr, split_index + 1) new_string = before_split .. str .. "\n" .. after_split else new_string = oldstr .. str end return new_string end ------------------------------------显示物品栏拴绳信息--------------------------------------- I discovered that although this 'target' cannot pass 'components', it can pass 'tags'. Therefore, I used 'tags' to transmit my information. Additionally, since the method for displaying item information for 'prefabs' on the ground and in the inventory seemed different, I made modifications in two places to fulfill my requirements. Once again, thank you for your response. Link to comment https://forums.kleientertainment.com/forums/topic/155109-helpim-having-trouble-with-theinputgetworldentityundermouse/#findComment-1706553 Share on other sites More sharing options...
_zwb Posted March 21, 2024 Share Posted March 21, 2024 7 hours ago, liuheniw said: I discovered that although this 'target' cannot pass 'components', it can pass 'tags'. Therefore, I used 'tags' to transmit my information. 嗯,标签在主客机是同步的,这样确实省事 Link to comment https://forums.kleientertainment.com/forums/topic/155109-helpim-having-trouble-with-theinputgetworldentityundermouse/#findComment-1706636 Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now