Categories
Unity

Best way to get variable from another script C# unity

public class Bool_Script_To_Access : MonoBehaviour
{
    public bool Smashed_Ant = false;
}
public class Script_To_Do_Stuff : MonoBehaviour
{
    public GameObject ant;
    private Bool_Script_To_Access bool_script;
    private void Start()
    {
        bool_script = ant.GetComponent<Bool_Script_To_Access>();
    }
    private void Update()
    {
        bool_script.Smashed_Ant = true;
    }
}

This is assuming that the two scripts are attached to two separate game objects. Now, in the inspector, select the game object which is attached to Script_To_Do_Stuff. Then, drag the game object which is attached to Bool_Script_To_Access to where it says “ant.” Done. This will work for most cases you’ll have. If you need more general reference to a game object it gets more difficult, and you might want to look into singleton approach.

You might check out the first 15 minutes or so of https://unity3d.com/learn/tutorials/topics/scripting/persistence-saving-and-loading-data
where he creates a GameControl which stores variables that can be accessed from any script. It is a great video

Ref: https://forum.unity.com/threads/best-way-to-get-variable-from-another-script-c-unity.579322/

Categories
Uncategorized

How to fix command not found : code

Step 1: Locate the vs-code executable file.

Step 2 : Edit ~/.bash_profile or  ~/.zshrc

export PATH="$PATH:/Applications/Visual Studio Code.app/Contents/Resources/app/bin"

Step 3: Restart your terminal to make change and try to open any files with code command again. That’s all!

Hope this help

Categories
Uncategorized

How to store access token securely in React Native

Async Storage#

Async Storage is a community-maintained module for React Native that provides an asynchronous, unencrypted, key-value store. Async Storage is not shared between apps: every app has its own sandbox environment and has no access to data from other apps.

DO USE ASYNC STORAGE WHEN…DON’T USE ASYNC STORAGE FOR…
Persisting non-sensitive data across app runsToken storage
Persisting Redux stateSecrets
Persisting GraphQL state
Storing global app-wide variables

Secure Storage#

React Native does not come bundled with any way of storing sensitive data. However, there are pre-existing solutions for Android and iOS platforms.

iOS – Keychain Services#

Keychain Services allows you to securely store small chunks of sensitive info for the user. This is an ideal place to store certificates, tokens, passwords, and any other sensitive information that doesn’t belong in Async Storage.

Android – Secure Shared Preferences#

Shared Preferences is the Android equivalent for a persistent key-value data store. Data in Shared Preferences is not encrypted by default, but Encrypted Shared Preferences wraps the Shared Preferences class for Android, and automatically encrypts keys and values.

Android – Keystore#

The Android Keystore system lets you store cryptographic keys in a container to make it more difficult to extract from the device.

In order to use iOS Keychain services or Android Secure Shared Preferences, you can either write a bridge yourself or use a library which wraps them for you and provides a unified API at your own risk. Some libraries to consider:

Ref: https://reactnative.dev/docs/security

Categories
React-Native

How to format ISO 8601 date to local date time in moment.js

As your getting date as String, so you need to convert it into Date then you can update formate as you want.

// YOUR_DATE_STRING = 2014-09-08T08:02:17-05:00

const utcDate = moment.utc(YOUR_DATE_STRING).toDate();
const localDate = moment(utcDate).local().format('YYYY-MM-DD HH:mm:ss');
Categories
Android React-Native

Change package name for Android in React Native

I’ve renamed the project’ subfolder from: “android/app/src/main/java/MY/APP/OLD_ID/” to: “android/app/src/main/java/MY/APP/NEW_ID/

Then manually switched the old and new package ids:

In: android/app/src/main/java/MY/APP/NEW_ID/MainActivity.java:

package MY.APP.NEW_ID;

In android/app/src/main/java/MY/APP/NEW_ID/MainApplication.java:

package MY.APP.NEW_ID;

In android/app/src/main/AndroidManifest.xml:

package="MY.APP.NEW_ID"

And in android/app/build.gradle:

applicationId "MY.APP.NEW_ID"

In android/app/BUCK:

android_build_config(
  package="MY.APP.NEW_ID"
)
android_resource(
  package="MY.APP.NEW_ID"
)

