question

Dylan Webb avatar image
0 Likes"
Dylan Webb asked Jordan Johnson edited

Adding custom events to an object in module sdk

(Edited)
I'm trying to understand how to create a listenable event in a custom module. I understand how to inherit from a listener class and override the execute() function.
There is a line in the manual showing this:
MyObject* myObject = objectNode->objectAs(MyObject);
TreeNode* onDoStuffNode = myObject->assertEvent("OnDoStuff");
should I be able to just use: this->assertEvent("SnowEvent") from inside the bindEvents function?

More importantly, is this meant to create a flexscript node on the object for the event somewhere? how might a custom event be triggered? by calling function_s() pointing at some point on that flexscript node?

If I wanted my custom module to hold the code for the event, say SnowProcessor::SnowEvent() do I need to create a dll toggled node under SnowProcessor>behavior/cppfunctions/ or something like that?

And I need to just manually add OnSnowEvent as an object node like the others under to >behavior/eventfunctions/eventInfo to make it selectable from the eyedropper of a process flow?

FlexSim 21.0.5
module sdkflexsim 21.0.5
· 1
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Dylan Webb avatar image Dylan Webb commented ·

To clarify my confusion/understanding: an instance of a class that inherits FlexSimEvent is the node added to the eventslist the timestepper will run when it gets to it, and is created in that list by creating an instance of MyFlexSimEvent class, setting the time parameter to a number, and running createevent(e) on that instance e? Do we put all the logic for the event in that MyFlexSimEvent class that inherits from FlexSimEvent? And how do we pass it the caller and params? and is it just linked by name on the event in eventInfo? Do I need to make a child node called eventfunctions under the behaviour node? how does that link to the MyFlexSimEvent class? I suspect it's using bindEvents() somehow...

0 Likes 0 ·

1 Answer

·
Jordan Johnson avatar image
1 Like"
Jordan Johnson answered Jordan Johnson edited

I added a custom event to the SnowProcessor. You can now listen to the OnItemDusted event, which fires just after the item is received. Here are the steps to do this:

First, I added a node called onItemDusted to the SnowProcessor class. Adding events is easiest if you name the event onMyEvent, lower case "on", and CamelCase after that. I also added overrides for bindEvents() and getEventInfoObject():

class SnowProcessor : public Processor
{
protected:
//...

// events
TreeNode* onItemDusted = nullptr;
void bindEvents() override;
TreeNode* getEventInfoObject(const char* eventName) override;

public:
// ...
};

Next, I implemented bindEvents and getEventInfoObject():

void SnowProcessor::bindEvents()
{
  __super::bindEvents();
  bindEvent(ItemDusted);
}

TreeNode* SnowProcessor::getEventInfoObject(const char* eventName)
{
  auto eventFolder = eventfunctions(classobject(holder))->find("eventInfo");
  if (eventFolder) {
    auto info = eventFolder->find(eventName);
    if (info) {
      return info;
    }
  }

  return __super::getEventInfoObject(eventName);
}

The bindEvents() function is fairly straightforward. You just need to call the bindEvent() macro, passing in the name of your node, but without the "on" at the beginning. You probably want to bind all the parent's events as well.

The getEventInfoObject() function is a little more interesting, but not much. The basic idea is that you want to tell FlexSim where it can find the EventInfo object for the event name. We'll assume that there's a node called eventInfo in the eventfunctions of the SnowProcessor class. We usually put EventInfo objects inside eventfunctions, because the eventfunctions node is not copied when you create an instance of your class. If you don't find the event in this object's folder, be sure to pass this call up to the parent. I'll cover making the Event Info object later.

As a last step in the dll, you probably want to fire your event. Things that listen to your event respond when the event is fired. To do that, I overrode the onItemReceived() method:

double SnowProcessor::onReceive(treenode item, int port)
{
  double result = __super::onReceive(item, port);

  FIRE_SDT_EVENT(onItemDusted, item);

  return result;
}

The important part is using the FIRE_SDT_EVENT() macro. You pass in the node for the event you want to fire, and any important parameters you want.

Once that's done, all you need to do is add the Event Info object in the tree. Here's how I did it:

1631545870910.png

I added an eventfunctions node inside the behaviour node. I added the eventInfo folder inside that, and added the OnItemDusted event info object inside that node. Note that the name should match the name of your node, but begin with "On" instead of "on".

Once I had done all of that, I could listen to the event.

I also saw that you had questions about inheriting from FlexSimEvent. That class can be used to create a scheduled event (something you could see in the event list) or for listening to an existing event. The second case is something you'd only do if you are making an object that can listen to other objects. If you need coaching on that topic, I can help you out there as well.

SnowProcessor.h.txtSnowProcessor.cpp.txtSnow.t.txt


· 1
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Dylan Webb avatar image Dylan Webb commented ·

Thanks so much Jordan! I was missing the getEventInfoObject piece of the puzzle!

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.