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

Discussion and feedback on Construct 2

Post » Tue May 06, 2014 12:26 am

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.
Soulmachine @Soundcloud
B
14
S
2
G
1
Posts: 63
Reputation: 928

Post » Tue May 06, 2014 7:22 am

Interesting. Definitely consider uploading a capx, I'd be able to solve this quicker! :p
B
51
S
20
G
10
Posts: 571
Reputation: 9,819

Post » Tue May 06, 2014 9:34 am

@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.
You do not have the required permissions to view the files attached to this post.
B
34
S
13
G
8
Posts: 134
Reputation: 8,118

Post » Sun May 11, 2014 7:53 pm

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.
SteeringNormal.capx

SteeringUsingArray.capx


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 do not have the required permissions to view the files attached to this post.
Soulmachine @Soundcloud
B
14
S
2
G
1
Posts: 63
Reputation: 928

Post » Sun May 11, 2014 10:42 pm

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

Like so ;) :
You do not have the required permissions to view the files attached to this post.
B
34
S
13
G
8
Posts: 134
Reputation: 8,118

Post » Sun May 11, 2014 10:58 pm

Thats BRILLIANT :) :) how nice of you, THANKS! :)

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! :)
Soulmachine @Soundcloud
B
14
S
2
G
1
Posts: 63
Reputation: 928


Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 5 guests