Gradle’ cleaning in the end (in /android folder):

./gradlew clean

Ref: https://stackoverflow.com/questions/37389905/change-package-name-for-android-in-react-native

Categories
Firebase

Firebase cloud functions deploy only one function

Assumed you have defined your function like this.

exports.yourfunction = functions.https.onRequest(async (req, res) => {
  functions.logger.info("Hello logs!", { structuredData: true });
});

Deploy your function to google cloud with command: firebase deploy –only functions:function_name

firebase deploy --only functions:yourfunction

You should see the completed deploy result like this.

=== Deploying to 'xxxxxxxx'...

i  deploying functions
i  functions: ensuring required API cloudfunctions.googleapis.com is enabled...
i  functions: ensuring required API cloudbuild.googleapis.com is enabled...
✔  functions: required API cloudbuild.googleapis.com is enabled
✔  functions: required API cloudfunctions.googleapis.com is enabled

...

✔  functions[yourfunction(us-central1)]: Successful update operation. 

✔  Deploy complete!
Categories
PHP

How to convert JSON string to array

If you pass the JSON in your post to json_decode, it will fail. Valid JSON strings have quoted keys:

json_decode('{foo:"bar"}');         // this fails
json_decode('{"foo":"bar"}', true); // returns array("foo" => "bar")
json_decode('{"foo":"bar"}');       // returns an object, not an array.

Ref: https://stackoverflow.com/questions/7511821/how-to-convert-json-string-to-array

Categories
PHP

PHP cURL retrieve response headers AND body in a single request

$url = "https://www.googleapis.com";
$ch = curl_init();
$headers = [];
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$body = curl_exec($ch);

// this function is called by curl for each header received
curl_setopt(
    $ch,
    CURLOPT_HEADERFUNCTION,
    function ($curl, $header) use (&$headers) {
        $len = strlen($header);
        $header = explode(':', $header, 2);
        if (count($header) < 2) { // ignore invalid headers
            return $len;
        }

        $headers[strtolower(trim($header[0]))][] = trim($header[1]);

        return $len;
    }
);

$data = curl_exec($ch);
print_r($headers);
print_r($body);

Ref: https://stackoverflow.com/questions/9183178/can-php-curl-retrieve-response-headers-and-body-in-a-single-request

Categories
SwiftUI

SwiftUi Navigation Bar Back Button disappears after entering the third view

The .toolbar modifier on the NavigationView hides the Backbutton in a Buggy way!

Solution:

var body: some View {
        VStack (alignment: .leading) {
            ...
        }
        .toolbar {
            // to fix the back button disappeared
            ToolbarItem(placement: .navigationBarLeading) {
                Text("")
            }
            

            ToolbarItemGroup(placement: .navigationBarTrailing) {
                Button("About") {
                    print("About tapped!")
                }
                
                Button("Help") {
                    print("Help tapped!")
                }
            }
        }
    }

Hope this help.

Ref: https://stackoverflow.com/questions/64409091/swiftui-navigation-bar-button-disappears-after-entering-the-third-view-controll

Categories
Android React-Native

No emulators found as an output of emulator -list-avds

I got this error when running react-native app by running the command: npx react-native run-android

error Failed to launch emulator. Reason: No emulators found as an output of `emulator -list-avds`.
warn Please launch an emulator manually or connect a device. Otherwise app may fail to launch

Checking adb device running and the command not found.

adb devices
zsh: command not found: adb

The solution for Mac user is to edit .zshrc by add following:

# fix adb command not found
alias adb='/Users/softmastx/Library/Android/sdk/platform-tools/adb'

# fix No emulators found
export ANDROID_HOME=$HOME/Library/Android/sdk
export PATH=$PATH:$ANDROID_HOME/emulator
export PATH=$PATH:$ANDROID_HOME/tools
export PATH=$PATH:$ANDROID_HOME/tools/bin
export PATH=$PATH:$ANDROID_HOME/platform-tools

Save it and don’t forget to restart zsh for the changes to take effect!

source ~/.zshrc

Ref: https://stackoverflow.com/questions/38577669/run-react-native-on-android-emulator

https://stackoverflow.com/questions/7609270/not-able-to-access-adb-in-os-x-through-terminal-command-not-found