Jump to content

Help with RPC and BufferedActions


Recommended Posts

Hello,
I am trying to make a mod which allows users to place items in a grid by dragging while clicking. Similar to ActionQueue by simplex and xiaoXzzz, I hope to make this a client only mod and I think I am done with 90% of the framework. However, I am having an issue with RPCs and BufferedActions (Needed to do deploy, build, and drop on a dedicated sever.)

My issue is as follows:
I set on AddSuccessAction and AddFailAction functions for the BufferedActions (build, deploy, drop) generated by my mod. These are used to cancel everything and clean up on failure, or build the next queued item on success. Then, In order to get my mod to work server side, I add a preview_cb to the BufferedAction which contains a 'SendRPCToServer' corresponding to that action. However, when the BufferedAction is send to locomotor:PreviewAction it builds the first item then returns an action failure, causing the rest of the queue to be deleted.

 

I like the clean up on fail logic to ensure I am properly managing my memory, but I couldn't find any other examples which used RPCs and an AddFailAction method.

Here is my GitRepo. Attached is my mod in it's current state. Below are relevant (abridged) code snippets.

function TiledPlacer:deploy(inst, index)
  --[[ code hidden in here... ]]
  
  		-- Build BufferedAction for a deployable item.
		local action = BufferedAction(self.player, nil, 
				ACTIONS[inv_item:IsDeployable() and 'DEPLOY' or 'DROP'],
				place_item, pos_to_place, nil,
				inv_item:DeploySpacingRadius() or 3.2, nil, 
				self.player.Transform:GetRotation())		

		action:AddFailAction(function ()
			self:clean_up_all()
		end)

		action:AddSuccessAction(function ()
			print("Success Deploy")
			if self.planned then
				local temp_inst = table.remove(self.planned, index)
				temp_inst.prefab:Remove()
				self:build_next()
			end
		end)
		self:do_action(action)	
 	--[[ code hidden in here... ]]
end

function TiledPlacer:do_action(action)
  	--[[ code hidden in here... ]]
	
	if TheWorld.ismastersim then
		--self.player.components.playercontroller:DoAction(action)
    	-- This part works.
		self.player.components.locomotor:PushAction(action, true)
	else
		if action.action == ACTIONS.BUILD then
			print("Build to server.")
			action.preview_cb = function()
				SendRPCToServer(RPC.MakeRecipeAtPoint, action.recipe.rpc_id,
					action.pos.x, action.pos.z, action.rotation, action.skin)
				end
			self.player.components.locomotor:PreviewAction(action, true)
			
		elseif action.action == ACTIONS.DEPLOY then
			print("Deploy to server.")
			action.preview_cb = function()
				SendRPCToServer(RPC.ControllerActionButtonDeploy, 
					action.invobject, action.pos.x, action.pos.z,
					action.rotation, true)
				end
			self.player.components.locomotor:PreviewAction(action, true)
			
		elseif action.action == ACTIONS.DROP then
			print("Drop to server.")
			action.preview_cb = function()
				SendRPCToServer(RPC.DropItemFromInvTile, 
					action.invobject, true)
				end
			self.player.components.locomotor:PreviewAction(action, true)
			
		else
			print("Bad Action Type " .. action.action.id)
			return self:clean_up_all()
		end
	end
end

 

tiledplanting.zip

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.

×
  • Create New...