#https://gdl.space/ekipahabob.cpp i am

1 messages · Page 1 of 1 (latest)

cedar kestrel
#

what did you try

clever torrent
#

well i tried the scenedata which you suggested, but it still nothgn changes

cedar kestrel
#

can you show what you wrote

#

the thing i suggested requires more work

clever torrent
cedar kestrel
#

do you know how to use a scriptable object

clever torrent
#

well no, this is my first time

cedar kestrel
#

ok so this is gonna be a little more complicated

#

ill explain it but lemme show you the easy way first

#
public class LevelManager
{
  public static int level1MuonCount;
  public static int level2MuonCount;
}
#

something like that would be a lot simpler

#
[CreateAssetMenu(fileName = "SceneData", menuName = "ScriptableObjects/SceneData")]
public class LevelData : ScriptableObject 
{
    [field: SerializeField] public string SceneName { get; set; }
    [field: SerializeField] public int MuonCount { get; set; }
}
public class LevelManager : MonoBehaviour
{
  [SerializeField] private LevelData level1Data;
  [SerializeField] private LevelData level2Data;
  
  private Dictionary<int, LevelData> _levelDataMap;

  private void Awake()
  {
    _levelDataMap = new()
      {
        {1, level1Data},
        {2, level2Data}
      }
  }

  public int GetMuonCount(int levelIndex)
  {
    return _levelDataMap[levelIndex].MuonCount;
  }
public string GetSceneName(int levelIndex)
  {
    return _levelDataMap[levelIndex].SceneName;
  }
}
#

i mightve spelled some things wrong im typing this in discord

#

in the editor you right click like youre adding an item and at the top of the menu you should a new ScriptableObjects section

clever torrent
#

OK, i hope

#

there sure is some red underlines

#

also the two different scenes are calles "Stage 1-1" and "Stage 1-2"

cedar kestrel
#

you need to add the usings

#

like System.Collections.Generic

#

UnityEngine

#

maybe there are some spelling errors since i didnt write it in an ide

#

but im sure youcan figure that out

clever torrent
#

under new

cedar kestrel
#
[CreateAssetMenu(fileName = "SceneData", menuName = "ScriptableObjects/SceneData")]
public class LevelData : ScriptableObject
{
    [field: SerializeField] public string SceneName { get; set; }
    [field: SerializeField] public int MuonCount { get; set; }
}

public class LevelManager : MonoBehaviour
{
    [SerializeField] private LevelData level1Data;
    [SerializeField] private LevelData level2Data;

    public Dictionary<int, LevelData> Map { get; private set; };

    private void Awake()
    {
        Map = new()
        {
            {1, level1Data},
            {2, level2Data}
        };
    }
}
#

you can make the dictionary static if you want and in your other script where you need the count or whatever you can do LevelManager.Map[1].MuonCount

#

new() is a newer feature idk what version youre on but maybe you can try
Map = new Dictionary<int, LevelData>()

clever torrent
#

there seems to be still a red uenrline on new

#

oh wait, i accidentally put it in the wrong one

#

still having these red underlines

clever torrent
cedar kestrel
#

remove that semicolon

#
Map = new Dictionary<int, LevelData>()
{
    {1, level1Data},
    {2, level2Data}
};
#

im going to bed. what i wrote should work after fix that. after that you need to look up hwo to use scriptable objects

clever torrent
#

the map word still have a red underline

#

this is so hard and i don't understand

gusty jewel
gusty jewel
# clever torrent

Every time there is a red underline, you of course need to share what the error is. But perhaps it has something to do with the syntax error on the line where you declare Map

clever torrent
#

_levelDataMap instead?

gusty jewel
#

No, you just have to remove the semicolon.... the thing that is underlined
I drew a red arrow pointing right to it

clever torrent
#

oh

clever torrent
#

and I don't get how any of it have to do with MuonCounter script...

gusty jewel
#

Also, you still need to show what the error actually says

#

Just showing a red line is not usually helpful

clever torrent
clever torrent
gusty jewel
#

You changed it back to Map = new() for some reason

clever torrent
#

but how can they tell which scene it is?

gusty jewel
#

Well, that is a reference, so you need to drag the scene assets into those references via the inspector I guess

clever torrent
#

oh

#

but i can't drag in any scene in the inspector

clever torrent
#

what am i doing wrong?

cedar kestrel
#

did you fix it

