• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

RPG style game, ds grids? Or am I on the right track?

F

FunkyB

Guest
Hello, I am a beginner with GM. I just started an RPG (just like FF1 but with a twist). I can now say that without for loops, I would have died long ago from exhaustion.

I have been getting by with establishing global arrays and using for loops. However, I have found that it can get very messy (nomenclature issues). I wonder if I should be using a data structure instead. FYI: I have no experience using data structures, but I will learn if that is the way to go.

For example:

obj_buy_spell

Step Event

//When in the magic shop, and the player clicks on a spell on the shelf to buy, this obj is created.
//This is an obj that the player drags over a character, and left clicks to teach it to him/her.

/*
global.char[ 0, 0 ] = "fighter";
global.char[ 1, 0 ] = "blackmage";
global.char[ 2, 0 ] = "whitemage";
*/

var inst = collision_point( mouse_x, mouse_y, obj_char_parent, false, false );
if( inst != noone ) && ( mouse_check_button_pressed( mb_left ) )
{
//First, find out who you clicked on
for( var i = 0; i < global.party_count; i += 1 )
{
if( inst.name == global.char[ i, 0 ] ) //Ex: obj_fighter.name = "fighter" and global.char[ 0, 0 ] = "fighter"
{
var target = i; //target now equals the char you clicked on
}
}
}

There is a lot more code that follows, but basically I check if the target has or hasn't learned the spell. If he/she has, the spell is learned.

I end up checking arrays against arrays and it gets pretty wild. Am I on the right track? Or should I be taking another approach.

Thank you for reading, and let me know if I left anything unclear.
 

Hyomoto

Member
Short answer, if it works it's fine. Longer answer, this is horrifying. Honestly coding is a case of if it works it works, and if it doesn't work then you need to fix it. If this current method works for you, then it works and it's fine. Something only experience can teach is why you might use another method. And trust me, you can use a million other methods for this. Personally I create a controller object that has gridX and gridY values, then I determine which node is currently highlighted and perform actions based on a switch statement. That's my way of handling things.

Code:
if mouse[ click.left ] == click.released {
    var _node = ( mouse_x - x ) div tileSize + ( mouse_y - y ) div tileSize * gridX;

    switch ( _node ) {
        case 5 :
            parent.parent.editLayer ^= 1;
            break;
           
        case 6 :
            parent.parent.map.showInterior ^= 1;
            parent.parent.minimap.update = true;
            break;
           
        default :
            parent.tool = toolPick[ _node ];
            break;
           
    }
    mouse[ click.left ] = click.handled;

}
This is some code copied and pasted right out of my current project. Because of the way it works, I can reuse this code for any UI element I create, and to utilize it I only have to write the code you see above. That doesn't mean there's nothing else going on under behind the scenes, but this is just to demonstrate that there are lots of ways to accomplish a lot of tasks. So, really what you are asking is if this is a 'good' approach, and by extension, is there a 'better' approach.

First off, if you don't understand my approach it isn't necessarily better because you can't use it anyways. That said, your approach ... is interesting. At it's very worst I would just say it's probably pretty inefficient but you honestly seem to be thinking about what you are trying to accomplish and if your system is working, then it's working. For more advanced techniques you'd be looking into things like scalability. Right now your approach works in this single situation, perhaps you've used a derivative of it elsewhere, but having a unified approach means that you can avoid writing a lot of the same code over and over again. Anything you do more than once, you should probably ask yourself: how can I do this in a general way?

For example, you KNOW the player is clicking on a character. And honestly, if you've placed the instances they are clicking on, you probably already know the order. From that you could probably figure out which one they clicked on by simply doing something like :

Code:
clicked_on = mouse_x div ( room_width / 4 )
This would divide the room up into four columns of equal size, yielding a return of 0 - 3, which probably corresponds directly to your character array. So why bother with messy collision checking and for looping when you can simply check:

Code:
 global.char[ clicked_on, 0 ]
I'm really not sure how much of this you'll find helpful so the key points are if it works, it works, but you might need to ask will it continue to work, and does it always work. If the answer is no, you'll want to rethink it. The last part is that loops are fine but slow, and so if you don't need them you shouldn't use them. The simplest way of doing that is the character you are clicking on could easily know it has index 0 in your global array, so why not do something like:

Code:
char1 = instance_create( 0, 0, character );
char1.index = 0
That way when this instance already knows what array index it should be accessing for it's information, saving you a lot of trouble in the long run.
 
Top