PETE_MW'S PROFILE

Search

Filter

Creating a Sprite Array

author=pete_mw
I'll see what I can do about a crash course on the topic tomorrow: for now, it's midnight and I need to sleep...


Right, here's that promised crash course in variables, scoping, and garbage collection.

Objects and Variables:

In Ruby, each object you create is stored in its own dedicated slot somewhere on the 'heap'. There is never more than one of any given object, although two objects can end up looking almost identical.

Variables don't actually store objects. Instead, each variable points to an object -- that is, it allows you to communicate with the object (Variables in dynamic languages have also been described as being like a label you can tie onto an object). Array elements, hash keys, and hash values all work the same way variables do. This is why this code ends up doing seemingly weird things:

@sprite_array = Array.new
@sprite_array.fill(Sprite.new, 0, 15)


This appears to be trying to store sixteen sprites in an array. However, because you've only created one sprite, and because Ruby doesn't really store sprites in an array, what actually happens is that you end up with an array with sixteen elements, all of which actually point to the same sprite.

Instead, you should use this:

@sprite_array = Array.new
@sprite_array.fill(0, 15) {|i| Sprite.new}


This form of Array#fill calls the attached block for each element it modifies, and sets that element to whatever the block returns. That means you're calling Sprite.new sixteen times, so you end up with sixteen sprites.

The reason the first, 'broken' form of Array#fill and friends exists at all, in case you're wondering, is because for 'immutable' objects -- ones that never change -- it doesn't actually make any difference to your code whether you're creating actual copies or simply creating new pointers.

Garbage Collection:

Once created, an object lasts until Ruby decides that there's no chance that you'll ever talk to it. It shouldn't be possible for an object to be garbage-collected while you actually need it. Depending on what kind of variable points to an object, it might be visible under different circumstances.

Constants:

Constants belong to modules, but they are normally globally-accessible, which means they will not be garbage collected. Once you have assigned to a constant, you may not do so again, but constants do nothing to stop you from modifying the objects that they point to.

Local Variables:

Local variables are visible from within the method (or begin...end block) that defined them, and from within any closures (that is, {} or do...end blocks) that method creates. Each time you call a method, a new set of local variables is created.

Instance Variables:

Instance variables belong to an object. Once created, they last for as long as their owner does, and they are visible as long as their owner is the current object. As long as an object is reachable, so are any objects pointed to by its instance variables.

An instance variable is created by prefixing its name with an @ sign.

Global Variables:

Global variables belong to the program as a whole. A global variable is visible from any piece of code within your program, so as long as an object is pointed to by a global variable, it is always reachable.

Class Variables:

A class variable belongs to a class, and is visible whenever that class or one of its instances is the current object.

Hope this helps.

Creating a Sprite Array

author=MakoInfused
You must have been writing this for a long time, because this was solved more than an hour ago. Remember to refresh!


Yeah, I'm used to previewing showing me any new posts as well.

However, your solution has problem, which I noted in my edit.

author=Zachary_Braun
You bring up something I'd like to discuss regarding global variables. When I've attempted to create sprites in the past using the instance-level variable, as soon as the instance is over, the sprites are garbage-collected. I'm still learning, so I'm not sure when instances exactly end. I'm coming from RPG Maker 2003, which required a lot jumping from event to event to common event with "Call Event"s to get this level of functionality.


Right. I would recommend finding a tutorial on Ruby itself, and running through it. You might also want to find a book about the language. I'll see what I can do about a crash course on the topic tomorrow: for now, it's midnight and I need to sleep...

Creating a Sprite Array

Your most important problem is that you're creating an array with no elements and then trying to iterate over each element. Since there are no elements, Ruby won't do anything.

Additionally, this is wrong:

$sprite_array.each {|x| x = Sprite.new}

The problem here is that 'x' isn't part of the array. It's just a name that Ruby lets you use to refer to something.

You shouldn't actually be calling each on your array at all. Probably the best way to do this is to use the array comprehension form of Array.new, which you can find discussed here:

http://www.ruby-doc.org/core-1.8.7/Array.html

Essentially, you want to write something like:

$sprite_array = Array.new(10) {|i| Sprite.new}

Replacing 10 with however many sprites you actually want.

