How can I check if the user is playing the game for the first time? - unity3d

I want to show a text for the first time running like "Hi, my name is NAVI." and then when the player will start the game again the next time it will another text for example "Welcome back."

Using player prefs is probably the easiest solution, though definitely not the only one*.
Due to the lack of boolean playerpref I usually opt to use SetInt/GetInt instead, but that's personal preference. You could also do it with a string or float.
private void Start()
{
if(PlayerPrefs.GetInt("HasLaunched", 0) == 0)//Game hasn't launched before. 0 is the default value if the player pref doesn't exist yet.
{
//Code to display your first time text
}
else
{
//Code to show the returning user's text.
}
PlayerPrefs.SetInt("HasLaunched", 1); //Set to 1, so we know the user has been here before
}
Playerpref stores values between sessions, so the next session will still have "HasLaunched" set to 1. This is however stored on the local device. so users can manually reset it if they'd really want to.
If you ever want to show the "first launch" text again, simply set HasLaunched back to zero, or delete it altogether with DeleteKey("HasLaunched");
*There are plenty of alternative solutions, like storing it in a config file locally, or using a remote database (this would be required if you don't want users to be able to reset it). They would however come down to the same principle. Setting a value to true or 1 somewhere, and checking that value on launch.

Related

How to make a scene remeber an int value Unity

I have a simple project. A button adds points one at a time to a counter. I then change the scene via another button and return to the original scene. The point counter has gone back to zero. How do i prevent this?
Many ways to achieve this. But for starters you can save the number in Player Prefs.
"PlayerPrefs` is a class that stores Player preferences between game sessions. It can store string, float and integer values into the user’s platform registry"
So forexample this is your button callback function:
public void ButtonCallback()
{
number++;
PlayerPrefs.SetInt("MyNumber", number);
}
To get the number next time you are in the scene, you can get the number using:
int number = PlayerPrefs.GetInt("MyNumber");
You will probably need to check if number exists in the first place when you launch the scene first time using:
PlayerPrefs.HasKey("MyNumber");
Read up more on this on:
https://docs.unity3d.com/ScriptReference/PlayerPrefs.html

Saving a score to firebase with attached values

What I'm wanting to accomplish is save a score to firebase that has two values attached to it. Here's the code that writes the score to firebase.
func writeToFirebase() {
DispatchQueue.global(qos: .userInteractive).async {
self.ref = Database.database().reference()
if GameManager.instance.getTopScores().contains(GameManager.instance.getGameScore()) {
self.ref?.child("user")
.child(GameManager.instance.getUsername())
.child(String(GameManager.instance.getGameScore()))
.updateChildValues( [
"badge":GameManager.instance.getBadgeLevel(),
"vehicle": GameManager.instance.getVehicleSelected()
]
)
}
}
}
The issue I'm having is when a new score is saved with its values it sometimes overwrites the other scores. This seems to be random and its not when they're the same score or anything like that. Sometimes it will only overwrite one score and sometimes multiple. I'm watching firebase and I can see it being overwritten, it turns red and then is deleted. Sometimes the new score being added will be red and get deleted. The score doesn't need to be a child, but I don't know how to attach values to it if it's not. Any help is appreciated
This issue seems to happen occasionally so I am going to post my comment as an answer.
There are situations where an observer may be added to a node and when data changes in that node, like a write or update, it will fire that observer which may then overwrite the existing data with nil.
You can see this visually in the console as when the write occurs, you can see the data change/update, then it turns red and then mysteriously vanishes.
As suggested in my comment, add a breakpoint to the function that performs the write and run the code. See if that function is called twice (or more). If that's the case, the first write is storing the data properly but upon calling it a second time, the values being written are probably nil, which then makes the node 'go away' as Firebase nodes cannot exist without a value.
Generally speaking if you see your data turn red and vanish, it's likely caused by nil values being written to the node.

Symbol Barcode Reader on_read issue

I have a Moto Mc9096 device, EDMK SDK, VS2008 etc all of the prereq's
I'm having an issue where once I've scanned a barcode it constantly repeats the event. normally when this happens its a flag or status needs changing but there are no obvious settings to stop it reading again.
code below
private void Barcode_Read(object sender, ReaderData readerdata)
{
if (readerdata.Text != null)
{
if (readerdata.Text == "abc")
{
MessageBox.Show(readerdata.text);
}
}
}
Notes
I've tried
bar.Dispose();
bar.Reader.Actions.Flush();
bar.ReaderData.Dispose() ;
with no success. the EnabledScanner is set on form load and off during form close.
My expectation was when the user scans a barcode it fires the read event once.
but it constantly fires after the users first scan.
You might want to check the aimType property, by default it should be AIM_TYPE_TRIGGER but other settings allow a single trigger pull to perform multiple scans (AIM_TYPE_CONTINUOUS_READ) so perhaps that has been changed.
You should have some samples installed by the SDK at file:///C:/Users/Public/Motorola%20EMDK%20for%20.NET/v2.9/SampLauncher2008.htm (by default) that show best practice.

In Hartl's tutorial, Listing 8.49, why is there a "forget(user)" option?

This is in relation to the checkbox that allows users to stay logged in when they close their browser. In an intermediate version, we remembered the user regardless, and now we're checking the params to see if the checkbox was set. This is the line of code that confuses me:
params[:session][:remember_me] == '1' ? remember(user) : forget(user)
Specifically, why are we forgetting the user if params[:session][:remember_me] is 0? Since we have never remembered the user (I think -- I'm a major newbie), wouldn't this work:
remember(user) if (params[:session][:remember_me] == '1')
and make more sense? I tried it and it passes the tests (which are very basic), but it also seems to behave appropriately. But maybe there's some stray variable that's staying set that I'm missing because I don't know what I'm doing.
I am at the exact same point and was wondering about the exact same thing.
And I came to the conclusion: it's only about security.
Because if an user never logs out of your app, an attacker who stole her user_id and remember_token cookies could use them all the time. However if the user eventually logs in on another computer either the remember_digest attribute gets a new value or is set to nil. Either way the attacker gets locked out.
By omitting forget(user) the only time the remember_digest is set to nil is when the user deliberately logs out.
However the version remember(user) if (params[:session][:remember_me] == '1') gives the user the ability to select one "remembered" computer.

Qt: Preferences not restored through signal and slot mechanism

In my Text Editor application, I save the users font format selection as a preference.
Signals and slots are first set up in the constructor, and then the preferences are read as in the code below:
Constructor:
boldAction->setCheckable(true);
italicAction->setCheckable(true);
underlineAction->setCheckable(true);
fontSizeSelector->setCheckable(false);
connect(boldAction,SIGNAL(changed()),this,SLOT(bold()));
connect(italicAction,SIGNAL(triggered()),this,SLOT(italic()));
connect(underlineAction,SIGNAL(triggered()),this,SLOT(underline()));
ReadUserPreferences():
void TextEditor::readUserPreferences()
{
QSettings userPreferences(QSettings::NativeFormat,QSettings::UserScope,ORGANIZATION_TITLE,APPLICATION_TITLE);
this->boldAction->setChecked( userPreferences.value("appearance/bold").toBool() );
this->italicAction->setChecked( userPreferences.value("appearance/italic").toBool() );
this->underlineAction->setChecked( userPreferences.value("appearance/underline").toBool());
//other code.
}
Now, in the readPreferences function, when boldAction->setChecked(true);, shouldn't the text become bold because the signal and slot mechanism has already been defined? If so, then why isn't it working on my application? The bold function itself works perfectly fine.
Is there a better way of doing this than what I'm doing? Thanks for your help
There appear to be two things wrong here.
Firstly, you are connecting to the wrong signals. The changed signal does not pass a value indicating the action's checked state, and triggered is not emitted at all when setChecked is called. You need to connect to the toggled signal.
Secondly, the signal will only be emitted if the checked state has changed. So if the action is already checked and the user preference evaluates to true, nothing will happen. You probably need to take steps to ensure the appropriate default state is set before connecting the signals.