Chaos Project

RPG Maker => RPG Maker Scripts => Topic started by: Seox on July 06, 2009, 10:25:31 am

Title: Is there a way to....
Post by: Seox on July 06, 2009, 10:25:31 am
Do something like this?

kill_some_people unless moral_code == good
If so, then do ______.


instead of this:

kill_some_people unless moral_code == good
if moral_code == good....


That may not seem like much, but when the line goes ACROSS THE PAGE, it gets a bit ambiguous, repetitive, and also confusing as hell. I'm just looking for a way to go
DO A unless B, and if B, then do this instead.
without restating B. Ternary operators seem like they MIGHT work, but, for some reason, I don't seem to think they work for anything except assignment. Meh.
Title: Re: Is there a way to....
Post by: Blizzard on July 06, 2009, 10:37:57 am
unless X

is the same as
if not (X)


So this:
unless X
  # a
else
  # b
end

is the same as:
if not X
  # a
else
  # b
end

Title: Re: Is there a way to....
Post by: G_G on July 06, 2009, 11:19:08 am
Wow blizz that makes more sense...I even had troubles with the unless command. lol thnx
Title: Re: Is there a way to....
Post by: Seox on July 06, 2009, 01:37:59 pm
Let me restate,

$data_weapons[53].build_encryption_ray(743, ham, nine) + $data_weapons[oaphfoiwenbrkjbwmtgf] unless i_give_a_f***
if $data_weapons[53].build_encryption_ray(743, ham, nine) + $data_weapons[oaphfoiwenbrkjbwmtgf]



That's a bit long. It's not about if or unless, it's about, is there a way to avoid using up a WHOLE nother line, and making it look UBER confusing, just to restate the conditional?


Oh yeah. Unless/else. Der >.>. I'm just thinking about within the context of the statement modifiers.
Title: Re: Is there a way to....
Post by: Ryex on July 06, 2009, 01:44:58 pm
if you make it clear the lin is unfinished like leafing an && or || or ,  or (  or {   ect. at the end of the line then press enter and indent it will continue the lin on the next line.
i didn't really explain that will so I hope someone one else dose better or did i even answer your question?
Title: Re: Is there a way to....
Post by: Blizzard on July 06, 2009, 01:47:20 pm
Because indentation is used to mark where blocks start and end so the code gets readable.

@Seox: Why checking 2 times for the same condition? Use if-else/unless-else blocks.

And what you want is probably this.

if a then b
b if a
unless a then b
b unless a


I never tried the third one, BTW.
Title: Re: Is there a way to....
Post by: Fantasist on July 06, 2009, 04:00:19 pm
Quote
kill_some_people unless moral_code == good
If so, then do ______.


instead of this:

kill_some_people unless moral_code == good
if moral_code == good....



I'd do it this way, it's least confusing:


if moral_code == good
  #blah
else
  kill_some_people
end
Title: Re: Is there a way to....
Post by: Seox on July 06, 2009, 06:46:44 pm
Thank you, everyone. I guess I'm just a lazy bastard who was looking to use statement modifiers like that, when I shoulda just stopped and thought.

^_^
Title: Re: Is there a way to....
Post by: Fantasist on July 06, 2009, 07:49:19 pm
Not really. One fine day, you could realize s shortcut did exist and you haven't been using it.
I think you know this already, but large conditions can stored in local variables for convelience:
$data_weapons[53].build_encryption_ray(743, ham, nine) + $data_weapons[oaphfoiwenbrkjbwmtgf] unless i_give_a_f***
if $data_weapons[53].build_encryption_ray(743, ham, nine) + $data_weapons[oaphfoiwenbrkjbwmtgf]


can be:

condition = $data_weapons[53].build_encryption_ray(743, ham, nine) +
                $data_weapons[oaphfoiwenbrkjbwmtgf] unless i_give_a_f***
if condition


And don't forget the ternary operator ?: It's a handy replacement for small if-else statements, and it's my personal favorite.

Syntax:
condition ? statement_if_true : statement_if_false


For example, this:
moral_code == good ? donate : kill_someone


is the same as this:

if moral_code == good
  donate
else
  kill_someone
end
Title: Re: Is there a way to....
Post by: fugibo on July 06, 2009, 08:22:38 pm
If you really feel like using one line, you can encapsulate all the <evil> stuff and all the <good> stuff in their own methods, and have them return true or false depending on morals (in other words, the method "evil" would do evil things and return true if you are evil, and return false if not, and simiilar with the method "good"). Then you could just use

good unless evil


Of course, that's rather hard to read/impractical. But if you absolutely have to have one line...
Title: Re: Is there a way to....
Post by: Seox on July 06, 2009, 09:05:47 pm
O_O

Thank you, all of you.

Yeah, I LOVE ternary operators. They also sound cool as hell.

Thank you, LongFellow ^_^
Title: Re: Is there a way to....
Post by: Fantasist on July 07, 2009, 04:23:05 am
Ternary operators just mean they operate on 3 operands (binary 2, unary 1).
Title: Re: Is there a way to....
Post by: Blizzard on July 07, 2009, 07:49:00 am
Quote from: Longfellow on July 06, 2009, 08:22:38 pm
If you really feel like using one line, you can encapsulate all the <evil> stuff and all the <good> stuff in their own methods, and have them return true or false depending on morals (in other words, the method "evil" would do evil things and return true if you are evil, and return false if not, and simiilar with the method "good"). Then you could just use

good unless evil


Of course, that's rather hard to read/impractical. But if you absolutely have to have one line...


VERY impractical. "evil" would be executed every time.

Quote from: Seox on July 06, 2009, 09:05:47 pm
Yeah, I LOVE ternary operators. They also sound cool as hell.


I prefer "C ? A : B"  in short statements only.
Title: Re: Is there a way to....
Post by: fugibo on July 07, 2009, 09:24:57 am
I meant evil would be like

def evil
  if @morals == :wrong
    kill_people
    return true
  end
end


"evil" would then return nil (which acts like false when you use "if <var>") unless you are evil. It would execute, but it wouldn't do anything evil.

And lol, why didn't I think of ternary? I use it all the freaking time :P
Title: Re: Is there a way to....
Post by: Blizzard on July 07, 2009, 09:26:37 am
That's a very inconsistent concept. -_- The cohersion of your methods is very bad.
Title: Re: Is there a way to....
Post by: fugibo on July 07, 2009, 01:23:21 pm
Quote from: Blizzard on July 07, 2009, 09:26:37 am
That's a very inconsistent concept. -_- The cohersion of your methods is very bad.


That's why I called it impractical, lol. :P
Title: Re: Is there a way to....
Post by: Blizzard on July 07, 2009, 04:25:09 pm
And that's why I don't understand you going on with that idea. When I come to the conclusion that something is impractical, I stop thinking about it.
Title: Re: Is there a way to....
Post by: Seox on July 07, 2009, 04:31:44 pm
Quote from: Blizzard on July 07, 2009, 04:25:09 pm
And that's why I don't understand you going on with that idea. When I come to the conclusion that something is impractical, I stop thinking about it.


Hey Blizz. Breathing is impractical.

XD

Seriously though, at first, I really didn't think about "good coding" and "bad coding." You guys've really helped me to learn to NOT be a lazy-ass, both in and out of this thread. Thank you ^_^