SOLVED Unlocking Characters

Hi! I need help with doing Unlockable characters. In my game i have a character selector, it works perfectly, and a global var who its supposed to, when the score is >=200 the pj2 is unlocked, but doesn't work at all and the pj2 still locked. Any ideas to do the unlockable characters?
 
How do you determine whether a character is locked or unlocked?
There is an object called "selector", and when i put the selector in the character selector (a room), the creation code of each selector have if(global.[charactername] = true) makes a variable called CHAR1, 2, 3 = true. If CHAR 2 = true it means the character selected is 2, and makes spawn the instance pj_2. So, the global.[charactername] determines if the character is avaliable or not.
 
There is an object called "selector", and when i put the selector in the character selector (a room), the creation code of each selector have if(global.[charactername] = true) makes a variable called CHAR1, 2, 3 = true. If CHAR 2 = true it means the character selected is 2, and makes spawn the instance pj_2. So, the global.[charactername] determines if the character is avaliable or not.
I writed if score >= 100, for example, makes the var global.[charactername] = true, but it didnt work at all
 

TsukaYuriko

☄️
Forum Staff
Moderator
I writed if score >= 100, for example, makes the var global.[charactername] = true, but it didnt work at all
All I can tell you about this is that, when implemented correctly, this will work... which means it wasn't implemented correctly, but to tell you why that is, we'd have to see it. ;)

Please post the actual code instead of paraphrasing it and include where said code is located.
 
Ok, so i have this obj_unlock, who is supossed to verificate if the character is unlocked or not. In "create" it has
GML:
global.pj2 = false;
global.pj3 = false;
And in "step" it has
Code:
if (score >= 200)
{
    global.pj2 = true;
}

if (score >= 300)
{
    global.pj3 = true;
}
And the selector (the obj who defines what character do you gonna play) has in "create"
Code:
global.Character = 0;
variable_global_get(global.pj2);
variable_global_get(global.pj3);
I think the error is, when the unlocks object creates, overwrites the information from step. The unlocks object its in 2 different rooms, the game and the main menu. The character selector only imports the info from the menu.

PD: The pj2 and the pj3 are different characters, obviously.
 

TsukaYuriko

☄️
Forum Staff
Moderator
Ok, so i have this obj_unlock, who is supossed to verificate if the character is unlocked or not. In "create" it has
GML:
global.pj2 = false;
global.pj3 = false;
[/CODE]

...

I think the error is, when the unlocks object creates, overwrites the information from step. The unlocks object its in 2 different rooms, the game and the main menu. The character selector only imports the info from the menu.
Putting that in the Create event indeed means this code will run every time an instance of this object is created. If you have multiple instances of it in different rooms, or a single instance that is not persistent in a room that is visited more than once and is also not persistent, you'll be periodically resetting the unlock status (and if you also reset the score, the Step code will not be setting them back to unlocked).

I suggest moving this to a script, as that will run exactly once at the start of the game (2.3+) or to the Game Start event of an instance of an object in the first room of the game that is not visited more than once (<2.3).


Code:
variable_global_get(global.pj2);
variable_global_get(global.pj3);
These two lines do absolutely nothing. What was the intention here?
 
Putting that in the Create event indeed means this code will run every time an instance of this object is created. If you have multiple instances of it in different rooms, or a single instance that is not persistent in a room that is visited more than once and is also not persistent, you'll be periodically resetting the unlock status (and if you also reset the score, the Step code will not be setting them back to unlocked).

I suggest moving this to a script, as that will run exactly once at the start of the game (2.3+) or to the Game Start event of an instance of an object in the first room of the game that is not visited more than once (<2.3).



These two lines do absolutely nothing. What was the intention here?
Nice! Where do I run the script? In "create" of the obj_unlocks?
 

TsukaYuriko

☄️
Forum Staff
Moderator
You do not run the script. The script runs YOU... uh, itself, at the start of the game.


This was changed in 2.3.
 

TsukaYuriko

☄️
Forum Staff
Moderator
Any code you put into a script - and by that I mean the Script asset type, not to be confused with Object - will run at the start of the game at global scope.
 
Really thanks! I will try it.
I have an error, it seems the obj_selector doesnt receive the state of the global.pj2


FATAL ERROR in
action number 1
of Create Event
for object obj_selector:

global variable name 'pj2' index (100014) not set before reading it.
at gml_RoomCC_character_select_1_Create (line 1) - if global.pj2 = true
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_RoomCC_character_select_1_Create (line 1)

I made this script
GML:
global.pj2 = false;
global.pj3 = false;


if (score >= 200)
{
    global.pj2 = true;
}

if (score >= 300)
{
    global.pj3 = true;
}
 

TsukaYuriko

☄️
Forum Staff
Moderator
Scripts only run once at the start of the game. This means that the unlock will not be running more than once. You'll have to put that part in something that repeats, or something that is called every time the score increases.

Just to make sure - what version are you on? Everything I've provided assumes you're on the latest version.
 
Scripts only run once at the start of the game. This means that the unlock will not be running more than once. You'll have to put that part in something that repeats, or something that is called every time the score increases.

Just to make sure - what version are you on? Everything I've provided assumes you're on the latest version.
I'm in the 2.2.5.378
 
Scripts only run once at the start of the game. This means that the unlock will not be running more than once. You'll have to put that part in something that repeats, or something that is called every time the score increases.

Just to make sure - what version are you on? Everything I've provided assumes you're on the latest version.
How do I do something that is called every time the score increases?
 

TsukaYuriko

