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.

[SOLVED]Making Switches only effect one event

Status
Not open for further replies.
Before you read any further, I am not looking for how to use switches or Self Switches. This is different.

Hi. For ages I've been developing the ABS for my game. It's all evented, and works perfectly, but I have one problem. A problem that I think can be solved pretty easily with scripts. All of my enemy events are set up so as to read where the actor is in realation to the enemy event through variables. Then when the player fires a gun (it's a shooting game) the enemy will react differently depending on which gun. That all works. Then it checks the enemies health, and if it is bellow a certain variable, the enemy dies. Now here's my problem. This works really well, but it means each enemy must have a unique set of variables for each, so that all the enemies don't die at once. Are you with me so far? Ok.

What I want to know is is there a way with the script part of Conditional Branch to make the same variable store different numbers depending on the event it is being used in? I've tried, and this is what I've come up with, but it doesn't work:

A script entry at the top of the event page says

Name_Of_Event =
$game_map.events[@event_id].event.name

Then the eventing I have already done follows where it checks for what gun is equiped and how far away the player is, ect. Then when the player is in range and fire the gun, I tried the script input

$game_variables[14+Name_Of_Event.to_i] =
$game_variables[14+Name_Of_Event.to_i]
-7

Variable 14 is the enemies health. Then underneath that in the event page it says

Conditional Branch Script

$game_variables[14+Name_Of_Event] <= 20

The enemies event vanishes after there is a falling animation and it flashes. Basically.

So, what am I doing wrong? Can it even be done? Please help.

Thanks,
Fox
 
Sounds like you have a decent idea of scripts which is very good for something like this.

I suggest making a ABS_Enemy class that holds each enemies health (and other attributes) as opposed to using Game_Variables (will just make you change your Change Variable event command with the script command to change these values.

Try this:
[rgss]class Abs_Enemy
  attr_accessor :hp
  def initialize(hp = 100)
    @hp = hp
  end
end
 
class Game_Event
  attr_reader :abs_enemy
  alias_method :seph_absenemysetup_gmevt_refresh, :refresh
  def refresh
    seph_absenemysetup_gmevt_refresh
    @abs_enemy = nil
    return if @erased || @list == nil
    for ec in @list
      next unless ec.code == 108
      if ec.parameters[0].upcase.include?('ABS::ENEMY')
        parameters = ec.parameters[0].split
        @abs_enemy = Abs_Enemy.new(parameters[1].to_i)
        break
      end
    end
  end
end
[/rgss]

Now I am not sure how you are doing this system but for your event but now to setup an event as an abs enemy, insert a comment on the page with:
[rgss]Comment: ABS::Enemy n
[/rgss]

Replace n with the starting life for an enemy.

Now to read/decrease enemy life:
[rgss]# Read
$game_map.events[event_id].abs_enemy.hp
# Subtract life
$game_map.events[event_id].abs_enemy.hp -= n
# Add life
$game_map.events[event_id].abs_enemy.hp += n
# etc.
[/rgss]


I hope this doesn't make you modify too much. Let me know if you need any further help with this.
 
Make sure you are replace event_id with the corresponding event id it is associated with.

So if in event ID:005, you add the comment with this:
Code:
Comment: ABS::Enemy 100

To get that events hp, you would use:
Code:
$game_map.events[5].abs_enemy.hp

And make sure there is a space between ABS::Enemy and the hp number.
 
What is the script command you are using to get that?

Just a break-down of the line I gave you
$game_map.events -> Returns events hash from $game_map
$game_map.events[5] -> Returns the value from the above hash with the index (event_id) of 5
$game_map.events[5].abs_enemy -> Returns the ABS_Enemy object
$game_map.events[5].abs_enemy.hp -> Returns hp instance value from above object
 
yeah, sure. I'll upload it and PM you it.

EDIT: Wait, doesn't matter, I've fixed it. It was just how I was using it. Sorted. Now I have another problem, though. I have a conditional branch running, that says

$game_map.events[20].abs_enemy.hp <= 0

and then if that is true, it turns on Self Switch B, which then causes the enemy to die.
But it doesn't work... Is it something I'm doing?
 
No, scrap that again, I've sorted it. But for some reason, the enemies health renews itself every time it's shot. Here's how I've got the event set up. I'm pretty sure it can be sorted by just moving it somewhere else, but I can't fix it.

Ok:

On the first event page, I have an event that runs when the event touches the player, and the player then loses health.
The second page runs when the switch 'Shooting' is turned on. It's turned on through a common event that checks what weapon is equiped and so on. On this second page, I've put the Comment that says ABS::Enemy 14 (it's a weak enemy). Then there is a list of conditional branches that check where the player is in relation to the event, what weapon is equiped again and so on. When all the checks off as true, I've put the script command

$game_map.events[20].abs_enemy.hp -=7

Then at the bottom of the list of conditional branches, there is another conditional branch that says

$game_map.events[20].abs_enemy.hp <=0

Then a self switch that turns on and activates the event's death.

This all works fine when the enemies health is set at 7 - the enemy dies in one shot. But when it's anything higher, the health seems to renew itself every time the second page is played out. What am I doing wrong?

Thanks.
 
I am guessing because you are refreshing the events and everytime the event is refreshed, it re-creates the ABS::Enemy instance.

Try this code instead:
[rgss]class Abs_Enemy
  attr_accessor :hp
  def initialize(hp = 100)
    @hp = hp
  end
end
 
class Game_Event
  attr_reader :abs_enemy
  alias_method :seph_absenemysetup_gmevt_init, :initialize
  def initialize(map_id, event_id)
    seph_absenemysetup_gmevt_init(map_id, event_id)
    @abs_enemy = nil
    return if @erased || @list == nil
    for ec in @list
      next unless ec.code == 108
      if ec.parameters[0].upcase.include?('ABS::ENEMY')
        parameters = ec.parameters[0].split
        @abs_enemy = Abs_Enemy.new(parameters[1].to_i)
        break
      end
    end
  end
end
[/rgss]

Hopefully that helps.
 
I believe it's an error in your coding then or use (I could be wrong).

I test this with an event:

Code:
Comment :ABS::ENEMY 100

Script&#058; enemy = $game_map.events[2].abs_enemy

 

p enemy.hp

enemy.hp -= 7

Whenever I click on the event, I get 100, 93, 86, etc. so I believe the script is working correctly.
 
Ok you comment has to be placed on the first page (because of the way it automatically sets the ABS::Enemy object).

If you wanted to change that so that the script didn't automatically set the ABS::Enemy object, change this line:
attr_reader :abs_enemy
to
attr_accessor :abs_enemy


Now to set the abs_enemy object for your events, just use a script with this call:
Code:
event = $game_map.events[event_id]

event.abs_enemy = Abs_Enemy.new(life)

Replace event_id with the events id and life with the maxhp for that enemy.

Hope that helps.
 
Status
Not open for further replies.

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