#Variables

1 messages · Page 1 of 1 (latest)

viral lark
#

Where are you at?

midnight fossil
#

wdym

viral lark
#

What are you working with so far?

midnight fossil
#

hold on

viral lark
#

👍

midnight fossil
#

this

#

So, I want to make the durability tick down on the tile until it hits 0

#

then the tile is destroyed

viral lark
#

Where's the scriptable object?

candid torrent
#

GetTile takes in a <T>

midnight fossil
#

right now I have the tile inside of the list in the editor

candid torrent
#

GetTile<newTile>(pos)

midnight fossil
#

okay

#

I do need a ref

#

lemme try that

candid torrent
#
var myTile =  tilemap.GetTile<newTile>(pos);
Debug.Log(myTile.durability);
midnight fossil
#

okay so if I want a specific tile

#

from the newTile class

candid torrent
#

use index for array of tiles

midnight fossil
#

Im using tile base

candid torrent
#

tilebase is the base class

midnight fossil
#

newTile[]

#

?

candid torrent
#

all tiles inherit from it so you can drag any tile there including newTile

candid torrent
midnight fossil
#

I see

#

okay

#

I got it to work

#

but I want the durability to pertain to the type of the tyle

#

so if I click on a dirt tile, then it gets one shot since it has one durability
and if I click on a stone tile then I need to click it again since it has 2

candid torrent
#

you have to now make different tiles

#

adjust their numbers accordingly

midnight fossil
#

hold on

candid torrent
#

also store them as newTile[] not TileBase

midnight fossil
#

I did

#

OHHH

#

i sww

candid torrent
#

since you cant do something specific with TileBase

midnight fossil
#

see

#

switch(tiles)

#

thanks

#

or rather

candid torrent
#

make an enum for the tile

#

tiletype

midnight fossil
#

can you elaborate

#
enum tiletype {
dirt,
grass
}
candid torrent
#

i mean yeah if you need specifc thing based on type

midnight fossil
#

I need the durability for each tile type to be different so yeah

#

k lemme implement

candid torrent
#

durability is already set through its variable

midnight fossil
#

so would I add to the enum from the editor

#

or how would this work

candid torrent
#

are you trying to add damage or something on this

midnight fossil
#

yeah

#

so a better pickaxe will break the tile faster

#

does this make sense

candid torrent
#

how the two related

midnight fossil
#
durability -= weapon.strength;

if(durability <= 0) {
  t.SetTile(cellPosition, null);
}
#

I got it working

#

I dont think im doing it very efficiently

candid torrent
#

wdym

#

you prob just need some null checks so it doesn't error if you don't have a tile anymore since it's null

midnight fossil
#

hold on

#

I'm still figuring it out

candid torrent
midnight fossil
#

lol

#

I was about to type that

#

I figured it all out

#

except that now

#

hold on I think I have a fix

#

nvm

#

doesnt work

#

hmmm

midnight fossil
#

nvm

#

I got it

candid torrent
midnight fossil
#

i was gonna do it like its done in terraria

#

where if you switch blocks then the progress resets

#

bc I don't know how to make it individual to each tile

#

is there a way? Assuming it is easy to implement

candid torrent
midnight fossil
#

a dictionary!

#

I didn't think of that

#

I need to use both anyway I just realized

#

hold on

#

but is it inefficient to add a different tiledata for each tile?

#

wouldn't that cause lag on a mass scale

candid torrent
#

🤷‍♂️ if you're constantly iterating through ALL the tiles maybe , I never had issue yet with that

midnight fossil
#

okay

candid torrent
#

there are other ways , each has it's own pros and cons, you're gonna have to experiment and adapt

midnight fossil
#
List<TileData> tileDatas;
dataFromTiles = new Dictionary<TileBase, TileData>

foreach(var tdata in tileDatas) {
  foreach(var tile in tdata.tiles) {
    dataFromTiles.Add(tile, tdata);
  }
}

#

would some loop like this work??

candid torrent
#

nah you should store their tileboard pos and their data

#

you could even add the tiles only when you interact with them as well

#

be mindful what kinda data you truly wanna store anyway

#

most of it should be only readonly anyway

midnight fossil
#

like durability

candid torrent
#

you mean health ?

midnight fossil
#

sure

#

yeah

candid torrent
#

ok

midnight fossil
#

I think this works

#

then I just subtract directly from the dictionary

#

rather than the tile itself

#

thoughts?

#

