Sign In Register

How can we help you today?

Start a new topic
Answered

How to create any Request method ?

Hello,

How to create any Request method, that I will be call multiple times from the script ?
For example, let's take getPropertyRequest() and create simple method:

 

  public void getGSProperties(string shortCode, int state)
  {
  new GameSparks.Api.Requests.GetPropertyRequest().SetPropertyShortCode(shortCode).Send((response),  =>
  {
  if (!response.HasErrors)
  {
  Debug.Log("'" + shortCode + "' received from server");
  GSData target = response.Property;

  }
  else
  {
  Debug.Log ( "Error Getting '" + shortCode +"' from server");
  }
  });

  }

 

GetPropertyRequest using Action<GetPropertyResponse> delegate, which cannot return response property from the lambda expression. Secondly, i'ts worked, if we assign variable locally, but I can't create universal method, because lambda expression doesn't support OUT and REF params, so we can't create method with reference parameter, for ex: public void getGSProperties(string shortCode, ref GSData target).

How to create 1 method for getPropertyRequest (or any other request methods), which can return received value to the script ?


Best Answer

Hi Maxim,


Would the following sample of code help in what you're trying to achieve?

 

public void GetLevelItems(string levelName, Action<IEnumerable<ItemModel>> onLevelItemsReceived)
{
	new GameSparks.Api.Requests.LogEventRequest_getLevelItems()
		.Set_levelName(levelName)
		.Send((response) =>
		{
			if (response.HasErrors) return;
			onLevelItemsReceived(ModelFactory.CreateLevelItemTransferModel(response));
		});
}

 

As you can see what I'm doing here is passing in an Action that can then be called once a response have received.


Hope this helps,

 - Steve

1 Comment

Answer

Hi Maxim,


Would the following sample of code help in what you're trying to achieve?

 

public void GetLevelItems(string levelName, Action<IEnumerable<ItemModel>> onLevelItemsReceived)
{
	new GameSparks.Api.Requests.LogEventRequest_getLevelItems()
		.Set_levelName(levelName)
		.Send((response) =>
		{
			if (response.HasErrors) return;
			onLevelItemsReceived(ModelFactory.CreateLevelItemTransferModel(response));
		});
}

 

As you can see what I'm doing here is passing in an Action that can then be called once a response have received.


Hope this helps,

 - Steve

Login to post a comment