Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Script-Based Switches, Event-based self-switches/variables

poccil

Sponsor

Script-Based Switches (Written by Peter O.)

This script section is designed to enable advanced control over event handling
by assigning certain switches to an arbitrary script.  This script also contains
support for temporary self-switches and event-specific variables.

For script-based switches, the switch is true as long as the condition on the switch's
name remains true. Script-based switches are distinguished from regular switches
using the prefix "s:". Be aware that due to the size limit on a switch's name, it may
be necessary to use this kind of switch to call other methods
that return a true or false value.  Methods on script-based switches are called within
the context of the Game_Event class.

Only switches on map events are supported, not those on common events, and
only within the "Conditions" section of an event page.

Temporary Self Switches

Each event in the game comes with a set of temporary self switches.  This set
is cleared whenever a map is entered.  To check whether a temporary self switch
is on, use a switch named "s:tsOn?(X)" where X is one of "A", "B", "C", or "D".
To check whether it's off, use a switch named "s:tsOff?(X)".  To turn a temporary
self switch on or off, use a script like:

setTempSwitchOn("A")

or:

setTempSwitchOff("A")

Event-Specific Variables

Each event in the game comes with its own variable, which can be used for any purpose. 
To retrieve a variable, call the script function "getVariable", and to set the value of the
current event's variable, call the script function "setVariable(X)", where X is the value
to set to the variable.  To check the variable's value in an event page's condition, use the
event function "variable" (or "varAsInt", meaning "variable as integer") to help you.  For example, to check whether the event's variable is equal to 1, you would use a switch named: "s:varAsInt==1".

Checking Self Switches

A script-based switch may be useful, among other things, for checking whether
a self-switch is off as one of an event page's conditions.  This is possible by using a
switch whose name is: s:isOff?("A")  etc.

The code for script-based switches follows.

Code:
 

SDK.log("Script-Based Switches", "Peter O.", 1.0, "10/23/07")

if SDK.state("Script-Based Switches") == true 

 

class Game_Event

  alias petero_scriptswitches_Game_Event_initialize initialize

  def initialize(map_id, event)

    @tempSwitches={}

    petero_scriptswitches_Game_Event_initialize(map_id,event)

  end

  def name

   return @event.name

  end

  def id

   return @event.id

  end

  def tsOn?(c)

    return @tempSwitches && @tempSwitches[c]==true

  end

  def tsOff?(c)

    return !@tempSwitches || !@tempSwitches[c]

  end

  def setTempSwitchOn(c)

    @tempSwitches[c]=true    

    refresh

  end

  def setTempSwitchOff(c)

    @tempSwitches[c]=false

    refresh

  end

  def variable

    return nil if !$game_eventvars

    return $game_eventvars[[@map_id,@event.id]]

  end

  def varAsInt

    return 0 if !$game_eventvars

    return $game_eventvars[[@map_id,@event.id]].to_i

  end

  def expired?(secs=86400)

    ontime=self.variable

    return ontime && (Time.now.to_i>ontime+secs)

  end

  def onEvent?

    return @map_id==$game_map.map_id &&

       $game_player.x==self.x && $game_player.y==self.y

  end

  def isOff?(c)

    return !$game_self_switches[[@map_id,@event.id,c]]

  end

  def switchIsOn?(id)

    switchname=$data_system.switches[id]

    return false if !switchname

    if switchname[/^s\:/]

      return eval($~.post_match)

    else

      return $game_switches[id]

    end

  end

  def refresh_trigger_conditions

    # Check in order of large event pages

    for page in @event.pages.reverse

      # Make possible referrence for event condition with c

      c = page.condition

      # Switch 1 condition confirmation

      if c.switch1_valid

        if switchIsOn?(c.switch1_id) == false  # Modification

          next

        end

      end

      # Switch 2 condition confirmation

      if c.switch2_valid

        if switchIsOn?(c.switch2_id) == false  # Modification

          next

        end

      end

      # Variable condition confirmation

      if c.variable_valid

        if $game_variables[c.variable_id] < c.variable_value

          next

        end

      end

      # Self switch condition confirmation

      if c.self_switch_valid

        key = [@map_id, @event.id, c.self_switch_ch]

        if $game_self_switches[key] != true

          next

        end

      end

      # Set local variable: new_page

      new_page = page

      # Remove loop

      break

    end

    # Return new page

    return new_page

  end

end

 

 

