How to make a character selection in a 2D Platformer that collects coins

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By GrandeHolt

For each coin I need it to connect a signal to each character in a level, I already made a character selection but how do I put a character in a level while the other characters don’t come up?

:bust_in_silhouette: Reply From: Gluon

I am not sure I fully understand your question so if this isnt the answer you want I apologise. If I have understood correctly you are asking how you can keep a track of the number of coins collected when it is different nodes acting as characters to collect those coins?

If this is the case then the answer is an autoload script. If you go to Project>Project Settings > Autoload then in here you can add a script as autoload. so if you create a script called “COINS” and add a variable to it called coin_count then this will always load whenever you load another scene. This means all scenes can connect to it so when character a collects a coin they add 1 to coin_count in the COINS script and when character b loads they can see the same number and can add to it as well. Hope this helps.

My apologies for the confusion, my problem is when creating a character selection, I needed to select a character but I dont know how to put the character in a scene. Regardless of the question your answer was still helpful, now I dont have to manually connect a signal to each character in a scene Thank you!

GrandeHolt | 2022-12-25 09:20

Oh okay so if you create separate scenes for each of your characters then you can include them as a scene within a scene. If its a character selection scene though you probably wont want to include the players movement script etc so probably best to have a separate sprite with just the characters sprite on and then put an invisible button over the top of each of these characters so when the player clicks the character they are actually selecting the invisible button. You can then attach code to the button and can use an autoload script to store the choice the player made, then in the next scene load whichever character they selected.

Gluon | 2022-12-25 09:56

The part where I am confused is how do I add the character in the world?. I have a character selection made, this is the code:

 func _process(delta):
match Game.PlayerSelect:

 func _on_Left_pressed():
if Game.PlayerSelect > 0:
	Game.PlayerSelect -= 1

 func _on_Right_pressed():
if Game.PlayerSelect < 8:
	Game.PlayerSelect += 1

 func _on_Play_pressed():
get_tree().change_scene("res://Level 1.tscn")

In the Level 1 Scene, I am confused on how to put my characters in that level, or maybe I just need a more better script than this?. the struggle I had was putting all the character in Level 1 but wanted the other character to disappear when I selected a certain Character. My apologies,I am a newbie

GrandeHolt | 2022-12-25 14:23

No need to apologise. To do this what you would want to do is add your character dynamically. To give an example below (I have assumed you have a global script called foobar which holds an integer relating to each character and a position 2d node where you want the characters to start);

const Char1 = preload("res://Path/To/File/Character1.tscn")
const Char2 = preload("res://Path/To/File/Character2.tscn")
const Char3 = preload("res://Path/To/File/Character3.tscn")

func _ready():
    var char
    if foobar.character == 1:
        char = Char1.instance()
    if foobar.character ==2:
        char = Char2.instance()
    if foobar.character ==3:
        char = Char3.instance()
    char.global_position = position2d.global_position

what the above does is it preloads all the character scenes, then checks an autoload script where you have stored which character the player choose. Once that is done you can then add an instance of that character to your level as the one chosen by the player.

Gluon | 2022-12-25 14:42

FYI when your coding gets a little better there are tricks you can use to have less code but I did it this way to make it very clear what is happening in the code.

Gluon | 2022-12-25 14:43

I actually don’t know what a foobar was until now and I actually dont have one, what do i put in the script in foobar?.

GrandeHolt | 2022-12-25 14:53

Thank you! This obstacle is the one that puts me on a stop because I have no idea on what to do, there’s also less tutorial about it

GrandeHolt | 2022-12-25 14:54

foobar is just a name I chose for a global script, its a meaningless word used often in coding to just mean “a thing”. For foobar just replace the name of any autoload script you use to hold the character chosen and for character just change it to any variable you have set to hold the character chosen.

Gluon | 2022-12-25 14:55

Oh for some tutorials you might want to take a look at the official documentation below. There are several tutorials on the basics of building a game

Your first 2D game — Godot Engine (stable) documentation in English

Gluon | 2022-12-25 14:56

Thank you so much for your help! I’ll dig deep in on what you said and go for trial and error, the tutorial also means a lot. Thank you!

GrandeHolt | 2022-12-25 15:01

No problem, it can be challenging to build games but I hope you keep at it, its a great way to be creative with programming. Merry Xmas and happy new year!

Gluon | 2022-12-25 15:03

Merry Christmas and Happy new year to you too!! <3

GrandeHolt | 2022-12-25 15:04