☄️
Forum Staff
Moderator
I'm in the 2.2.5.
Then you either have to update or do things the old way (and specify that you're using an outdated version when posting topics), either via the Game Start event as described above or the room creation code of a room that is visited only once at the start of the game.

How do I do something that is called every time the score increases?
Rather than incrementing score manually to add score, make a function (in 2.2 that would still be called a script) that does so and then checks the unlock criteria.
 
Then you either have to update or do things the old way (and specify that you're using an outdated version when posting topics), either via the Game Start event as described above or the room creation code of a room that is visited only once at the start of the game.


Rather than incrementing score manually to add score, make a function (in 2.2 that would still be called a script) that does so and then checks the unlock criteria.
It doesnt work, i tried executing the script in the creation code of the 3 rooms, and works in the same way that the obj_unlocks did, so it didnt work. And you can do an example for the score script? I dont know how to do it, and how do i call it.
 

FrostyCat

Redemption Seeker
It doesnt work, i tried executing the script in the creation code of the 3 rooms, and works in the same way that the obj_unlocks did, so it didnt work.
You know full well that your unlocks are failing to last because you are locking them again whenever an instance of obj_unlocks sets up. Why then do the same crap using Room Creation Code?

Put this in the Room Creation Code of the first room ONLY. DO NOT put this in subsequent rooms. DO NOT repeat it in the Create event of obj_unlocks.
GML:
global.pj2 = false;
global.pj3 = false;
And you can do an example for the score script? I dont know how to do it, and how do i call it.
Read what TsukaYuriko said again. She said to create a script that:
  • Increases the score by a given amount
  • Checks the score and unlocks accordingly
So do as she says and create a script called increase_score (GMS 2.2 syntax, not applicable to GMS 2.3+):
GML:
///@func increase_score(n)
///@param n
score += argument0;

if (score >= 200)
{
    global.pj2 = true;
}
if (score >= 300)
{
    global.pj3 = true;
}
Then instead of incrementing score directly, do it through a script call:
GML:
increase_score(5);
 
You know full well that your unlocks are failing to last because you are locking them again whenever an instance of obj_unlocks sets up. Why then do the same crap using Room Creation Code?

Put this in the Room Creation Code of the first room ONLY. DO NOT put this in subsequent rooms. DO NOT repeat it in the Create event of obj_unlocks.
GML:
global.pj2 = false;
global.pj3 = false;

Read what TsukaYuriko said again. She said to create a script that:
  • Increases the score by a given amount
  • Checks the score and unlocks accordingly
So do as she says and create a script called increase_score (GMS 2.2 syntax, not applicable to GMS 2.3+):
GML:
///@func increase_score(n)
///@param n
score += argument0;

if (score >= 200)
{
    global.pj2 = true;
}
if (score >= 300)
{
    global.pj3 = true;
}
Then instead of incrementing score directly, do it through a script call:
GML:
increase_score(5);
Wha-. Did you just illuminated me? Really thanks, when i get home i will test it and tell the results here.
 
You know full well that your unlocks are failing to last because you are locking them again whenever an instance of obj_unlocks sets up. Why then do the same crap using Room Creation Code?

Put this in the Room Creation Code of the first room ONLY. DO NOT put this in subsequent rooms. DO NOT repeat it in the Create event of obj_unlocks.
GML:
global.pj2 = false;
global.pj3 = false;

Read what TsukaYuriko said again. She said to create a script that:
  • Increases the score by a given amount
  • Checks the score and unlocks accordingly
So do as she says and create a script called increase_score (GMS 2.2 syntax, not applicable to GMS 2.3+):
GML:
///@func increase_score(n)
///@param n
score += argument0;

if (score >= 200)
{
    global.pj2 = true;
}
if (score >= 300)
{
    global.pj3 = true;
}
Then instead of incrementing score directly, do it through a script call:
GML:
increase_score(5);
I have a question, do the whole game will save the information that the global.pj2 = true? Because when you die you can retry or exit to the menu, this wouldn't make, when you go to the main menu, the information in false?
 
I have a question, do the whole game will save the information that the global.pj2 = true? Because when you die you can retry or exit to the menu, this wouldn't make, when you go to the main menu, the information in false?
Is like i said. The first room is the menu, and when you score 200 points, nothing happens. I think its doing the same thing like before. How do I change the variable to true without resetting it.
 

TsukaYuriko

☄️
Forum Staff
Moderator
I have a question, do the whole game will save the information that the global.pj2 = true? Because when you die you can retry or exit to the menu, this wouldn't make, when you go to the main menu, the information in false?
The game stores whatever information you tell it to store, whenever you tell it to store it.

If you set a global variable to false in the room creation code of a room, it stays false until you change it.
If, after it has been set initially, you change its value to true, it will now be true.
If you then re-enter this room, it will be set to false again because that's what your room creation code told it to do.

Is like i said. The first room is the menu, and when you score 200 points, nothing happens. I think its doing the same thing like before. How do I change the variable to true without resetting it.
If you tell it to set the variable to false when entering the menu, it will do that without fail. If you don't want it to do that, don't tell it to do that.

Hence my suggestion of declaring variables "in the first room of the game that is not visited more than once", and I mean every part of that literally.
If you have such a room in your game, put the initial declaration in that room's room creation code.
If you don't have such a room in your game, make such a room - as in, an empty room that does nothing aside from running variable declarations and then going to the next room - and never return to it after leaving it.

This method is foolproof and guarantees two things:
- that this code will run before all other code (so that the variables in question exist by the time they are referenced elsewhere)
- that this code will only run once (so that the variables in question don't ever reset)
both of which are vital for what you're trying to achieve.
 
Top