123
-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|655|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
Socoder -> Basic Basement -> Separating Input and Movement

Tue, 21 Jun 2016, 04:48
Dither
Hi all! It's been a while.

I'm doing a top-down 2D game and I have the player object moving around in 8 directions.

As it stands, I have a function that checks to see if the requisite keys have been pressed and, if so, moves the player. All good.

Now I want to add some baddies and get them moving around. The baddies type, like the player, has fields for x and y coordinates, speed and direction.

Since the arithmetic is the same, it occurred to me that it might be nice to have a single function that can be called upon to move any object that needs to be moved (thus separating movement from player input and other functions). But I can't figure out how to make such a function work.

Is it possible to pass an instance of a type to a function and have the function modify the contents of its fields?

I'm writing this in Purebasic but can translate from Blitz Basic/Blitz3D.

Thanks in advance for any suggestions!
Tue, 21 Jun 2016, 05:53
Jayenkai
Depending on how many enemies there are, it might be slightly quicker to keep it as two seperate elements. If you have 100 enemies, that's 100 calls to a function, with at least 2 variable callbacks,(X&Y) that, realistically, you don't need.

But it's probably good for ease of tweaking to only have one instance, instead of finding that Enemy X doesn't collide with the scenery the way that the Player does!!

The way I tend to do collisions, is to create a Matrix array, and fill it with nearby objects.
GetMatrix(GridX,GridY,Player)
vs
GetMatrix(GridX,GridY,Enemy)
Within there do a matrix grid of 3x3 surrounding tiles, with player specific objects showing up for player or not...

Within that, use "collide" checks so that the resulting Global Dim Matrix(X,Y) array has either 1s for "walkable" or 0s for not walkable.

Then you end up with movement code which is only ever checking 1s or 0s within Matrix(), for can or can't move, and you can easily copy and paste that smaller bit between player or enemy because most of the checks are happening in the function.

.. But I'm an oldskool coder, and do things messily, and usually in arrays, so maybe not a good idea to take my advice??!

-=-=-
''Load, Next List!''
Wed, 22 Jun 2016, 04:28
Dither
Thanks for the reply, Jay! I'm not actually dealing with collision checks yet but that's good information, much appreciated. I've read about setting up a grid, in which each space has its own list of contents updated as objects move around.

I did find out how to pass a structured type variable to a function in Purebasic. It is passed by reference and requires an asterisk before the name of the variable standing in for the one passed. Pretty simple and it works the way I'd hoped.