Ttiki Posted July 28, 2023 Share Posted July 28, 2023 Hi everyone, I'm currently encountering an issue with a Pick operation in my Lua code and I'm hoping someone can help me figure out what I'm missing. Here's the context: I have a player controller, which is essentially a pivot that creates a light and a camera. The player controller doesn't involve any movement, the mouse position on the screen simply influences the camera rotation. I also have an enemy that randomly spawns and passes in front of the light, down a corridor. I want to check two things - if the light is on, and if the enemy crosses the light's path. If both conditions are met, I want to call a method from the player script to the enemy script. Here's the relevant part of my player controller code which handles the light being on and the Pick operation: function Script:UpdateWorld() [...] if not self.flashlight:Hidden() then local pickInfo = PickInfo() local i**** = self.entity.world:Pick( self.entity:GetPosition(), self.lightRayEnd:GetPosition(), pickInfo, .25, false, Collision.Character ) if i**** and pickInfo.entity:GetKeyValue("classname") == "npc_nurse" then pickInfo.entity.script:SetPlayerIsShiningLight() end end end The flashlight check works as expected. However, I'm having trouble with the Pick method. It seems to correctly pick up the enemy when it crosses the ray's path. But it also appears to pick up another entity. I've simplified my map to include only a floor for the enemy to walk on and the basic set-up for the player controller. I've confirmed that the KeyValue is being correctly set in the enemy script (in the Start method), and that the collision is set to Character. Through debugging (I've removed all the console debug statements from the code above for clarity), I found that when I test for the pick position, it returns 0,0,0. To make sure I wasn't missing something in my calculations, I created another pivot to mark the end of the raycast (self.lightRayEnd). Does anyone have any idea what I could be doing wrong, or what might be causing this issue? I appreciate any input or suggestions. Thanks! Just making games for fun. Currently working on T3-L4 Link to comment Share on other sites More sharing options...
Yue Posted July 29, 2023 Share Posted July 29, 2023 What is the origin of the lightning?, a mesh? Edit: pickInfo.entity:Hide() --<<< Test. 1 Link to comment Share on other sites More sharing options...
Ttiki Posted July 29, 2023 Author Share Posted July 29, 2023 3 hours ago, Yue said: What is the origin of the lightning?, a mesh? Edit: pickInfo.entity:Hide() --<<< Test. The origin of the lightning is a spotlight I create inside the player controller script at the Start method. And thanks, I did not think of testing to hide the picked entity... I will try that... Just making games for fun. Currently working on T3-L4 Link to comment Share on other sites More sharing options...
Ttiki Posted July 29, 2023 Author Share Posted July 29, 2023 Ok, so after some more testing, I'm left even more confused. I know it's me again being stupid and missing something obvious, but I can't find the problem. When trying to hide the picked entity, it got hidden, but I couldn't see any changement. I thought it could have been a problem with some of my point entities. So I gave all my entities a KeyValue and logged which entity was being caught, but even then, the picked entity returned a KeyValue of nothing (" "). As I said, I'm on a map with just simple brush for the floor for the entity to walk on, the enemy, and my pivot which handles the player controller part by creating the light and the camera and which is used to detect if the enemy is passing through the light. I thought it could have been with the model of my enemy which has lots of children and different materials. So I deleted the model and attached my enemy script to a simple cylinder brush with Character collision and characters controller as physics mode. Each entity in the world, each brush have a simple script attached to it to set a key value for classname, which I log into the console in the Pick test. And now, the Pick entity is not even picking anything from the scene. I tried removing the Collision.Character and set a radius, nothing worked. I logged every position of the Pick (Pick start point and Pick end Point as well as the enemy position in the world, but still nothing. The enemy passes through the raycast, the coordinates tell me it should be picked up by the Pick method. Just making games for fun. Currently working on T3-L4 Link to comment Share on other sites More sharing options...
Marcousik Posted July 29, 2023 Share Posted July 29, 2023 Hi 2 ideas to try: 1) 14 hours ago, Ttiki said: self.lightRayEnd:GetPosition() Check if this is not a child, if it is, it should be GetPosition(true) 2) I got an issue with pick() returning entities not precisely enough Try to replace the false with true in the Pick() arguments https://www.leadwerks.com/learn?page=API-Reference_Object_Entity_Pick it's about the "closest" arguments Also maybe use [Name-of-your-created-world].Pick(....) 14 hours ago, Ttiki said: local i**** = self.entity.world:Pick( self.entity:GetPosition(), self.lightRayEnd:GetPosition(), pickInfo, .25, true, Collision.Character ) Link to comment Share on other sites More sharing options...
Ttiki Posted July 29, 2023 Author Share Posted July 29, 2023 11 minutes ago, Marcousik said: Hi 2 ideas to try: self.lightRayEnd:GetPosition() 1) Check if this is not a child, if it is, it should be GetPosition(true) No, it's not a child of anything. It a simple Pivot I've placed to avoid any confusion with the end point (i.e I'm sure what is the endpoint of the ray). 13 minutes ago, Marcousik said: 2) I got an issue with pick() returning entities not precisely enough Try to replace the false with true in the Pick() arguments Yeah, i've tried that as well with the new I've just made, no concluent changes happen. Quote Also maybe use [Name-of-your-created-world].Pick(....) Do you mean something like World:GetCurrent():Pick() ? Just making games for fun. Currently working on T3-L4 Link to comment Share on other sites More sharing options...
Ttiki Posted July 29, 2023 Author Share Posted July 29, 2023 I can create an empty project with everything you need to test on your end if you want / have the time? 1 Just making games for fun. Currently working on T3-L4 Link to comment Share on other sites More sharing options...
Marcousik Posted July 29, 2023 Share Posted July 29, 2023 yea upload is good 1 Link to comment Share on other sites More sharing options...
Ttiki Posted July 29, 2023 Author Share Posted July 29, 2023 There are two maps included: the first one, n1, represents the normal gameplay level or the first level. To reduce file size, I've removed most models from this map. It features a hospital bed with a pivot point to which the PlayerController.lua script is attached. This is where the Pick event occurs. Please bear in mind that the code might not be optimal as it's still under heavy development, and I haven't started optimizing it yet. At both ends of the corridor, you'll find two empty pivots named HallwayWaypointA and HallwayWaypointB. Just below, there's a black box containing the monster with the Nurse.lua script attached. Essentially, her script starts a random counter, and when the counter hits a certain value, she will spawn randomly at one of the two points in the hallway and then proceed to move to the other point. The test inside the PlayerController.lua script is to check if the player has their flashlight on and whether the nurse passes through the raycast. There's another box outside the map with a simple FPSController. When you try the game, you'll find that you can only move the camera. Press 'F' to turn the flashlight on/off, and 'q' to enter sleep mode, which enables the FPSController. Press 'q' again to return to the default game mode. The second map is a stripped-down version of the level without the outside box and without any brushes or models except for the floor. This map is better suited for testing purposes since it contains only the essential entities and brushes. Thank you very much for your help! If you have any questions, feel free to ask. I'm here to assist you! 2132315131_T3-L4-Dev.zip Just making games for fun. Currently working on T3-L4 Link to comment Share on other sites More sharing options...
Yue Posted July 29, 2023 Share Posted July 29, 2023 GetKeyValue("name"); Link to comment Share on other sites More sharing options...
Ttiki Posted July 30, 2023 Author Share Posted July 30, 2023 12 hours ago, Yue said: GetKeyValue("name"); Yeah, I already tried that. but the returned string was empty. Just making games for fun. Currently working on T3-L4 Link to comment Share on other sites More sharing options...
Solution Marcousik Posted July 30, 2023 Solution Share Posted July 30, 2023 Ok I got it to work. I made the test only on your DEVROOM_gameplay.map I found 2 problems: 1) In script Nurse.lua: Remove this line in start() - this causes the bug Quote self.entity:SetPickMode(Entity.BoxPick,true) 2) Your nurse Entity is too little ! It runs under the raycast you are testing and so doesn't trigger anything. -> Just put a scale on the nurse entity (1,10,1) After that the nurse is going to be detected and is coming to the camera, I also reduced the murse move speed to 1 or 2 1 1 Link to comment Share on other sites More sharing options...
Ttiki Posted July 30, 2023 Author Share Posted July 30, 2023 Thank you so much. I didn't think self.entity:SetPickMode(Entity.BoxPick,true) self.entity:SetPickMode(Entity.BoxPick,true) would be the problem. Might have to check the documentation more thoroughly. As well as for the scale. Thank you very very much ! Have a nice day. Just making games for fun. Currently working on T3-L4 Link to comment Share on other sites More sharing options...
Recommended Posts