Bjvb09 Posted June 15, 2019 Share Posted June 15, 2019 Hi! I'm new in this of modding. Right now I'm trying to do something very simple but still, I get stuck.I have this code: function SpawnCreature(inst) print(inst.Transform:GetWorldPosition()) local beefalo = GLOBAL.SpawnPrefab("beefalo") print(beefalo.Transform:GetWorldPosition()) print(beefalo) beefalo.Transform:SetPosition(40.00,0.00,236.48) print(beefalo.Transform:GetWorldPosition()) end AddPlayerPostInit(SpawnCreature) This function tries to spawn a Beefalo at the player's position. Now the problem, until I now inst is the player, but GetWorldPosition() always return 0,0,0. This is what is print in the client_log.txt: [00:00:25]: 0 0 0 [00:00:25]: 0 0 0 [00:00:25]: 114161 - beefalo [00:00:25]: 40 0 236.47999572754 [00:00:25]: Spawning player at: [Load] (33.78, 0.00, 249.78) I'm able to spawn the beefalo if I set the position with the Transform:SetPosition, but I can't get the position of the player correctly. Also, If I try to get the position of the beefalo before I set it manually, it shows me 0,0,0 too. What am I doing wrong? Link to comment Share on other sites More sharing options...
Fuzzy_Waffle Posted June 16, 2019 Share Posted June 16, 2019 What input are you feeding your function when you run it? Try adding another print line in your code to see what it thinks inst actually is just prior to the GetWorldPosition(). Link to comment Share on other sites More sharing options...
Bjvb09 Posted June 16, 2019 Author Share Posted June 16, 2019 (edited) 2 hours ago, Fuzzy_Waffle said: What input are you feeding your function when you run it? Try adding another print line in your code to see what it thinks inst actually is just prior to the GetWorldPosition(). Thanks for replying, I added a new print line (This is all the code that I have in my modmain.lua): --[NEW] This function spawns a 'Beefalo' at the player's position. The player parameter is a reference to the player in game. function SpawnCreature(inst) print(inst) -- New Line Added print(inst.Transform:GetWorldPosition()) local beefalo = GLOBAL.SpawnPrefab("beefalo") print(beefalo.Transform:GetWorldPosition()) print(beefalo) beefalo.Transform:SetPosition(40.00,0.00,236.48) print(beefalo.Transform:GetWorldPosition()) end AddPlayerPostInit(SpawnCreature) The input of the function, SpawnCreature, is supposed to be the player. This is from modutil.lua from the code of the game: -- An illustrative example of how to use a global prefab post init, in this case, we're making a player prefab post init. env.AddPlayerPostInit = function(fn) env.AddPrefabPostInitAny( function(inst) if inst and inst:HasTag("player") then fn(inst) end end) end And this is the function that I'm using for spawn the beefalo after a player spawn. This is the output in client_log.txt with the new line: [00:01:24]: 114101 - webber [00:01:24]: 0 0 0 [00:01:24]: 0 0 0 [00:01:24]: 114104 - beefalo [00:01:24]: 40 0 236.47999572754 [00:01:24]: Spawning player at: [Load] (33.78, 0.00, 249.78) When I print inst, it really contains the player that I'm using. In this case, Webber. I also print all the values of the inst table and the Transform value is there, but I can't print it because it says that is a userdata value and gives me an error. Edited June 16, 2019 by Bjvb09 Link to comment Share on other sites More sharing options...
Ultroman Posted June 25, 2019 Share Posted June 25, 2019 I believe this is because while the server is being set up, during which the mods are being loaded, everything is spawned at position 0,0,0, and is then finally moved when everything is done loading or during the first frame or something like that. I know that whatever thing you spawn is always spawned at 0,0,0, and then you have to manually set the position on the Transform, like you're doing. All you need to do, really, is delay the spawning of the beefalo until the player has a position, which will be on the first frame: function SpawnCreature(inst) inst:DoTaskInTime(0, function(inst) print(inst.Transform:GetWorldPosition()) local beefalo = GLOBAL.SpawnPrefab("beefalo") print(beefalo.Transform:GetWorldPosition()) print(beefalo) beefalo.Transform:SetPosition(40.00,0.00,236.48) print(beefalo.Transform:GetWorldPosition()) end) end Link to comment 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