Camera tracking a variable

I’ve been making a relatively complex game recently which involves switching between players. These players each have events linked to them which makes a single camera follow them. As of now, my code is along these lines:
If global variable ‘characterused’ = ‘player’ Move camera to object ‘player’ with a force of 280 etc…
If global variable ‘characterused’ = ‘greenplayer’ Move the camera to object ‘greenplayer’ with a force of 280 etc…

I was wondering if it would be possible instead to do this:
When a new player is selected, set a global variable, which could be called ‘characterused’ to that player’s name.
Then have a constant event which moves the camera to object GlobalVariableString(characterused)

That way, it would be much easier to implement more characters without having to write a lot of code.
Thanks for your help,
Blooper :slight_smile:

Would be easier to create a single Objet named “Character”, add inside a variable labelled “player”, initialized to 99.


while i < numberOfPlayers create an objet Character if Character.player = 99 set its variable "player" to i set variable i + 1 end while

Player 1 will be 0
Player 2 will be 1
Player 3 will be 2

With that, you set an event like this one

If characterused=Character.player   Move camera to Character with a force...

Okay thanks for your help. I’ll try that and let you know if I need any more help with it :smiley:

[Edit] Could you please explain the first part of the code? What is ‘i’? etc

“i” is a counter variable,

while "i" < numberOfPlayers: ........ variable of player = "i" ........"i" + 1

“i” = 0, then “i” = 1, “i” = 2, etc. So every player will have different values in their variables, “i” determine when the while will be stopped too.

Okay I understand that now :slight_smile:
Is there any particular way that I have to use i?
Do I set it as a variable or is it a function…?

[Edit] Oh, I get it now. i is just a label for the process of adding up each character’s number.

Could you please re-create this in GD and screenshot it please? I’m having trouble setting up those events :confused:
I can’t seem to add multiple events to the ‘while’ block.

Just click “Add sub-event” under a “while” event.

Wow. I’m such a muppet ;D
Thanks guys!

Sorry guys, I’m still having trouble with this one.
I dunno, maybe if I fully explain my game to you, we can make more progress :smiley:

Let’s say I have 2 characters:

I want to be able to switch between them in-game and control them individually with the camera following the one that I’m currently using.
The code I have is as follows:

No conditions        Do =GlobalVariableString(characternumber) to the text of characternumber

Global variable CharacterNumber is =0        Do ="player" to the text of global variable CharacterUsed
The text of the global variable CharacterUsed is ="player"
        No conditions        Rotate player to MouseX();MouseY()
        w key is pressed    Do -0.6 to Y position of Player
                            Do =1 to global variable Moving
Then the same but for A/S/D

Now I have the same for ‘GreenPlayer’ but it’s when the Global Variable CharacterNumber is =1
I have an invisible object Called ‘PlayerTracer’ which follows whichever character is being used. The camera is centered around ‘PlayerTracer’, which moves towards the player in use.

What I want to do is have a system where I don’t have to keep replicating code for each player I have (which will be around 6 per level).
That’s why I wanted to know if it was possible to center the camera around a player whose name is stored in a variable instead of the way I have it now.

Hope this helps us,

If both players are the same object (from object Player you create two instances “Player”) it should be easy…
-Create a variable “ID” for each Player, ID = 1 for Player 1 and “ID” = 2 for Player 2
-Create a scene variable “ActualPlayer” = 1

Event to change players, for example:

"E" key is pressed: ........Action: variable "checkNext" = 1 ........If variable "ActualPlayer" = 2: ................Action: variable "ActualPlayer" = 1 ................Action: variable "checkNext" = 0 ........If variable "ActualPlayer" = 1 and variable "checkNext" = 1: ................Action: variable "ActualPlayer" = 2
or something like this (variable “checkNext” to know if the next sub-event should be executed)

Events related to active Player needs the condition:

Player.Variable(ID) = variable "ActualPlayer"

Okay that worked fine thanks ;D
Thanks for your help there guys!

Okay thanks. I’ve completely sorted it now :smiley:

Thanks for the help guys!