Array help (rendering and istances)

This forum is currently in read-only mode.
From the Asset Store
Game with complete Source-Code (Construct 3 / .c3p) + HTML5 Exported.
  • In my game I have an array, 10x10x1 (x,y array). This array can have 3 values :

    0) player

    1) wall

    2) enemy

    ---

    And now I'm stuck. How can I move the player, and move every single '1' basing upon the direction of the player?

    I want the player to move on the array when I press the arrow keys.

    I want the wall to block enemy and player movements.

    I want the enemies to move every turn towards the player (even diagonally).

    How can I find a value in the array (0) and move it?

    How can I find each value in the array (2) and move them independently?

    If someone provides me an example I'll be very happy.

    ---

    Also, how can I render my array with sprites? Rendering the player is easy, but what about walls and stuff? Won't there be a delay rendering every cell?

  • Also, how can I render my array with sprites? Rendering the player is easy, but what about walls and stuff? Won't there be a delay rendering every cell?

    Arrays (in this kind of situation) are usually just used for level loading and saving... the array is called on to load the level, then the array just sits there and does nothing until it's needed again (for instance, to load the next level, to save the level, etc.). Game-play functions usually aren't passed through an array like that in the main game loop. I'm curious as to why you would need to do that. Is this a grid-based movement type of game? If so you should separate the array level-loader/saver and the grid movement.

  • >

    > Also, how can I render my array with sprites? Rendering the player is easy, but what about walls and stuff? Won't there be a delay rendering every cell?

    >

    Arrays (in this kind of situation) are usually just used for level loading and saving... the array is called on to load the level, then the array just sits there and does nothing until it's needed again (for instance, to load the next level, to save the level, etc.). Game-play functions usually aren't passed through an array like that in the main game loop. I'm curious as to why you would need to do that. Is this a grid-based movement type of game? If so you should separate the array level-loader/saver and the grid movement.

    I'm trying to remake a grid-based game with very complex enemy AI.

    For example, Goblins check with the Pythegorean Theorem the best square to reach the player's back (to backstab him) and assing to it a value. If they don't find a better value they move there, unless stopped by something.

    For example, Wraithwings check their distance from the player and from other Wraithwings. With several criteria they decide to flee from the player or attack him rushing.

    See? It's must better to have an array, or if you can, even with an example, explain me how I could do it.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Actually, I think you are really overcomplicating this by wanting to use an array like that. You can track the X and Y co-ordinate in the array of the player and the individual enemy coordinates by variables, more arrays, lists and so on - it's possible - but there's a much simpler way...

    Why not just position three kinds of sprite - Wall, Player and Enemy - in a 10x10 grid? Then to detect walls, you can use ordinary detector sprites, or test if a wall sprite exists at an X and Y co-ordinate in the layout, use ordinary events to move the enemies, and so on. That's much simpler. With arrays you'll end up with a big, fiddly mess of variables, cell co-ordinates, and generally an eventing nightmare. Arrays have their uses - providing the entire engine for a game probably isnt one of them!

  • The reason I say it's inefficient to pass all the level data though an array every cycle is because once the level is loaded you don't really need the array to keep track of walls and floors and such any more. The array is like a blueprint... but once the building is built you don't need to reference the blueprint to walk around in it... you have real-world walls and floors to see and navigate through.

    Imagine the building is your house, and you need to go to the bathroom. Instead of looking down the hall and moving toward the bathroom door, you're looking at the blueprint of your house. Every step you take, you have the contractors tear down your house and build a whole new house around you. As fast as those contractors might be, they really don't need to be doing that, and you don't really need to be looking at your blueprint.

    So toss the blueprint and just walk to the bathroom on your own.

    As for enemies keeping track of the player position and obstacles, you can give each of them a "mini-array." Let's assume, in a grid-based system where each enemy can move one space, that there are nine choices an enemy can make when moving: One of eight spaces surrounding it, or the space it's currently standing on (i.e., do not move). You only need a 3x3 array to cover all movement options. So you run your algorithm to pick the space to move to, and then go on to the next part of the selection, which is checking for obstacles...

    Your enemy has a target space that it wants to move to, so it runs a quick check to see if there is a real-world, physical wall in the way. If there is, do not move (or move to the second best space, etc.). If there isn't, then move to the target space.

    Using a mini array like this is much more efficient that keeping track of the entire playing field, and what's better is it's recyclable. You can clear the array and reuse it for each enemy during the enemy AI routine. Once they have their final target coordinates stored in a pvt variable or whatever, they don't need to reference the array any more. The engine moves on to the movement routine, checks the coord stored for them, and just places them there.

    I know it's not the example you were looking for, and it might not be exactly suited to your purposes, but maybe it'll give you some ideas on how you can craft your own AI/movement. Hope this helps.

    Edit:

    Er, yeah. What Ashley said.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)