Calling a Method using SendEvent

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
LNMRae
Posts: 7
Joined: Tue Sep 20, 2016 3:59 am

Calling a Method using SendEvent

Post by LNMRae »

Hello, I'm having an issue with the SendEvent sequence. My coding level is very, very basic at the moment, so I'm sure this is a matter of me not understanding something and possibly using the wrong sequence entirely, ahahaha.

I have an empty gameobject called MoneyManager. I attached a C# script to it called money. Inside that script I have a really simple method called takemymoney.

Code: Select all

public void takemymoney(int takemoney)
	{
		currentMoney = currentMoney - takemoney;
	}
When I call this method by clicking a UI button, it works exactly as expected. It takes whatever value I put in and subtracts it from my int currentMoney with every click.

What I hoped to accomplish is upon saying you'll buy something in the dialogue system (clicking 'yes' as a response when prompted) the method will run, subtracting an amount from my int currentMoney.

So, I used SendMessage(takemymoney, 5, MoneyManager) in the sequencer on the node after you agree to buy the item. This gives me the following error:

Dialogue System: Lua code 'return SendMessage(takemymoney, 5, MoneyManager)' threw exception 'Invoke function call on non function value.'
UnityEngine.Debug:LogError(Object)

Any suggestions on how to accomplish what I'm attempting?
User avatar
Tony Li
Posts: 22655
Joined: Thu Jul 18, 2013 1:27 pm

Re: Calling a Method using SendEvent

Post by Tony Li »

Hi,

From the error message, I'm guessing that you put the SendMessage() sequencer command in the Script or Conditions field rather than the Sequence field.

You have two options: the Script field (for Lua commands) or the Sequence field (for sequencer commands). I'll describe the Sequence option. If you want to use the Script (Lua command) option instead, read the Lua - Registering Functions instructions.

The SendMessage() sequencer command has a restriction that I had thought was documented in the command description, but I apologize -- it's implied in the example, but it's never stated outright: the argument (i.e., "5" in SendMessage(takemymoney, 5, MoneyManager)) is always sent to the GameObject as a string. I'll add that to the documentation in the next release.

To make this work in your project:

1. In your script, add another method:

Code: Select all

public void takemymoneystring(string takemoney)
   {
      takemymoney(PixelCrushers.DialogueSystem.Tools.StringToInt(takemoney));
   }
This adds a method that accepts a string. It converts the string to an int and passes that to your original method.

2. Move the command from the Script field to the Sequence field and change it to:

Code: Select all

SendMessage(takemymoneystring, 5, MoneyManager)
LNMRae
Posts: 7
Joined: Tue Sep 20, 2016 3:59 am

Re: Calling a Method using SendEvent

Post by LNMRae »

That was exactly the issue, here I thought I had made sure I put it in the right place, but I guess I was more tired at the time than I realized. Whoops!

I did not know that about it being read as a string though, thanks for that bit of code. I would have run into that issue next. Thanks as always for the help!
User avatar
Tony Li
Posts: 22655
Joined: Thu Jul 18, 2013 1:27 pm

Re: Calling a Method using SendEvent

Post by Tony Li »

Always glad to help!
Post Reply