clever torrent
clever torrent
#

if you know what i mean

cedar kestrel
#

i dont

clever torrent
# clever torrent

in the inspector about none (scene data) what am i supposed to add in there?

cedar kestrel
#

did you create instances of the scriptable objects

clever torrent
#

instances?

cedar kestrel
#

did you read about scriptable objects

#

you need to create an instance of them to use it

clever torrent
cedar kestrel
#

watch the video

clever torrent
#

I am

cedar kestrel
#

1:30

clever torrent
#

I have a scriptable script

cedar kestrel
#

ok

#

so did you create the instance

#

you right click in the editor hierarchy like youre creating an asset and go to ScriptableObjects -> SceneData

#

the video explains it

#

you just add your data to that then drag it over into the level manager

clever torrent
#

yet what does have any of it to do with showing how many muons there are from other scenes while it is still not showing how many there are

clever torrent
#

I just don't get it... i am confused...

cedar kestrel
#

this is just for storing the counts

#

to read the counts you will access the level manager

#

on your object that you want to show the count you can do something like LevelManager.Map[1].MuonCount

#

you can also use it for loading scenes like SceneManager.LoadScene(LevelManager.Map[1].SceneName)

#

then you can add more stuff to LevelData if you need ot access it outside the scene

cedar kestrel
#

do your objects that display the count have a script attached

clever torrent
cedar kestrel
#

those icons that show the count

clever torrent
#

it's jsut the text

#

the second 0 is supposed to show how many muons there are in other scenes

cedar kestrel
#

ok so in your LevelManager you can do

private int GetTotalMuonCount()
{
  int count = 0;
  foreach (var item in Map)
  {
      count += Map[item.Key].MuonCount;
  }
  return count;
}
clever torrent
#

the script is called PLyaerManager

#

like this?

cedar kestrel
#

yup

#

then in the script you want to display the count you just do PlayerManager.GetTotalMuonCount()

clever torrent
#

but doesn't it get a red underline there if i put it in there, in the same script?

cedar kestrel
#

oh if its in the same script then just do GetTotalMuonCount()

#

these things you can fix yourself

#

just read what the error is

#

im trying to help but im not sure if youre just really confused or you dont wanna help yourself

clever torrent
cedar kestrel
#

do you understand what the error says

clever torrent
#

yes, it sys that the method must have a return type

cedar kestrel
#

the pictures youre posting aren helpful

#

do you have a function called PlayerManager?

clever torrent
#

the script is called PlayerManager

cedar kestrel
#

can you just show the code

clever torrent
cedar kestrel
#

thats not how you call a function

#

you need to call it inside another function definition or property

#

if youre using textmeshpro you use get a reference to the label

#

like [SerializeField] private TextMeshProUGUI countLabel;

#

then you drag the label into the playermanager field for that

#

then in start you can do countLabel.text = GetTotalMuonCount().ToString()

#

in your start method i mean

clever torrent
#

private void start?

cedar kestrel
#

yup

#

i would recommend you try learning a little bit more about c#. its gonna make your development a lot faster

gusty jewel
clever torrent
#

i seem to have gotten an error

cedar kestrel
#

i have no idea whats at line 297

gusty jewel
#

Line 297 of PlayerManager

cedar kestrel
#

maybe its because you didnt add another reference for the second level

clever torrent
cedar kestrel
gusty jewel
#

Ah yes 🧠
The value at that key is null

clever torrent
#

ok, now it's no logner showing the error, but yet.... it still doesn't showing the amount of muons there....

cedar kestrel
#

you need to add the data for level 2

clever torrent
#

I did

cedar kestrel
#

did you edit the data

#

its not gonna auto update without you writing it in. if you want it to update automatically you need to make an editor script

clever torrent
#

even if i did, it still wouldn't show any numbers

cedar kestrel
#

can you show the code

clever torrent
#

and where the countlabel is

#

also, it's supposed to show how many muons from each scene there is, not to choose a number

https://gdl.space/oruvuroyag.cs and this is the script the UItext have

cedar kestrel
#

also

#

youre not gonna be able to easily just get the count from each scene at runtime so easily

#

youd probably have to open each scene to get the count

#

you dont need to write it in yourself. you can make an editor script to automatically do it for you

#

but thats more advanced and if youre having trouble with this i dont think its smart to go into details about editor scripting

clever torrent
#

