Even though this is already solved, a few details on your original issue:
(I may have overlooked something and these have already been addressed, sorry if that is the case...)
a.add_state(20);v[(a.index + 1)||(a.enemy_id + 4)]=(b.index+1)||(b.enemy_id+4)
The
|| (logical OR-Operation) can be used to create a fallback option. When the left side
(a.index + 1) does not result in something true
(meaning false or nil), then the right side
(a.enemy_id + 4) will be looked at instead.
In your case
(a.index + 1) will usually either be a number
(the sum of the battlers position and 1) or throw an error
(when the actor is not in the party, so you are trying to add 1 to something non-numeric), so the fallback option never applies.
Also, the "index" method is not reliable to distinguish between enemies and actors. This method will return a battlers position in their respective unit, whether that's the actors party or the enemy troop.
id_to_check = self.enemy_id + 4 if enemy?() else self.index
First of all, congratulations to already finding Ruby syntax I've never heard of for the second time.
😀
Although, this time I'm not sure if this is actually a legal statement or a bug in the parser, considering it only seems to work within an existing block and the else branch seems to get executed regardless of the outcome of the if-statement...
(Maybe someone could shine a light on that, please? 🙂 )
Alternative forms of course would include a multiple line if-statement
Code:
if enemy?
id_to_check = self.enemy_id + 4
else
id_to_check = self.index
end
or the tenary operator ? : like A-Moonless-Nights suggestion
Code:
id_to_check = enemy? ? (self.enemy_id + 4) : self.index
script game_variables line 19: typeError occurred. no implicit conversion from nil to integer
do i need to convert the id to an integer?
As Theo Allen already said, you should be careful when "fixing" unexpected results this way.
It might of course fix your problem if you actually just forgot to do a conversion, it might do nothing, or it might muddy the water by getting rid of the error message without actually fixing the error.
In this case for example, the error says you used nil instead of an integer, so you should make sure you know where the nil is coming from to rule out faulty behaviour of your code. The "implicit conversion" only applies to specific number types like Floats
(decimal numbers), which can be rounded into an integer. nil itself is not a number.
In this case, you could trace the nil value back to the if-statement line from the last quote. If the battler is not an enemy, the
"id_to_check = self.enemy_id + 4" part gets skipped and your "id_to_check" - variable would stay at its default value, which happens to be nil.
nil is a special object in Ruby that is commonly used as a placeholder for "no object available" and is also the default value for most variables. Converting nil into an integer using ".to_i" for would give you 0, which would of course get rid of the error, but also cause all your actors to share variable 0 for their last target.
Depending on when you notice the discrepancy, it may become a lot more tedious to trace an issue of "sometimes wrong targets" back to your original error.
---
As an alternative, if you actually just want to reserve one variable for each actor/enemy battler you have, you could also consider adding a new property to them. Battlers already have a distinct values for hp, name, etc., so maybe you could add one more for "last_target" or something.
The easiest way to add a property is probably using the
attr_... calls. For battlers, you could use
Code:
class Game_Battler
attr_accessor :last_target
end
to read and store a unique "last_target" per battler
(The value would be stored in that battlers @last_target object variable).
So
a.last_target = b
would store a target, and
a.last_target
would read it. The default value is - as always - nil until you store a target.