I have the key and value swapped

candid torrent
#

key should be Vector3Int

#

value should be DataTile or int for just durability

midnight fossil
#

yeah

#

I just realized that walking back

#

but this should work

#

I just have to check to make sure I'm not adding a duplicate

candid torrent
#
  void TileFunct(Vector3Int cellPos)
    {
        // init new data if tile not present in dict
        if (!tilesBoard.ContainsKey(cellPos))
        {
            tilesBoard[cellPos] = new CustomTileData { Durability = tileType.grass.Durability };
        }

        CustomTileData tileData = tilesBoard[cellPos];
        tileData.Durability--;
        // Set the modified value back in the dictionary
        tilesBoard[cellPos] = tileData;

        if (tilesBoard[cellPos].Durability <= 0)
        {
            tilemap.SetTile(cellPos, null);
            tilesBoard.Remove(cellPos);
        }
    }```
#

sum like this should work

#

just an example , has to be change to account for other things

midnight fossil
#

if (!tilesBoard.ContainsKey(cellPos))
{
tilesBoard[cellPos] = new CustomTileData { Durability = tileType.grass.Durability };
}

thats way better than what i was trying

#

thanks so much

#

It feels good to be somewhat done with this

#

how many hours was this

#

2 lol

#

do you do this for like a job or just for a hobby

candid torrent
#

for fun I guess :p

midnight fossil
#

wow

candid torrent
#
[Serializable]
public struct CustomTileData
{
        public int Durability;
        public CustomTileData(int durability)
    {
        this.Durability = durability;
    }
}```
#

public class BoardTile: Tile
{
    public CustomTileData dataTile;
}
midnight fossil
#

I didn't need this but thanks!

candid torrent
#

oh ok cool!

midnight fossil
#

okay I tried to do it without looking at your code

#

and I got it with no errors

#

but its not deleting the tiles in the game

candid torrent
#

!code

lone moonBOT
#
Posting code

📃 Large Code Blocks
Large code blocks should be posted as links to services like:
https://gdl.space/, https://paste.ofcode.org/, https://hatebin.com/
https://paste.myst.rs/, https://hastebin.com/

📃 Inline Code
Surround code with three backquotes. Not quotation marks.
To get C# formatting the first line should only contain cs or csharp.
Add a comment with a line number if there is an error message.
```cs
// Your code here
```
Do not share screenshots of code unless requested.

midnight fossil
candid torrent
#

did you look at what I sent

midnight fossil
#

I think i see it now

candid torrent
#

they're about the same but few minor differences

midnight fossil
#

wait a minute

#

hmm still not working

candid torrent
#

what exactly isn't working ?

#

are you sure it's grabbing the right tiles

midnight fossil
#

I was checking that right now

#

I keep getting this

#

is it the add function?

candid torrent
#

whatexactly are u debugging?

midnight fossil
#
Debug.Log(dataFromTiles);
#

and then I tried the keys

#

I wanted the specific data but its just telling me what it is generically

candid torrent
#

Debug.Log(dataFromTiles.Count);

midnight fossil
#

that seems to be working

#

I also need to add a catch for when I click on an empty tile

candid torrent
#

do you wanna just copy / learn from my script i just made that works?

midnight fossil
#

I'd rather stick this out

#

I just don't understand why mine doesn't

#

still trying to figure it out

#

do you know the issue?

candid torrent
#

hard to tell from here

midnight fossil
#

makes sense

candid torrent
#

needs more debugs

midnight fossil
#

alright

candid torrent
#

ima just like leave this here

midnight fossil
#

thank you

#

ASDFEGBGF

#

its always the smallest things

#
//wrong
var newVal = dataFromTiles[cellPosition]--;
//right
var newVal = dataFromTiles[cellPosition] - 1;
#

Its all solved now

midnight fossil
#

that was the only issue

candid torrent
#

it worked for me

midnight fossil
#

it works now so idk

candid torrent
#

one is subtracting the other is getting last element

midnight fossil
#

the --?

candid torrent
#

yes that's -= 1

midnight fossil
#

oh I see

candid torrent
#

in my script i assign it a var first

#

then change var then put it back in dictionary

midnight fossil
#

is using the struct that much more efficient

#

or just coders choiice

candid torrent
#

is a good way to store bunch of data

midnight fossil
#

I'll try implementing it

candid torrent
#

you're using structs all the time like whenyou use Vector3

midnight fossil
#

man today just relight my like for coding