NullReferenceException when trying to use TryPasteFromVector3Solved

Hiya,

I'm trying to paste a base back using TryPasteFromVector3 but I get a NRE when I load my CS file.

The relevant code is below:

		void Init() {

			var position = new Vector3(-393.7489f, 19.02035f, 1276.887f);
			float rotation = 3.7f; // rotationdiff

			PasteBackBuilding(position, rotation, "base-1");

		}

		bool PasteBackBuilding(Vector3 position, float rotation, string filename) {

			var options = new List<string>{ "blockcollision", "true" };
			var success = CopyPaste.Call("TryPasteFromVector3", position, rotation, filename, options.ToArray());

			Puts("Success: " + success);

			return true;

		}

But all I get as an error is the following:

22:44 [Error] Failed to initialize plugin 'CopyPasteBasePaster v2020.6.29' (NullReferenceException: Object reference not set to an instance of an object)
  at Oxide.Plugins.CopyPasteBasePaster.BuyBuilding (UnityEngine.Vector3 position, System.Single rotation, System.String filename) [0x0004d] in <02823facea474d6687b86173514a5bf1>:0 
  at Oxide.Plugins.CopyPasteBasePaster.Init () [0x0001c] in <02823facea474d6687b86173514a5bf1>:0 
  at Oxide.Plugins.CopyPasteBasePaster.DirectCallHook (System.String name, System.Object& ret, System.Object[] args) [0x00032] in <02823facea474d6687b86173514a5bf1>:0 
  at Oxide.Plugins.CSharpPlugin.InvokeMethod (Oxide.Core.Plugins.HookMethod method, System.Object[] args) [0x00079] in <1e2d7ad0801a4037ab6a24578c814b54>:0 
  at Oxide.Core.Plugins.CSPlugin.OnCallHook (System.String name, System.Object[] args) [0x000d8] in <1fe782f4b27a43ae85d29ebb2d56c007>:0 
  at Oxide.Core.Plugins.CSPlugin.HandleAddedToManager (Oxide.Core.Plugins.PluginManager manager) [0x00043] in <1fe782f4b27a43ae85d29ebb2d56c007>:0 