but that is not how i wanted it to work, because even in game, you only start a the title screen, then to the menu screen, then to a world select screen, in the level select screen and stage selection screens where it shows how many muons there are from each stage scene and how many of them you have rescued...

cedar kestrel
#

i told you before that this is for a static count

clever torrent
#

I know...

#

yet even though i have static, i don't get why it is still not working...

cedar kestrel
#

no

#

static meaning it wont change

#

meaning this solution works if the count doesnt change in your game

#

i also told you that if it changes you will need to implement some data persistence

#

ok so ill ask this differently

#

you want to have a count of all muons in game, and another count of the ones you saved as "4/10" for example?

clever torrent
#

yeah, and while currently, there are 3 muons in each stage, and there's 5 stages (while on is a travel stage) and the one big stage is a boss stage, which makes 15 together

#

and yeah, like when you rescued all muons in one stage, which makes it "3/15"

cedar kestrel
#

when you close the game is the data supposed to be saved?

#

or do you start over

clever torrent
#

saved

#

so you don't have to start over and over agian then that would be fustrating to rescue a lot of the muons agian

cedar kestrel
#

ok so weve spent a lot of time on this so im just gonna give you a general direction

#

In this video, I show how to make a Save and Load system in Unity that will work for any type of game. We'll save the data to a file in both a JSON format as well as an encrypted format which we'll be able to toggle between in the Unity inspector.

IMPORTANT: For WebGL games, because WebGL can't save directly to a file system, a different meth...

â–¶ Play video
#

then you can use what i gave you to keep track of the muons or delete it. it is useful if you want to add more data about your levels later on but you might not need it

#

you will store a count of the muons in your game, using the system i showed you or just a simple int muonCount = 15

#

youll need a variable like muonsSaved = 0

#

when you save a muon you do muonsSaved += 1

#

then you will use the save system from that video to store the save count even after you close the game

clever torrent
cedar kestrel
#

so just store the save coutn there

#

now the other issue is your count not updating in game

cedar kestrel
#

so in this script you just need to fetch the muon count and save count and update it

clever torrent
clever torrent
cedar kestrel
#

in the persistence you will get the muon remaining count

#

pass that to the script that controls the label

#

at the start of that level change. while youre playing you dont need to access the persistence script if its just gonna read the file again. you can store that in memory

clever torrent
#

..i... just don't know what to write

cedar kestrel
#

in the persistence script when the game loads you get the muon remaining count

#

you save that in memory

#

make a private int muonsRemaining then muonsRemaining = (get it from the file or whatever method youre using)

#

you can make it a public property actually

#

then whenever you go back to that level select scene you can read the property to update the count

clever torrent
cedar kestrel
#

try that and if youre confused you can ask for more help

#

but first try to figure out the steps that i outlined for you

clever torrent
#

what's the difference between loadgame and savegame?

cedar kestrel
#

you save a game when you wanna go back to that point

#

like if youre gonna clsoe the game or reach a checkpoint you save it

#

then load takes you back to that saved state

clever torrent
clever torrent
#

I am getting anxious about it...

clever torrent
clever torrent
cedar kestrel
clever torrent
cedar kestrel
#

is it working?

clever torrent
#

well.......... I don't get how TotalMuonPerLevel and CurrentMuonsPerLevel texts can tell with only just that?

#

since the number on the first number text still wouldn't go up when i touched a muon gameobject

cedar kestrel
#

when you pick it up are you calling PickedUp()

clever torrent
#

well yeah the pickedUpwould be when i touch it

cedar kestrel
#

where is this muoncounter

#

is it in the level select

#

oh its dontdestroyonload

clever torrent
cedar kestrel
#
foreach (string tag in muonTags)
        {
            GameObject[] gameObjects = GameObject.FindGameObjectsWithTag(tag);
            _allMuons += gameObjects.Length;
        }

#

this is only gonna be called on start

#

so this only works when you load the level screen

#

is your text object always in the scene

clever torrent
#

only in the level select scene

cedar kestrel
#

then PickedUp() is going to throw an error

#

whats the issue youre having now

#

do you know if pickedup is actally being run

#

oh i guess it wont because updatetext checks for null

#

so if PickedUp works, when you go back to the level screen the value should still be correct

#

you need to call UpdateText when you load the level select scene again

clever torrent
#

the number should change up number hwne i come back to the level screen, yeah

#