As for the '$' sign, yes, that creates a global variable. They aren't a "waste of memory" as long as you use them, but it does look to me like what you're doing could potentially end up leading to debugging hell.

What are you actually trying to achieve with this?

EDIT:

This:

@thespritemap = Array.new
@thespritemap.fill(Sprite.new, 0, 15)

is also wrong. The problem here is that this only creates one sprite and then sets each element of the array to that one sprite. The correct version would be:

@thespritemap = Array.new
@thespritemap.fill(0, 15) {|i| Sprite.new}

As for '@thespritemap', that would create an instance variable on whatever the current object is when this code actually gets called. I can't tell you whether or not that's actually what you want, because I don't know the context in which this code appears.

Programming Help for a beginner

Just for reference, by 'testing', I mean using ruby -c to verify syntax, and using minitest (which is bundled with Ruby) with appropriate mocks for unit testing. I don't think there's much else you'd be able to do.

Programming Help for a beginner

If you're coming from PHP, Ruby does have a few similarities, but there are also quite a few gotchas -- for example, Ruby's === doesn't do anything like what PHP's === does.

If you have a look at the Ruby website, there are quite a few tutorials listed there. The RubyKoans are pretty good.

Also, don't let your experiences with RPG Maker colour what you think of Ruby as a programming language. For example, Ruby normally has much better error reporting than RPG Maker will give you -- although if you create your scripts outside of RPG Maker, you can use the regular Ruby to test them.

Scripting problem. Not sure what it means.

I'm assuming that you're using RPG Maker VX with this script, right?

In any event, the snippet of code you've posted is completely fine and there's nothing wrong with it. Your error is that it's being sent junk data from another piece of code.

I don't think it's that easy to get a useful stack trace from RPG Maker, so fixing this properly is probably going to be way more hassle than you're prepared to handle.

Discussing Original Classes

How do these look? They're fairly early starts at fleshing out a couple of ideas I have in mind for my current project.

The Enchantress:
Roles:
  • Weapon skills augmented by self-buffs are good at quickly dispatching important targets, and effective at dispatching tough enemies.
  • Enchantments are excellent at protecting the enchantress and her allies, and also helpful for neutralising priority enemies early on.

Skill Examples:
  • Aegis: A potent magical shield. Caps incoming damage, but shatters under extraordinarily powerful attacks.
  • Abjuration: Sequesters an enemy or an ally for two rounds.

Here, I'm assuming that an 'enchantress' is a female who crafts enchantments, rather than the D&D definition. I know that abjuration will be all kinds of fun to balance, but it's at least more interesting than a regular stun effect.

The Songstress:
Roles:
  • Weapon skills augmented by self-buffs are good at dispatching multiple opponents
  • Can use Spell-songs to hinder opponents and aid allies.

Skill Examples:
  • Fury: Inspires rage in the songstress, increasing her damage and allowing her to rampage.
  • Rampage: Triggered by successful weapon attacks while under the effects of fury. Hits random enemies.
  • Majesty: A defensive song. Enemies are awed, causing them to hesitate in their actions. Allies are emboldened, and take less damage when attacked.

The 'songs' here are, for the most part, instant casts with a short duration. They aren't really songs in their own right, just elements that can be woven into a song.

I've never played Final Fantasy X-2, so I don't really know what its interpretation of a 'songstress' was like.

Alchemist: Naia

You can require in the RTP by adding this:
rtp=RPGVXAce

to game.ini

which is what I did to get the game running.

I would've tried just modifying the project itself, but it doesn't seem like you can use the full version of Ace on a lite project.

Alchemist: Naia

I forgot about it when I first made that post, but I also ran into a missing resource error when I tried to start a new game.

[Poll] What type of Encounter Type is better in your opinion?

People can do stupid things with any idea. It usually isn't the idea's fault when it happens. Having random encounters every two or three steps and no access to suppression gear is just the same thing as sticking six touch encounters in a 1 x 13 linear corridor.

If encounters are present in a particular part of the game at all, there should be a reason for them, and the best way to handle them is whatever best fits that reason.

If the MacGuffin is unstable, you can use random encounters to ram the point home and help establish the urgency of the situation. If the player needs to sneak into an enemy camp, you can use LOS encounters to represent enemy patrols.
Pages: first prev 123 next last