I've been staring at this too long and I can't see the issue :(

Thanks in advance for any help!

I think success can be null. Make sure ToArray also returns string[] or just replace list with array

Hmm, no joy, is this close to what you meant...?

		void Init() {

			var position = new Vector3(-393.7489f, 19.02035f, 1276.887f);
			float rotation = 3.7f; // rotationdiff

			PasteBackBuilding(position, rotation, "base-1");

		}

		bool PasteBackBuilding(Vector3 position, float rotation, string filename) {

			string[] options = new string[] { "blockcollision", "true" };
			CopyPaste.Call("TryPasteFromVector3", position, rotation, filename, options, Success(filename) );

			return true;

		}

		bool Success(string filename) {

			Puts("File loaded: " + filename);

			return true;

		}


Merged post

What would be really nice, would be if there was a PasteBack API function that simply took the position/rotation data from the file...

So you'd just do:

CopyPaste.Call("TryPasteBack", filename, options);


Merged post

Anyone able to see what's wrong here...?

Thanks.

Merged post

I've tried paring this down to just the essentails - I got rid of the callback, I didn't need it, changed the var to Vector3, function now doesn't return anything.

		
		[PluginReference] Plugin CopyPaste;

		void Init() {

			Vector3 position = new Vector3(-393.8177f, 18.96364f, 1276.837f);
			float rotation = 3.719609f; // rotationdiff
			string filename = "base-1";

			PasteBackBuilding(position, rotation, filename);

		}

		void PasteBackBuilding(Vector3 position, float rotation, string filename) {

			string[] options = new string[] { "blockcollision", "true" };
			CopyPaste.Call("TryPasteFromVector3", position, rotation, filename, options);

		}​

These changes are partially based on this bit of code: https://umod.org/community/copy-paste/19023-trypastefromvector3-not-working



Merged post

So I added a slight delay to the CopyPaste call, just 10 seconds, and reintroduced the callback. Now there is no error but also no base pasted, but the callback does trigger with the expected data.

My full code now looks like this:

		[PluginReference]
		private Plugin CopyPaste;

		private void OnPluginLoaded(Plugin plugin) {
			switch (plugin.Title) {
				case "CopyPaste": {
					CopyPaste = plugin;
					break;
				}
			}
		}

		private void OnPluginUnloaded(Plugin plugin) {
			switch (plugin.Title) {
				case "CopyPaste": {
					CopyPaste = null;
					break;
				}
			}
		}

		void Init() {

			timer.Once(10f, () => {

				/* Anvil base at gridref I2 */

				Vector3 position = new Vector3(-393.8177f, 18.96364f, 1276.837f);
				float rotation = 3.719609f;
				string filename = "base_1";
				string description = "Anvil base at gridref I2";

				PasteBackBuilding(position, rotation, filename, description);

			});

		}

		void PasteBackBuilding(Vector3 position, float rotation, string filename, string description) {

			string[] options = new string[] { "blockcollision", "true" };
			CopyPaste.Call("TryPasteFromVector3", position, rotation, filename, options, PasteBackCallBack(filename, description));

		}

		object PasteBackCallBack(string filename, string description) {

			Puts("Pasted: " + description + " | Filename: " + filename);
			return true;

		}​

The OnPluginLoaded/OnPluginUnloaded was stolen from somewhere else, no idea whether it adds anything...

What else can I try? 

Merged post

@misticos - any help greatly appreciated when you get a chance. 
OnPluginLoaded/Unloaded is unnecessary, attribute does everything for you.

Merged post

Try also options stability false and autoheight true or false and set height manually.

@misticos - Hiya thanks again but still no joy.

I've removed the OnPluginLoaded/Unloaded and added the stability/autoheight options but still nothing:

		[PluginReference]
		private Plugin CopyPaste;


		void Init() {

			timer.Once(5f, () => {

				/* Starter base at gridref C3 */

				Vector3 position = new Vector3(-1201.91f, 1.008322f, 1104.868f);
				float rotation = 4.892161f; // rotationdiff
				string filename = "starter_1";
				string description = "Starter base at gridref C3";

				PasteBackBuilding(position, rotation, filename, description);

			});

		}

		void PasteBackBuilding(Vector3 position, float rotation, string filename, string description) {

			string[] options = new string[] { "stability", "false", "autoheight", "false" };
			CopyPaste.Call("TryPasteFromVector3", position, rotation, filename, options, PasteBackCallBack(filename, description));

		}

		object PasteBackCallBack(string filename, string description) {

			Puts("Pasted: " + description + " | Filename: " + filename);
			return true;

		}


By `set the height manually`, do you mean as an option? These are the options I tried:

			string[] options = new string[] { "stability", "false", "autoheight", "false", "height", "1" };
			string[] options = new string[] { "stability", "true", "autoheight", "false", "height", "1" };
			string[] options = new string[] { "stability", "true", "autoheight", "false" };
			string[] options = new string[] { "stability", "true", "autoheight", "true" };
			string[] options = new string[] { "stability", "false", "autoheight", "false" };
			string[] options = new string[] { "stability", "false", "autoheight", "true" };


The base is just a simple 2x1 with no deployables/inventories/electrics etc - I'm trying to paste it on flat beach off the water.

Thanks again :D



Merged post

Just took these settings from RaidableBases but still no joy:

			string[] options = new string[] { "stability", "false", "autoheight", "false", "height", "2.5", "entityowner", "false" };​


Merged post

@misticos - I added my own PasteFromFile API endpoint to your CopyPaste.cs and it worked perfectly. I'm happy to send you over the file if you want to incorporate the changes? Not sure how best to do that though...

Merged post

Hmm, actually it pastes a bit high - but it pastes notheless :D

Merged post

Oh, still had height 2.5 in options - works fine now I've taken that out :)
Locked automatically