Welcome to Keen Software House Forums! Log in or Sign up to interact with the KSH community.
  1. You are currently browsing our forum as a guest. Create your own forum account to access all forum functionality.

Spawn Prefab Relative to Block

Discussion in 'Modding' started by Mortisfire, Aug 21, 2015.

Thread Status:
This last post in this thread was made more than 31 days old.
  1. Mortisfire Apprentice Engineer

    Messages:
    148
    Hey, I've been trying to figure out how I can spawn a prefab relative to block. In this case, it's a TextPanel block. I've looked at Midspace's admin helper but his code spawns relative to the player.

    I've been using this code from the "Mission Blocks" mod except it only spawns ships in specific locations on the map.
    Code:
    private IMyEntity SpawnPrefab(string prefabName, VRageMath.Vector3 location, long owner)
            {
                IMyEntity entity = null;
                location = new Vector3D();
    
    
                MyPrefabDefinition prefab = null;
                var prefabKvp = MyDefinitionManager.Static.GetPrefabDefinitions().FirstOrDefault(kvp => kvp.Key.Equals(prefabName, StringComparison.InvariantCultureIgnoreCase));
    
                if (prefabKvp.Value != null)
                {
                    prefab = prefabKvp.Value;
                }
                var index = 0;
                if (prefab != null && prefab.CubeGrids.Count() != 0)
                {
    
               
                    MyAPIGateway.Entities.RemapObjectBuilderCollection(prefab.CubeGrids);
                    foreach (var grid in prefab.CubeGrids)
                    {
                        grid.PositionAndOrientation = new MyPositionAndOrientation(
                            grid.PositionAndOrientation.Value.Position,
                            grid.PositionAndOrientation.Value.Forward,
                            grid.PositionAndOrientation.Value.Up
                        );
                        entity = MyAPIGateway.Entities.CreateFromObjectBuilderAndAdd(grid);
                        try
                        {
                            if (owner != 0L)
                            {
                                (entity as Sandbox.ModAPI.IMyCubeGrid).ChangeGridOwnership(owner, MyOwnershipShareModeEnum.None);
                            }
                        }
                        catch (Exception ex)
                        {
                            MyAPIGateway.Utilities.ShowMessage("Exception", ex.Message);
                        }
                    }
                }  // if prefab != null
                return entity;
            }
    Hope someone can help me out. Thanks.
     
  2. Mortisfire Apprentice Engineer

    Messages:
    148
    Well if anyone in the future takes a look at this, I have come up with the code. Though it is very sloppy as I am an amateur. If you are interested in anything like that, just post here and I can paste the code.
     
  3. soknorb Trainee Engineer

    Messages:
    65
    sure
    Post it and ill give it a look to help clear it up a bit if needed
     
Thread Status:
This last post in this thread was made more than 31 days old.