Creating an event

Please post any questions about developing your plugin here. Please use the search function before posting!
Hedgehog
Member
Posts: 62
Joined: Sun Nov 03, 2013 8:54 pm

Creating an event

Postby Hedgehog » Tue Jun 03, 2014 3:18 pm

How to create and fire custom event? :)
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Tue Jun 03, 2014 3:43 pm

We hope to get the wiki back up sometime in the near future, but until then:

Syntax: Select all

from events import Event
from events.custom import CustomEvent
from events.resource import ResourceFile
from events.variable import BoolVariable
from events.variable import ByteVariable
from events.variable import ShortVariable
from events.variable import LongVariable
from events.variable import FloatVariable
from events.variable import StringVariable


# The name of the class is used as the event name (using .lower())
class My_Custom_Event(CustomEvent):
# Set the variables to be used with the event by their type

# Note that passing in a comment adds that comment to the file when saved
# Note also that not passing in a default value will result in the native type's default being used when no value is given on fire()

# You can just pass in a comment
some_bool = BoolVariable('This is a bool variable')
some_byte = ByteVariable('This is a byte variable')

# You can also pass in a default value
some_short = ShortVariable('This is a short variable', 0)

# You can also just pass in the default without a comment
some_long = LongVariable(default=1)
some_float = FloatVarialbe(default=0.0)

# You can also pass in neither a default or comment
some_string = StringVariable()

# Create the resource file object
# The filename passed in is the location from ../resource/source-python/events/
# that the file will be created as
# Pass in any events you wish to be in the file as other arguments
my_file = ResourceFile('my_resource_file', My_Custom_Event)

# Write the file and load the events
my_file.write()
my_file.load_events()


def something():
# Create an instance of the event to fire
# You can pass in all, some, or none of the variables (by name)
# Not passing them in will result in default being used
my_custom_event_instance = My_Custom_Event(some_bool=True, some_byte=2, some_short=17)

# You can also add/change variables as attributes of the instance
my_custom_event_instance.some_long = 4
my_custom_event_instance.some_float = 2.2

# And again, you can "change" a value you set on instantiation
my_custom_event_instance.some_bool = False

# Again, even though you have not set all values, the defaults will be used for values not given when fired
my_custom_event_instance.fire()


# You must define your event function after my_file.load_events()
# otherwise the event will not have been registered in the engine
# and you will not be able to register your function to be called on the event
# Also notice that the name of the event is the same as the class except lower-case
@Event
def my_custom_event(game_event):
print('MY EVENT FIRED!!!')


You can also get the instance of the event in the global scope and just set the variables each time. If you wish to do that, you might find the "reset" method of use:

Syntax: Select all

my_custom_event_instance = My_Custom_Event()


def something():
my_custom_event_instance.some_bool = True
my_custom_event_instance.some_float = 1.1

# Fire the event and reset the variables to their defaults afterwards
my_custom_event_instance.fire()
my_custom_event_instance.reset()
Hedgehog
Member
Posts: 62
Joined: Sun Nov 03, 2013 8:54 pm

Postby Hedgehog » Tue Jun 03, 2014 5:18 pm

satoon101, thank you) I was close...

Return to “Plugin Development Support”

Who is online

Users browsing this forum: Bing [Bot] and 49 guests