Read from 2 different Arrays at the "same time", problem

0 favourites
  • 6 posts
From the Asset Store
Adjusting the game screen for different resolutions (Letterbox scale)
  • Hi Guys!

    I'm not sure how to explain this - or if I'm posting under the right topic/headline. So I posted here in Construct 2 General.

    I'll try and do my best to be as concrete and simple as possible.

    First my problem.

    Construct wont read values from two separate arrays "at the same time".

    What I have is

      a 2 Player game. Player 1 has an own dedicated Array Player 2 also has an own dedicated Array The Player(s) has steering and can be controlled "RotateLeft" , "RotateRight" , "Forward" , "Backward" The Player(s) dont use the bullet behaviour to move or to rotate this is important. So forth I have 16 cells with static values in an Array 0-15, they contain the Angle the player should rotate to. This is used to make a "Direction-Based Sprite Animation" ...Like old school games that has a separate animation-sprite that faces and "walks" in the direction the player is moving. I'm using the array to store the angle the player should rotate to - in 22.5degrees increment . Like 0 degrees, 22.5 degrees, 45, 67.5, 90 degrees and so on. When "RightArrow" are held down - a Function are Called that read a value from the Players dedicated Array. And use that value to set the players angle. ( I explain more further down) The Player object and the Array object are in a container

    The thing is.

    ...And the function works like this:

      I'm holding down "RightArrow" The Keyboard event calls a function: Function Call "SteerRight" ("Player1") I'm sending Player1 along as a parameter. In the function "SteerRight" the first event are a Pick by Comparison, and the player object gets picked that has "Player1" in an Identification variable. Then immediately I set the players angle by reading a instance variable from the player... Player.State and using that to choose which cell from the players Array that should be read. Like this... Set Angle to ArrayPlayer.At(ArrayPlayer.State) After that I have an Event that adds 1 to another variable called KeyDown When KeyDown are Greater than 10 ...I add 1 to the Player.State variable. Then if Player.State are Greater or Equal to 16 it reset the Player.State to 0. So the state starts back at state zero. A full 360degree circle. Thats the function... that gets called constantly as long as I hold down the right arrow-key.

    This works perfectly with ONLY one player.

    The player rotate both clockwise and cclockwise, I can move forward (thats another function with no array) and move backward.

    ALSO when I use the Player2 controls ( WASD) it also works perfectly.

    I have checked the Arrays in the Debug layout and everything looks fine. The Player1 Array are read independently from the Player2 array. I've checked the variables and every instance of the objects and so on. It all looks fine.

    Now here's my problem

    If I hold the right arrow-key down (rotateClockwise) AND the D-key (rotateClockwise for Player2) down simultaneously - the Player1 and Player2 should both rotate clockwise. But they are not.

    Only one of the players do. And the other players Array does not get read at all.

    It's like one array gets priority over the other or something?

    This is a big problem, cause you kind of wanna be able to steer both the players at the same time - otherwise its not a 2 player game

    The big strange thing is that if I use traditional ways to rotate the players IT WORKS???

    I tried this because i first thought you could not call the same function at the same time like this. But YOU CAN.

      If I instead have the function "SteerRight" And inside I simply have Rotate 2 degrees clockwise. ...Yes I send along the "Player1" or "Player2" parameter and have a Pick by Comparison event first, so either the Player1 or Player2 gets picked. Everything works fine. The Player1 and Player2 both can be steered independently and at the same time with no problems.

    I can have as many players as I want this way.

    But if I have the array in the function and I have to read something from one of the arrays cells, it does not work?

    Probably I could achieve the same result I'm after in a different way, without arrays. But I just wanted to know why I cant use a Function Call like this when there is an Array involved?

    Does anyone know what is happening here in the Construct 2 mechanics or have good ideas of what's wrong?

    I apologize for my wearisomely explanations - I hope someone understands them.

    And thanks for reading this far *HUG*

    If anyone wants to, I can upload a .CAPX in the future.

  • Interesting. Definitely consider uploading a capx, I'd be able to solve this quicker! :p

  • Soulmachine

    I did a simple test with function reading & modify 2 array at the same time (array & player inside container similar to your setup).

    When I press "RIGHT ARROW" & "D" simultaneously, the function can read & modify both array at the same time just fine.

    I attach the capx & screenshot, not sure if this will help but I hope it can give you some idea to fix yours.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I've been away for a while in the real world, I forgot about programming for a moment but I'm back now...

    Jase00

    I did two CAPX examples. One with Normal steering without any array, and one with the array, as I constructed it when I wrote this post.

    [attachment=1:moo0bvf1][/attachment:moo0bvf1]

    [attachment=0:moo0bvf1][/attachment:moo0bvf1]

    As you can see, if you try to steer right ( rotate right ) with both the player1 and 2 at the same time, it do not work in the array version. But one player at a time works.

    The controls

    Player1 Arrows: Left, Right, Up, Down. Shoot with CTRL

    Player2 Keys: WASD. Shoot with SPACE

    Potato

    Your example are very interesting - I'm reviewing it now to see If I perhaps could use your technique. THANKS for your effort

    -

    If I don't manage to solve this using an Array I'm considering try to do it in another way some how. However I have so much freedom changing and tweaking the values if an Array could be used.

    In my real project I have different animation for each "state" I also have a lot of other things that depends on in which state and what value the array has. Like aiming and I also have a wind factor in my game.

    I'm creating a ship that sails on the sea.

  • You need to move your "Every 0.05 seconds" outside of function and put it on the main game loop.

    Like so :

  • Thats BRILLIANT

    I was so stuck at this problem. The solution works so nice.

    If you are wondering, I had that every 0.05 seconds to simulate how an old game work, a game that's been my inspiration. Also I've found if I use something like every tick, in some way. The timing gets different depending on how many FPS you have. So I needed a time based something...

    I mean if I add something to a variable every tick - the time it takes gets different if you got 75fps versus if you had like 30fps. Sometimes I use add dt (delta time) instead and have a real time based kind of delay or timer.

    This solves A LOT for me - in my real project. Thanks!

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