I was told that I can Now store the total and current for each level. Then I can access these dictionaries from anywhere to change the number when I collide with one, or to display as text would access this object and change the values on it whenever you collide with one, but yet i don't know how that would with only just
Dictionary<string, int> TotalMuonsPerLevel = new Dictionary<string, int>();
Dictionary<string, int> CurrentMuonsPerLevel = new Dictionary<string, int>();

clever torrent
cedar kestrel
#

on the counter script

clever torrent
#

oh alright, and it should have private MuonCounter muonCounter;

cedar kestrel
#

the way you have it written you cant access the dictionaries anywhere

#

youre defining them in the start method

#

so they only exist in that method

#

anyway thats not the issue

#

you need to call UpdateText when you load the level select scene

#

you can do that in the counter scripts start method

clever torrent
#

in LoadData or in the start?

cedar kestrel
#

you know ive told you and other people told you when you just post code with a red line its not helpful

#

did you read the error

#

im sure its because UpdateText is a method

#

it should be UpdateText()

clever torrent
#

I was only showing if it's there i should put it?

cedar kestrel
#

change it to public

clever torrent
#

ok, there

cedar kestrel
#

see if it works

clever torrent
cedar kestrel
#

you changed the script so you have to post the code again

#

did you read the errors

#

i dont think youre gonna learn anything if you just ask for help at every problem

clever torrent
#

hnag on, i am on the way

#

ever sine i add in the updatetext, i seems to be getting these two errors

cedar kestrel
#

ok so muonCounter is null

#

and probably _text

clever torrent
#

private MuonCounter muonCounter = null;?

cedar kestrel
#

no

#

the problem is that its null during runtiem so you cant access UpdateText

#

so setting it to null is not gonna do anything

#

its already null

#

you need a reference to the counter

#

did you set it

clever torrent
#

should i reference the counter in counter?

cedar kestrel
#

tbh i dont know whats going on

#

you posted 1 line of the updated code

#

you make it so hard to help you

clever torrent
#

here

cedar kestrel
#

and no offense but tbh im almost not feeling like it anymore because it doesnt seem like youre trying to help yourself and the errors you are posting arent helpful

#

let me explain something

clever torrent
#

before the updatetext was added in the coutner, there weren't any errors, but when i added i nthere, i got errors, even though the errors i get have nothing to do with the muoncounter since it's just for the coins

cedar kestrel
#

when you write private MuonCoutner muonCounter this is just stating that there is a variable of that type and name

#

when you do this its gonna be null

#

so you need to set it to something before you try to access it

#

so before you call UpdateText you need to initialize muonCounter

#

do you understand

clever torrent
#

like this?

cedar kestrel
#

yes

#

good job

clever torrent
#

thanks

#

ok, let me try and see

clever torrent
cedar kestrel
#

in PickedUp but a debug.log to see if its actually being called

clever torrent
cedar kestrel
#

im confused

clever torrent
#

and not only that, number go up seems to be triggering when i start the scene

cedar kestrel
#

you have a class called Counter and MuonCounter and both have a method called PickedUp

clever torrent
#

the pickup in Counter is for the coins

cedar kestrel
#

where is your collision function

clever torrent
#

collission function?, the counters don't have oncollisions

cedar kestrel
#

how are you picking them

#

there should be a function that is called

#

either oncollision or ontrigger

clever torrent
#

the Muoncounter is for countin how many gamoebject muons you have touched, and muons already have their own script

cedar kestrel
#

yea i get that

#

how is the counter gonna know something is picked up

#

you have a pickedup method

#

when the collision happens you should let the counter know or set up an event

#

otherwise the counter is never gonna know to update

clever torrent
#

