Anyone have a basic, modular, event handler that kicks in after player moves to new location?

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
henvu50
Veteran
Posts: 337
Joined: Wed Aug 22, 2018 1:22 am
Contact:

Anyone have a basic, modular, event handler that kicks in after player moves to new location?

#1 Post by henvu50 »

In code, you've moved to a new location. Now you need to check for events for the location you just moved to, of which there can be multiple event types that vary.

I can write this myself, but I'm hoping to avoid reinventing the wheel over & over & over again. This will probably take me 20 painful, grueling hours, lol.

This is pseudo code showing what I need. The concept is rather simple.

Code: Select all

label start:

      # player moved to alley50 via some function that returned a label
      $ returnedLabelLocation = playerMovedHere
           # label alley50
          jump returnedLabelLocation 
     return

label alley50:

    "you've arrived at alley 50.. you look around..."      

     #check for events      
     returnedEventLabel = checkForEvent("alley50")
        if returnedEventLabel not blank  then
            jump returnedEventLabel  #event50
      else
           #no events, player doesn't see anything going on and chooses to leave via navigation
      return

label event50:
      "You're being ambushed!!!"
      return

init python:
      # this is sloppy i know, but just want to show the flow
      checkForEvents {
                # should this be tied to the area or contain a list of every event in the game among all locations?
                conditionList = (event01: label: event50, condition, condition, condition,
                           event02: label: event51,  condition, condition, condition,
                           event03:  label: event52, condition, condition, condition,

              eventLabel = docheckAndReturnEventLabel
              return eventLabel
       }

Last edited by henvu50 on Sat Jun 12, 2021 10:50 pm, edited 1 time in total.

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2397
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: Anyone have a basic, modular, event handler that kicks in after player moves to new location?

#2 Post by Ocelot »

When asking to help with design it is usually helpful to tell what are you trying to achieve and what problems do you have with you current designs.

Because a bunch of if statement in the befinning of each label is a valid design choice, but I can assune that it is what you are trying to avoid.
One possible choice is to have a centralised event handler which you are jumping to instead of your label:

Code: Select all

# jump statements cannot pass parameters
# so we use global to pass them
default jump_target = ''

label jump_event_handler:
    # Handle events
    $ event = get_event(location=jump_target)
    if event:
         if event.type == "redirect":
            $ jump_target = event.target
            jump .post
        if event.type == ...
        # . . .
label .post:
    jump expression jump_target

label start:
    "..."
    $ jump_target = "some_label"
    jump jump_event_handler
< < insert Rick Cook quote here > >

henvu50
Veteran
Posts: 337
Joined: Wed Aug 22, 2018 1:22 am
Contact:

Re: Anyone have a basic, modular, event handler that kicks in after player moves to new location?

#3 Post by henvu50 »

When asking to help with design it is usually helpful to tell what are you trying to achieve and what problems do you have with you current designs.
Basically when the player moves to a new area, events will happen in that area, like he gets attacked, or an npc will show up and wait for him. I want an event handler that kicks in every time the player moves to different locations, so like you said, it's not a massive cluster of if then statement.

Your code is great for I need , it's very elegant! Two steps above what I was thinking of doing. Now i just need to write the get_event function. I'm currently trying to write that now, but I have an issue with define vs default and classes. I made another post. Data that triggers event is static, so I need to use the define statement, but I can't get define to work, but default statement works fine though.

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Anyone have a basic, modular, event handler that kicks in after player moves to new location?

#4 Post by Remix »

I would also advise not using the old Lexer Event Handler though would note that the basic concept is very usable.
I do have a part complete much better version of it in the works, just many weeks from being usable.

The general idea is:
One Handler that stores all Events
Each relevant label has its own Event
Each Event has possible Tests and Triggers
Tests can be things like "this event only occurs in ggg location" or "between 4pm and 7pm" or "when Eileen is present" etc
Triggers are just things like auto-start, repeat count etc

When the game loop cycles it just asks the Handler for all Events that are Valid (location/time/chars present etc) and uses those (if any) to progress the story. Moving to another location runs the loop again and possibly finds different events/labels valid for different location.

Your first real step should be to fully decide every single test and trigger you might need as you could simplify everything by just using a rigid label naming protocol...
Basic pseudo-code example

Code: Select all

init python:
    def get_valid_labels():
        valid_labels = []
        for k in renpy.get_all_labels():
            if not k.startswith("event_"):
                continue
            loc, per, ch = k[6:].split("_")
            if loc == location and per == period and ch.location == loc:
                valid_labels.append(k)
        return valid_labels
        
label event_forest_afternoon_eileen: ### label name dictates validity
    e "The trees look lovely in the sunlight"
Probably the simplest (yet pretty versatile) approach...
Frameworks & Scriptlets:

Post Reply

Who is online

Users browsing this forum: Google [Bot]