class Interpreter

 def setEventTime(*arg)

   time=Time.now.to_i

   setSelfSwitch(@event_id,"A",true)

   $game_eventvars[[@map_id,@event_id]]=time

   for otherevt in arg

    setSelfSwitch(otherevt,"A",true)

    $game_eventvars[[@map_id,otherevt]]=time

   end

 end

 def getVariable

  return $game_eventvars[[@map_id,@event_id]]

 end

 def getVariableAsInt

  value=$game_eventvars[[@map_id,@event_id]]

  return value ? value.to_i : 0

 end

 def setVariable(value)

  $game_eventvars[[@map_id,@event_id]]=value

 end

 def setTempSwitchOn(c)

  get_character(0).setTempSwitchOn(c)

 end

 def setTempSwitchOff(c)

  get_character(0).setTempSwitchOff(c)

 end

 def setSelfSwitch(event,swtch,value)

  $game_self_switches[[@map_id,event,swtch]]=value

  $game_map.need_refresh = true

 end

end

 

class Scene_Load < Scene_File

  alias petero_scriptswitches_Scene_Load_read_save_data read_save_data

  def read_save_data(file)

    petero_scriptswitches_Scene_Load_read_save_data(file)

    $game_eventvars=Marshal.load(file)

  end

end

 

class Scene_Save < Scene_File

  alias petero_scriptswitches_Scene_Save_write_save_data write_save_data

  def write_save_data(file)

    petero_scriptswitches_Scene_Save_write_save_data(file)

    Marshal.dump($game_eventvars,file)

  end

end

 

class Scene_Title

  alias petero_scriptswitches_Scene_Title_command_new_game command_new_game

  def command_new_game

    $game_eventvars={}

    petero_scriptswitches_Scene_Title_command_new_game

  end

end

 

 

end

 

 
 

poccil

Sponsor

In principle, event-based variables are arbitrary Ruby objects, so they can be arrays, strings, or hashes, or even more complex objects.  For instance, an event-based variable can contain two "variables" (that is, integers) by being an array.

The following sequence of event commands is an example (it's rather invented, admittedly):

Code:
Script: setVariable([0,0])  if !getVariable            -- Initialize variables
Conditional Branch:  Script:  getVariable()[0]==0         -- Access first event based variable
  Script: getVariable()[0]=1       -- Set first event based variable
Branch End
Conditional Branch:  Script:  getVariable()[0]==1        -- Access first event based variable
  Script: getVariable()[1]=1         -- Set second event based variable
Branch End

Also in principle, each event has an unlimited number of temporary self switches, which are reset when the player leaves the map.  For example, an event can have self switches (both temporary and conventional) named "E", "F", "G", "H", and so on.
 

Lei

Member

The problem that I face at the moment is i need to develop a system where both people and animals alike need to have a like-dislike towards the player...

animals also need hunger meters, happiness meters and have favorite foods etc... which would need to be set for each animal bred or captured...
 
Sry for pushing this up...

But, has anyone a Demo or a working Map with that??? Don´t check really how to use it..
 
mmm if you need to set variable value, you may also use this...
$game_variables[n] = any value you may need to set...
in case you need to turn a switch on or off...
$game_switches[n] = true
n is a number between 1 and 5000

if it is part of a condition, you'll need to code it like this...

if $game_variables[n] == any value you may need to set...

if $game_switches[n] == true
it may be true (on) or false (off)...
 

poccil

Sponsor

I am already aware of those two global variables.  However, the variables $game_switches and $game_variables are global, that is, they apply everywhere in the game, while temporary self-switches and event-based variables, as defined in my system, are specific to only one event in a map.  They also better enable the creation of "stand-alone" event systems, "stand-alone" here meaning not relying on the global switches and variables ($game_switches and $game_variables)
 
I don't remember I was talking to poccil... even so I think there was a way to use self switches already so there was no need to create that specific code...
 

poccil

Sponsor

Self-switches ($game_self_switches) are also specific to one event in a map, but the key difference here is that traditional self-switches retain their state even after leaving a map, whereas temporary self-switches are always reset whenever the player enters or leaves the map and are saved with the game data only for as long as the player remains on the map. 

Also, self-switches can only be either true or false.  Event-based variables, on the other hand, are both specific to an event and can be any Ruby object (numbers, strings, arrays, hashes, etc.).
Event-based variables are saved with the game data.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top