yeah, wait, was it supposed to be pickedup() instead of updatetext)(?

cedar kestrel
#

what

clever torrent
#

because i just tested that one and the number is finally going up when i touched a muon object

cedar kestrel
#

when the pickup happens you have to notify the counter somehow

#

oh so its fixed?

clever torrent
#

yeah... almost, since when i rescued 3 of the muons in the level scene, the number have only gone up one time, staying at "1/15"

clever torrent
cedar kestrel
#

ok

cedar kestrel
#

then read the start method again

#

and try to figure out why that happened

clever torrent
#

hmmm.... when i play tested, the only thing that is making the number go up is the first muon object with tag name "Muon1"

#

also i am doing the reading and trying to figure out

cedar kestrel
#

try it again and dont pick anything up

#

see what happens

clever torrent
#

well here's the debug when i enter the scene, also bu don't pick anything up.. you mean do not pick any muon objects up?

cedar kestrel
#

yup

#

go back to level select

#

see what happens to the coutner

clever torrent
#

oh wait, a number 1 went up

cedar kestrel
#

do you know why

#

follow the code from the counters start method

#

start is called when the gameobject is set to active, after spawning

clever torrent
#

it's because it went up of when finishing the level

cedar kestrel
#

im gonna tell you this but next time you need to read it yourself

#

when the counter spawns unity calls the start method

#

in your start method you are calling muonCounter.PickedUp()

#

in PickedUp you are increasing the coutn

#

so everytime this counter is loaded youre increasing the coutn

clever torrent
#

ohh....

cedar kestrel
#

do you see that

#

in the code

clever torrent
#

yeah

cedar kestrel
#

ok so change it back to what i told you earlier

clever torrent
#

ok, i have chnaged it back

cedar kestrel
#

start should call UpdateText everytime its loaded, not PickedUp

clever torrent
#

ok

cedar kestrel
#

so we know that PickedUp at least kinda works

#

back to what i said before

#

you need to call PickedUp when the collision happens (when youre actually picking it up)

clever torrent
#

so like... oncollissionenter?

cedar kestrel
#

yeah

#

this isnt what i would say is the best way to do it but it should work

#

lets just keep it simple

clever torrent
#

i always though collission is somehting you touch

clever torrent
cedar kestrel
#

it is

#

im not sure if this is what youre thinking but youre not gonna put the collision method in the counter

clever torrent
#

no

cedar kestrel
#

you can put it in your player script or the muon script

#

assuming your muon script is the one detecting the collision you would put it there

clever torrent
cedar kestrel
#

in OnTriggerEnter2D

clever torrent
cedar kestrel
#

no

#

remember what i told you

#

private MuonCounter muonCounter; is just a declaration

#

you need to initialize it or youll get a null error

#

also, you have some checks there on what the collision object is, so you should add picked up inside that

clever torrent
#

the same as in counter witht he null?

cedar kestrel
#

yup

#

now try it

clever torrent
#

man my game sure is going to be abig project..

cedar kestrel
#

this is the easy part

#

dont get overwhelmed

#

the problem is you dont know much about how code works

#

youll learn while building it

clever torrent
#

first try and second try, the first try when i moved the player around in the scene tab and second try was when i tried in the game tab

cedar kestrel
#

so it works?

clever torrent
#

yeah, i hope so, and now to save the data when rescuing them

cedar kestrel
#

ok good

#

good luck im glad you got it working

clever torrent
#

thank you, now for saving the data on about the text like when you exit the game and play it agian

#

uhhhhh..... ok, now this is a problem right now that sometimes number go by 2 when i touch one muon game object

cedar kestrel
#

post the collision code then im going to bed

clever torrent
#

ok
private void OnTriggerEnter2D(Collider2D collision)
{
muonCounter = FindObjectOfType<MuonCounter>();

    if (muonCounter == null)
    {
        Debug.LogError("MuonCounter not being found");
    }
    else
    {
        muonCounter.PickedUp();
    }

    if (collision.CompareTag("Player") || collision.CompareTag("Player 2") || collision.CompareTag("Attack") && !isRescued)
    {
        isRescued = true;
        animator.SetTrigger("Rescued");
        animator.SetBool("isRescued", true);
        popSound.Play();

        // Start a coroutine to handle the happy animation and disappearance
        StartCoroutine(HappyAnimationAndDisappear());
    }
}
cedar kestrel
#

probably the collision is happening twice

clever torrent
#

sometimes and sometimes not, even sometimes 3 times... i just nee dit to happen once

#

based on what gameobject it is

cedar kestrel
#

in the if statement you set isrescued to true

#

part of the if clause is if its not already rescued

#

so if you add the pickedup inside the if clause it should prevent it happening twice

#

thats the whole point of what you wrote

clever torrent
#

like this?

cedar kestrel
#

yup

clever torrent
#

ok, thank you, man my game sure is going to be big, even with 3 save files to choose from

cedar kestrel
#

youll get it

#

just try to learn from your mistakes and figure out why things work and why they dont work

#

thats the most important part of learning how to code

#

anyway im going to bed