Jump to content
Sign in to follow this  
  • entries
    3
  • comments
    2
  • views
    3,352

About this blog

Follow my amazing tales of...thinking about code. And possibly waffles.

Entries in this blog

 

Awkward RGSS code and how to refactor it.

Ruby is a fantastic language. It can often almost read as plain English, it's not strongly typed so you don't have to worry about declaring ints and strings and floats, the brackets after method declarations are optional, it supports optional parameters and arguments...and there are a million ways to achieve a given task. While this is on the surface a good thing, it means that there are a multitude of ways to accomplish something that are either capable of being coded more efficiently or are just plain inefficient to begin with.   Let's say we have an array we want to set to a particular value, but only if its value has not already been set: if example_array == nil example_array = []end So it takes us 3 lines to do that. It seems like there ought to be a better way. And there is! The first thing is that you don't need to compare with nil in Ruby; there's a built-in method that does this for you. if example_array.nil? example_array = []end Better, but still not quite there. How about utilising Ruby's support for having if statements after the condition? example_array = [] if example_array.nil? Great! But...what if it could be made even better? example_array ||= [] Even better! Ruby has what's called a conditional assignment operator, ||=, which assigns the right-hand value to the left-hand object only if that object doesn't already contain a value.   Okay, so let's take a "sanity check" condition where we want to return immediately if a method's passed-in value isn't an integer. if !example_parameter.is_a?(Integer) return "Parameter must be an integer"end Okay, first of all, that old exclamation mark thing for not conditions needs to go, right? If only Ruby had something that was kind of like an if statement, but did the opposite... unless example_parameter.is_a?(Integer) return "Parameter must be an integer"end It does! Unless is a conditional operator which returns true if the condition is false. In other words, this code will only run if the parameter is NOT an integer. But it could still be better. Oh yeah, we can have conditional operators after their conditions, can't we? return "Parameter must be an integer" unless example_parameter.is_a?(Integer) And you might think that's as efficient as you can make it, but remember when I said that the brackets for parameters and arguments are optional in Ruby? return "Parameter must be an integer" unless example_parameter.is_a? Integer One other cool thing about Ruby is implicit return values. Let's take a simple bit of code that adds up the numbers 1 to 10 and returns the result. sum = 0for num in (1..10) sum += numendreturn sum Ruby automatically returns the last evaluated expression in a block of code, so you don't even need the "return": sum = 0for num in (1..10) sum += numendsum There's something else about this example that bothers me. For loops seem a bit last year. I wonder if there's a better way to do this. sum = 010.times do |n| sum += nendsum Much better! .times is an iterative method that can be run on pretty much any number, and is way better than a for loop.   Let's go back to our original example array. If I want to add the value 5 to it: example_array.push(5) Push is a great method for adding a value to an array, but there's actually a more efficient one if you're only adding a single value: example_array << 5 << does the same thing as push, but for single values it's actually less processor-intensive. It doesn't support multiple values, though.   These are just a few of the little optimisations you can make to scripts. If you've come up with a better way to do something in your own scripting, feel free to mention it in the comments!

Trihan

Trihan

 

Free scripts vs paid ones

There are many kinds of scripter in the community, but they eventually come down to three main categories: those who make scripts for free, those who make them for money, and those who are a mixture of the two, making some scripts for free use and doing paid commissions for people looking for more sophisticated or specific scripts.   I used to be exclusively the former: I never wanted to have to charge people for something when I could provide it merely for the fun of coding something new. Unfortunately, life got in the way and I can't really afford to take time out of my other obligations to do many scripts for free. I tend to focus now on the paid requests that can't be covered by a generic free script, and I make a decent amount of money with it.   I'm hoping that once I've finished studying at college and have a more stable career rather than all the freelance work I do I'll have more free time to dedicate to making scripts for the public at large, but for now I have to prioritise based on what helps pay my bills. It's a difficult line to draw.

Trihan

Trihan

 

A wild blog appears!

I exist! This is totally a thing now, and there are words on it and everything which makes it official.   Some of these words will be about scripting, because I am a scripter. It's funny how that works, but them's the breaks.   Other words will be about nothing in particular, like these ones. And these ones. These ones, too! None of them add anything to the post, but here they are regardless, cluttering the place up with their verbosity.   So what's happening in the scripting world of Trihan? Well I did a couple of commissions over the past couple days, so that'll buy my next takeaway pizza. Thanks, guys! Also college is nearly over, which is nothing to do with scripting unless you count the fact that not having college over the summer means more time to make scripts.   I'm slightly disappointed at the lukewarm reception my Dialogue From File script got. Maybe I just need to put more oomph into my presentation? I'll be using it myself anyway, no more hunting down snippets of dialogue in events for me!   In other news, TOP SECRET THINGS are happening. I'm working on a script I think will be VERY well received, but I don't want to say anything about it yet, so keep your eyes and ears peeled. (maybe not your ears, since forums don't make sounds)   Other than that, not a lot to report on the front of things you actually care about. In 5 days I'll have been with my wife for 5 years, so that's a thing. Also today I ate cake and made a radio show. It was a good day.   If anyone is crazy enough to actually read this and wants a script of some kind, let me know! Maybe I'll make it in exchange for a fat sack of cash money. Maybe I'll do it for free! Maybe I'm secretly the final boss of the game.*   Until the next time I commit random thoughts to paper and unleash them into the uncharted corridors of the interwebs, this is Trihan signing off.   * I'm not.

Trihan

Trihan

Sign in to follow this  
×
Top ArrowTop Arrow Highlighted