MakeSureToKnock Posted September 30, 2024 Share Posted September 30, 2024 So, I'm trying to add an object to the game that spawns randomly in the world at the start of each cycle. Fortunatly for me there are pre-existing functions for both getting a random world location that is on land and spawning a prefab. Unfortunatly, it's doing some wack ****. Below is the code I'm using, for testing purposes I'm using marble pillars for my prefab. I also added this component to the forest.lua prefab, which is where similar spawners are found. function self:SpawnPlz() print("spawning") local pt = TheWorld.Map:FindRandomPointOnLand(50) print(pt.x, pt.y, pt.z) SpawnPrefab("marblepillar").Transform:SetPosition(pt.x, 0, pt.y) print("spawned") end Now for whats going wrong. For some god foresaken reason, when this is ran at the begining of each cycle, it's spawning TWO instances of the prefab, but one it weird. One of the instances is normal, it can be inspected, it can be mined, life is good. However, that normal instance spawns in a completely random location that has nothing to do with the coordinates given to the function. It can even spawn in the ocean. The other instance is not normal. It cannot be inspected nor mined, but has collosion, the pillar graphic, and spawns at the coordinates given to the function. The things I am positive of are: There are no other instances spawning this prefab. The coordinates are accurate. There is only one instance of the prefab because the ingame c_gonext() command only takes me to the one that can be inspected. I looked around in the code are there are instance of the SpawnPrefab line being using in conjuntion with the Transform:SetPosition and without that Transform the thing doesn't even spawn at all. I did notice that a lot of the code using it using GetWorldPosition() in some fashion but as far as I can tell the coodinates returned use the same vector grid. My guess is that the return coordinates are not the same, as FindRandomPointOnLand returns Vector3(x, y, z), but that wouldn't explain why it's spawning a dud at the right location. Potentially, I can use the Physics:Teleport to get the prefab in the correct location but, eeeeh. Obviously, I'll mess around some more and see if I can get something that actually works right but until then any insight from yall peeps would be amazing. Thanks in advanced! Link to comment https://forums.kleientertainment.com/forums/topic/160021-spawnprefab-function-spawning-two-sepertate-visuals-in-two-different-locations/ Share on other sites More sharing options...
MakeSureToKnock Posted October 1, 2024 Author Share Posted October 1, 2024 (edited) So I have discovered that any value that I use for SpawnPrefab("marblepillar").Transform:SetPosition(x, y, z) that is the result of a function, even something like math.random() the stuff above happens. The only case I have found where this works as predicted is when I do something like this: SpawnPrefab("marblepillar").Transform:SetPosition(500, 0, 500). I have no idea why. EDIT: The reason it's messing up is because I added it to the forest.lua prefab, works in a component attacted to my character. Still don't know why. Edited October 1, 2024 by MakeSureToKnock Link to comment https://forums.kleientertainment.com/forums/topic/160021-spawnprefab-function-spawning-two-sepertate-visuals-in-two-different-locations/#findComment-1751391 Share on other sites More sharing options...
MakeSureToKnock Posted October 1, 2024 Author Share Posted October 1, 2024 That's a mistake in this post and not in my actual code. The FindRandomPoint functions always return y = 0, so I would have noticed that consistancy between tests. For now, however, I've just moved this component to a different prefab and it works fine. I kinda bothers me that my spawn component isn't added to the forest prefab with every other spawn component but for now what I have is enough. If I ever find out why this was happening I'll make sure to post it here. Link to comment https://forums.kleientertainment.com/forums/topic/160021-spawnprefab-function-spawning-two-sepertate-visuals-in-two-different-locations/#findComment-1751441 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