#archived-code-advanced

1 messages · Page 142 of 1

olive totem
#

Glad I don't have that in my codebase, much rather have a couple of readable if statements

misty glade
#

Yeah, I think writing good code is making it as obviously simple as possible, not flexing with obscure syntax or use.. there's always fun competitions and stuff for that (IOCCC)

high lotus
#

Hey guys, I'm trying to make a spin wheel, but I want the arrow to stop at one of the four icons. Whats the best way to do this? Right now im just rotating the arrow for a random amount of time and then slowing it down.

olive totem
#

Agree it comes off as a bit pretentious

misty glade
#

A) Determine where you want to stop
B) Spin the wheel for a fixed amount of time
C) Tween the speed from 1.0 to 0.0 between the current direction and end direction

cedar ledge
#
float speed = useWalk
  ? (useSprint
    ? (Input.GetButton(sprintButtonName)
      ? walkSpeed * sprintMultiplier
      : walkSpeed)
     : walkSpeed)
   : (useSprint ? walkSpeed * sprintMultiplier
  : 0f);
misty glade
#

... that's really hard to mentally parse

cedar ledge
#

¯_(ツ)_/¯

#

better than 1 line

olive totem
#

@high lotus does this help?

int random = Random.Range(0,4);
float x = Mathf.Cos(((2 * Mathf.PI) / 4) * random);
float y = Mathf.Sin(((2 * Mathf.PI) / 4) * random);
Vector2 dialPosition = new Vector2(x, y);
#

It'll pick a random point from any of the 4 points in the circle

#

Forgot to add a radius but it gives you a vector in the range 0-1, if it needs to be bigger just multiply the vector

cedar ledge
#

for a sphere use Random.onUnitSphere

olive totem
#

He only has 4 points so not sure why you'd make it harder

cedar ledge
#

just leaving that knowledge there because uniform randomness on the surface of a sphere is unintuitive, ive been told

olive totem
#

Only thing random is one of those 4 points it can land on, again not sure why to make it harder than necessary

high lotus
#

Thanks guys, I'll try this

#

Nope, I have no idea what to do with that 😂

olive totem
#

How do you move that dial right now

#

I guess if you use some kind of rotate function you can do something similar in degrees, like dividing 360 by 4 and then multiply that number with a number between 1 and 4. This will give you a rotation that always lands on 1 of the 4 spots

high lotus
#
if (!finishedSpin)
        {
            spinwheelImage.transform.Rotate(0, 0, this.rotationSpeed * SpeedMultiplier * Time.deltaTime);
            if (slowDown)
            {
                this.rotationSpeed *= 0.92f;
            }
        }
#

In update

#

Then it counts down between 2 and 4 seconds and slowDown becomes true

olive totem
#

You could keep doing something like you're already doing, and just stop increasing the speed until it fully stops at a desired location

cedar ledge
#
private void Resize()
{
    Vector2 targetMotion;
    Rigidbody2D targetRB;
    if(target.TryGetComponent<Rigidbody2D>(out targetRB))
        targetMotion = targetRB.velocity;
    else if(target.parent.TryGetComponent<Rigidbody2D>(out targetRB))
        targetMotion = targetRB.velocity;
    else if(target.parent.TryGetComponent<Rigidbody2D>(out targetRB))
        targetMotion = targetRB.velocity;
    //for determining delta
    float before = c.orthographicSize;
    c.orthographicSize = Mathf.Lerp(c.orthographicSize,
            Mathf.Lerp(minZoom, maxZoom, targetMotion.sqrMagnitude), zoomStrength * Time.deltaTime);    //super smooth
    float delta = c.orthographicSize - before;
    //warning if camera zooms too fast
    if(Mathf.Log(Mathf.Abs(delta)) > -2)
        Debug.LogWarning("Camera changed size really quickly.");
}
#

why does it think targetMotion is an undefined local variable on the long line?

#

i defined it at the very top, so it should be in scope

distant pivot
#

It's declared but not defined--with your if statements, it doesn't appear to have a guaranteed value

cedar ledge
#

ah

#

(nevermind that)

distant pivot
#

If you have an else at the bottom, you could give it a default value (or before the ifs)

cedar ledge
#

it all makes sense now, thanks

#

for some reason i thought structs got defaulted to a zero-like state

#

but thats just with fields

#

im so happy i got my moving platforms, character controller, and camera follow working absolutely perfectly

#

the character sticks to the platform perfectly

native hinge
#

Here's a more comprehendible version of my quadro-ternary

#
float speed = 0f;
if (useWalk)
{
    if (useSprint)
    {
        if (Input.GetButton(sprintButtonName))
            speed = walkSpeed * sprintMultiplier;
        else
            speed = walkSpeed;
    }
    else
        speed = walkSpeed;
}
else if (useSprint)
    speed = walkSpeed * sprintMultiplier;
else
    speed = 0f;
#

If discord wouldn't auto tab stuff

cedar ledge
#

you could simplify that

#
if (useWalk)
{
    if (useSprint && Input.GetButton(sprintButtonName))
        speed = walkSpeed * sprintMultiplier;
    else
        speed = walkSpeed;
}
else if (useSprint)
    speed = walkSpeed * sprintMultiplier;
else
    speed = 0f;
#

i think i can do even more

#
if (useSprint && Input.GetButton(sprintButtonName))
    speed = walkSpeed * sprintMultiplier;
else if (useWalk)
    speed = walkSpeed;
else
    speed = 0f;
#

that makes sense to me

#

@native hinge

native hinge
#

O

#

h

#

thanks

#

There is just one problem

cedar ledge
#

also you should create a sprint axis in the input config instead of hard-coding the name

native hinge
#

and sprintButtonName == "Sprint"

cedar ledge
#

ah ok

#

i havent used getbutton before

native hinge
#

ah

#

Anyway, there is only one problem with your simplification

#

If useWalk == false but useSprint == true I want the player to auto-sprint

#

In your case it stops movement

cedar ledge
#

so the getbutton doesnt even matter

native hinge
#

In that case, yes

cedar ledge
#
if (useSprint)
    speed = walkSpeed * sprintMultiplier;
else if (useWalk)
    speed = walkSpeed;
else
    speed = 0f;
native hinge
#

Not quite what I meant

#

If you can both walk and sprint then the button must be held

#

If you can only sprint but not walk the button does not need to be held

cedar ledge
#
if (useSprint && (Input.GetButton(sprintButtonName) || !useWalk))
    speed = walkSpeed * sprintMultiplier;
else if (useWalk)
    speed = walkSpeed;
else
    speed = 0f;

like that?

native hinge
#

Yeah I think that works

cedar ledge
#

feel free to mess with it however works for you

#

i was just trying to show it should be possible to simplify

native hinge
#

Mhm, thanks

cunning dirge
#

It's true, but that's fine with me, it might be a little bit, I just want to get paid quickly. I think I was traumatized

vital breach
#

I want to Invoke something from a abstract class anyone an idea how to do that?

urban warren
vital breach
#

Maybe invoke is no the thing i meant. I want to introduce delay time to a method so that it cant be spammed. But i cant use coroutines

queen plover
#

You can if you get reference to a Mono ehavioe

#

Then you can use that to call StartCoroutine

vital breach
#

Yeah but this would make my Statemachine to coplex im searching for a simple method

queen plover
#

Without any context it's impossible to know what to do. There isn't a way to start a coroutine without a monobehavior (or maybe scriptable object) though

vital breach
#

hmmm

vital breach
#

async?

queen plover
#

I remember hearing that doesnt play nice (you can't use unity specific methods or something like that) though I could be wrong

urban warren
queen plover
#

I've definitely used it for addressables so I think I'm. Missing context on where I hear that

vital breach
compact ingot
urban warren
queen plover
#

That was where I got that from then

#

I always though async was it's own thread though, how else does it not freeze the game with stuff like await

compact ingot
#

Also the unity synchronization context introduces a 1ms thread.sleep whenever an async „thing“ is scheduled I.e. when you await something. That can be a problem but depends on the project.

queen plover
#

Btw, would you really need async for a state machine? Feels thatd be more event bases

compact ingot
#

Furthermore async allocates memory…. So those in terror of the GC don’t like it

compact ingot
queen plover
#

I mean in this user's question

#

They wanted something akin the coroutines for a statemachine

compact ingot
#

Why not use a proper state machine?

#

Coroutines are so very Limited

vital breach
queen plover
#

Tbh I try and avoid state machines for as long as I can. They spook me especially since the simple implementation is very ugly and the more complex one is - more complex to make

compact ingot
#

This one has very nice fluent interface and works for gameplay scripting just fine

vital breach
cobalt dome
#

hi i have a for loop. going through 10 objects. i need an int to count up until half way through the count then start counting down.
so as the for loop i goes 0 1 2 3 4 5 6 7 8 9 the int goes 0 1 2 3 4 4 3 2 1 0
anyone know how to do that?

queen plover
#

Probably a much cleaner way, but an If statement and book that is set when it reaches half way would do the trick

#

Though it doesn't sound very nice

compact ingot
austere jewel
#

One for loop, and another reverse for loop. Wow, easier to write, easier to read

urban warren
compact ingot
#

works just as vertx said 😉

cobalt dome
#

yes. i had somthing similar to the k part but the j is what i wasnt getting right

urban warren
compact ingot
#

that wasn't an advanced code question... was more like a leet code "very easy" question, kinda fizzbuzz level

austere jewel
#

Very much sounds like the kind of question that'd be on a bad quiz; which is why I generally would avoid answering them as given

mighty cipher
#

Anyone good in math and geometry to help me with something that might be easy, but i don't know the math to do what i want to do.

thing that i am doing, i'm placing two objects, then spawning a plane on the middle of both of them? (so far everything working like intended)
The object i'm spawning is a plane (as i am working on an AR app) the only thing is i would like the plane to be have it's rotations to make it be in the direction of the vector(just like if i was placing a wall), but i have no idea how to transform my vector dirrection into the rotation needed

        if(placedObjects[0] && placedObjects[1]){
            GameObject objectMiddle = GameObject.CreatePrimitive(PrimitiveType.Plane);
            objectMiddle.transform.localScale = new Vector3(100, 100, 1);
            objectMiddle.transform.LookAt((placedObjects[0].transform.position - placedObjects[1].transform.position));
            Vector3 middlePosition =  (placedObjects[0].transform.position+placedObjects[1].transform.position)/2;    
            objectMiddle.transform.position = middlePosition;
            EventManager.TriggerEvent(ShelfPlaneHandlerData.SpawnPlaneHandler);
        }
austere jewel
#

position it, then use LookAt. LookAt does not take a direction.

mighty cipher
#

the fact is

austere jewel
#

Or use Quaternion.LookRotation, which does take directions

mighty cipher
#

read what i said, i don't want it to look at something, that's not the question

vital breach
compact ingot
#

a plane doesn't work with simple lookAt because its normal is Y (iirc)

mighty cipher
#

the question is about the math

austere jewel
#

"i would like the plane to be have it's rotations to make it be in the direction of the vector(just like if i was placing a wall)"?

mighty cipher
#

yes

#

wana me to draw it so it's a bit more understandable ?

#

hold on

austere jewel
#

it's not difficult to understand

mighty cipher
#

So how ur answer is solving the problem ?

austere jewel
#

You are using LookAt incorrectly. I've corrected your usage, and described another method to orient the plane that takes explicit vectors

mighty cipher
#

that's on the last try, i wasn't using that before

#

but the fact is

#

the question is

#

How to convert a Vector dirrection to a vector of rotation

austere jewel
mighty cipher
#

....

austere jewel
mighty cipher
#

I'm trying it rn

#

but not sure it's gonna work

#

i'll keep u posted

#

not working

austere jewel
#

What is it doing?

mighty cipher
#

Nothing els than it was previously doing

austere jewel
#

It will do different things depending on how your plane is configured. As Anikki said, planes usually have Y up, so you'll have to do some cross products to get the correct vectors

mighty cipher
#

that's the thing i need, i am not that good in Math that's the issue

compact ingot
#

Quaternion.FromToRotation is your friend

austere jewel
#

you could just set Transform's up vector and do what FromToRotation does, as that's all up's setter does

#
// The green axis of the transform in world space.
public Vector3 up { ... set { rotation = Quaternion.FromToRotation(Vector3.up, value); } }```
mighty cipher
#

ok

#

i'll try it

#

ok so i did a few test before trying to change

#

with this

#

i have something, but to make it good for me rn with that X scale of my object is Y scale and Z scale is X scale, and Y = Z

#

Ok it's good, i'll manage with that, Thanks anyway @austere jewel and @compact ingot for the help it was good documentations to use !

finite pond
#

I've noticed that the elements of UnityEvents are re-orderable since Unity 2021. Does that mean the execution order is constant now, meaning from top to bottom? Or is that just a nice-to-have for designers?

flint sage
#

It just iterates through the call array and invokes them one by one

finite pond
#

Oh, really? I've read on the forums that it dealt with delegates and its execution order was random.

#

That's great to hear!

flint sage
#

Even if it was delegates it's not random

#

It's just not predictable in 100% of the scenarios

#

(while it should be in most iirc)

finite pond
#

Hm. Gotcha.

flint sage
#

I don't think Unity guarantees (i.e. in documentation) that they're executed sequentially but I doubt it'll change

finite pond
#

Yeah

unkempt nova
#

Don't crosspost

shadow seal
#

<@&502884371011731486> scam

hearty garnet
#

Hello,

I'm trying to get the spot light in Unity to have no fall-off. I want it to illuminate objects the same amount no matter the distance.

As far as I understand, I need to write a custom shader for that?

stable spear
hearty garnet
stable spear
#

have you tried creating a spot light and adjusting the parameters to see if it matches what you want

hearty garnet
stable spear
hearty garnet
dusky dawn
#

hello. I'm trying to get inspector like rotations from code. I see that one quaternion can be equivalent to multiple euler angles, but my question is how do I get the closest euler angle to my current stored euler angle. Any tips? I want a method where I pass a quaternion and the current euler angles and it returns the euler angle values that are closest to the input
Vector3 GetClosestEuler(Quaternion quat, Vector3 currentEuler)

untold moth
dusky dawn
#

I have an in game animation system and I need to auto key frame getting the euler values from the inspector

#

unity quaternion to euler conversion is quite bad for this usage

#

for example, 95, 0, 0 becomes 85, 180, 180

#

this will lead to a quite different rotation path than the desired

untold moth
#

Can't you set the quaternion value directly?

#

Also, that example doesn't sound like a realistic scenario

#

I think quaternion to euler would always return the smallest possible value.

dusky dawn
#

nevermind, you'd have to followed the path I followed to understand the problem

#

I guess this is a quite complex question for an internet forum

stable spear
#

transform.localEulerAngles should match the inspector

#

you can both get and set it

#

but if anything sets transform.rotation or localRotation, it will "mess up" the euler angles, in the sense that they get set automatically based on the quaternion (which is not a unique transformation)

dusky dawn
#

not it's not transform.eulerAngles

#

nevermind, it's a too advanced question for here

hearty garnet
dusky dawn
#

it would work, if it was not editor only

queen plover
#

Perhaps you can look at what GetInspectorRotation does

daring pelican
#

more of a C# question here: how do I write a generic method that would accept a monobehaviour as parameter and then do something like if(GetComponent<T>()) GetComponent<T>().enabled = false;

stable spear
#

you can add a condition for T, such as:
void EnableComponent<T>(GameObject obj) where T : MonoBehaviour { ...

tough tulip
hollow garden
daring pelican
#

Oh dain thanks guys

#

That's awesome

queen plover
#

Could you also do, GetComponent<Comp>()?.enabled = false

#

Can't test it right now so not sure

sturdy tartan
#

anyone used AnimationEvent.time in the past?
i'm getting some hasardous numbers with it, and i don't understand how to use it

dusky dawn
queen plover
dusky dawn
#

thanks

warm sonnet
#

Hey guys, Im making a damage indicator system that displays where the damage came from, and it all works besides the fact that it does not rotate with my mouse as I look at an enemy/source. If i move to the left of the enemy, it will rotate with movement correctly. It (the canvas element/damage indicator prefab) just does not rotate with my mouse. If anyone could help that'd be amazing, thanks! I also have the video I used to create it.

daring pelican
sturdy tartan
#

i basically just want to normalize the value of animationEvent.time

daring pelican
#

Yes you gotta scale it

#

What's the speed set in the animator state?

#

For this animation?

sturdy tartan
#

1

daring pelican
#

Hm ok hold on let me open up that old project

#

Check what's your animationClip.length is saying. I don't remember the solution exactly but I can see that I was using this in my code

#

You are not setting events via code?

pastel harbor
#

hello

#

Is there SVT under URP on the horizon already?

river musk
#

I have a list of 1mil gameObjects. Is this ok? Do I need to do it a different way to reference to them?

warped grove
#

what does => (lamda i think?) do?

carmine ermine
#

im currently spawning game objects randomly on a space of 100 x 100 plane. however, nothing stops them currently from spawning ontop of another.
how would i keep track of what positions are taken and which isnt? should i keep some sort of 2d array holding x and y values and try to keep track like that? or is there any other recommended way

compact ingot
#

you could in theory make a list of all 10k positions, shuffle them, and take N.... those then won't have duplicates... but you need a (relative) lot of memory and cycles

#

dont use a 2d array btw... use a hashset, the array would be as "wasteful" as the shuffle method

misty glade
#

Don't you hate it when you trip and fall into some corner of your codebase, and accidentally spend 3 days refactoring as a result?

misty glade
#

heh

#

it actually stemmed from a conversation in this channel, and I finally decided to do this refactor and.. obviously.. it was far bigger of a bite than i expected

#

but it's going to feel SO GOOD when it's done

compact ingot
compact ingot
#

if you make a casino game with that, please tell me;-)

misty glade
#

Yeah, it's clearly got some shortcomings, but for simple pRNG generation with some fixed criteria (non-repeating, true randomness not required, no moving elements) it's pretty cool

compact ingot
misty glade
#

@carmine ermine I'm doing something like:

        public GridPoint GetOpenSpot()
        {
            if (Items.Count == Width * Height) return null; // grid is full

            // Enumerate available locations.
            Dictionary<(int x, int y), GridPoint> availableLocations = new(Width * Height);
            for (int rows = 0; rows < Height; rows++)
            {
                for (int cols = 0; cols < Width; cols++)
                {
                    availableLocations.Add((cols, rows), new GridPoint(cols, rows)); //initialize
                }
            }

            foreach (GridItem gi in Items.GetAllGridItemsList())
            {
                availableLocations.Remove((gi.X, gi.Y)); // remove filled squares
            }

            int random = NumberUtils.Next(availableLocations.Count); // 0 to count-1
            int i = 0;
            foreach (KeyValuePair<(int x, int y), GridPoint> openSpotKVP in availableLocations)
            {
                if (i == random)
                    return openSpotKVP.Value;
                i++;
            }

            return null; //shouldn't be able to get here.
        }
compact ingot
#

hehe

misty glade
#

that's even only in one of my 3 applications, too

#

i did "complete" the server side of the refactor but who knows if it'll work yet 😛

misty glade
#

Basically I was tired of debugging stateful desync issues so I decided to refactor the entire "battle" object that contains everything about a 1v1 battle so that it could be serialized, compared, cloned, and checked for differences

misty glade
#

(but it works)

#

I just don't see how it can be immediately/obviously improved, aside from writing it using linq (which isn't my strong suit)

compact ingot
#

if the method should only return one point, its extremely wasteful

misty glade
#

yep, it only returns one point

#

thoughts on improving it?

#
    public class GridPoint
    {
        [Key(0)] public int X;
        [Key(1)] public int Y;
        public GridPoint(int x, int y) { X = x; Y = y; }
    }```
#

GridItem is a fairly bulky item that "exists" on a grid

compact ingot
#

you need to have a grid item for the entire grid i suppose?

misty glade
#

No - a grid item is ... a thing, on the grid

#

think uh... merge dragons?

#

Grid items have a bunch of properties and stuff on them, but the grid itself is sparsely populated and items exist at a GridPoint (just a simple class with an x and y)

carmine ermine
#

thank you guys! ill look into it

misty glade
#
            int random = NumberUtils.Next(availableLocations.Count); // 0 to count-1
            int i = 0;
            foreach (KeyValuePair<(int x, int y), GridPoint> openSpotKVP in availableLocations)
            {
                if (i == random)
                    return openSpotKVP.Value;
                i++;
            }

I think this is the most egregiously bad part of the above since this should be O(1) instead of worst case O(n) but I needed the dictionary for random access removal, and converting the dictionary to a list to get index-access to return that value would take best-case O(n) anyway

compact ingot
#
HashSet<Vector2Int> Items;

public GridPoint GetOpenSpot()
{
    cellCount = Width * Height;
    if (Items.Count == cellCount) return null; // grid is full

    Vector2Int candiate;
    for (int i = 0; i < 50; i++)
    {
        int random = NumberUtils.Next(cellCount);
        int row = random / Width;
        int col = random % Width;
        candiate = new Vector2Int(col, row);
        if (!Items.Contains(candidate))
            return candiate;
    }
    return null;
}```
misty glade
#

lemme digest, standby

#

erm, if I'm understanding that right, you're just attempting to get 50 random locations and then if it fails, return null?

#

if i understand that right - that can fail

compact ingot
#

depends on how dense you want to fill that grid

misty glade
#

100% dense

compact ingot
#

then this is no good

misty glade
#

🙂

#

the method should perhaps be named GetOpenSpotGuaranteed()

#

it only returns null if the grid is 100% dense

compact ingot
#

sec

misty glade
#

np - appreciate it, btw!

compact ingot
# misty glade np - appreciate it, btw!

would just keep that open list around ```cs
List<Vector2Int> Open;

public GridPoint GetOpenSpot()
{
if (Open == null)
{
Open = new List<Vector2Int>();
for (int i = 0; i < length; i++)
{
int random = LCG(Width * Height, i);
Open.Add(new Vector2Int(random % Width, random / Width));
}
Open.Shuffle();
}

return Open.RemoveAt(Open.Count - 1);

}```

misty glade
#

hehe, unfortunately that's a bit difficult since then I'd have to keep that list in sync with the gameplay - grid items can move around

#

so now moving would require finding the open item in that list, removing it, and adding the from location, etc

#

it gets complicated since there's lots of game actions that can bounce items back to a grid, remove items, etc - those would all need to get updated

#

right now the only "truth" is a list of grid items that have their X and Y internally

compact ingot
#

that grid isn't so big right?

misty glade
#

5x5 now, likely not (ever) bigger than 10x10

#

I don't think my solution is particularly awful in terms of memory/performance

#

it just... smells

compact ingot
#

then i'd just ignore any efficiency considerations and just avoid unnecessary allocations

misty glade
#

where are the unnecessary allocs?

#

(creating the Dict when i make the call, versus keeping it around?)

compact ingot
#

yes

misty glade
#

good point.. although moving it to a private member still incurs the memory cost

compact ingot
#

also, just a hunch... but it shouldnt be too difficult to maintain a open-positions list... it might be more effort to include in each game action... but thats basically how efficient data-structures get efficient... by maintaining some invariant on each add/remove/shift/rotate/....

misty glade
#

not that that's a huge issue atm - the server may have several hundred/thousand games at once, but rather the GetOpenSpot method is going to be called probably every turn anyway

#

it's uhm... super difficult to manage that, I think, unfortunately

compact ingot
#

well, turns arent frames 😉

misty glade
#

there are dozens of game actions, and the elements themselves are serialized/deserialized and two states managed at both ends

compact ingot
#

so i'd probably worry about other things... just make it somewhat nice, so i can sleep

misty glade
#

i'm not saying it's impossible but.. potentially a big piece of work

#

haha

#

thank you for the feedback 🙂

daring pelican
timber saddle
#

Is it possible to write native plugins for use in the Editor with loose C++ or Objective-C files?

urban warren
#

(I am only like 98% sure though so take that as you will)

timber saddle
misty glade
#

What are "loose" c++ files? You can compile libraries into native DLLs and they work fine..

#

@timber saddle

#

Do you just mean .. random files that don't have a corresponding project?

timber saddle
misty glade
#

Ah

timber saddle
#

I use that in Android and iOS. Now I wanted to make something for the Editor when running on a mac, but it seems I need to make an Xcode project and build a .bundle file.
Slight pain in the ass, but I guess it makes sense, as it's not using IL2CPP when running in the Editor.
I guess I'll just have to bite the bullet ¯_(ツ)_/¯

misty glade
#

ha - sorry I couldn't be more helpful 🙂

#

(I hope I didn't leave you hanging - I typed out the above and then had to go drop kids off at taekwondo and forgot to finish my thought)

vast meteor
#

does anyone know why this bug occurs (when i add the conditional before adding the dict), and then subscribe a object to a group, and try to subscribe a new object to a group, the new object dosent subscribe, however when i dont add that conditional, the objects all subscribe fine, however i cannot resubscribe a given object to any group while that object has been removed from a group and before it is deleted. The outcome i want is one where when i add a object, and then remove it, and then add it again, it adds like normal, and it works for any object i allow it to, basically the oposite of what the bugs do

#

remove script

#

video showing the bug

stable spear
vast meteor
#

i mean thats basically all you need but sure anyways

austere jewel
#

This is a really dodgy way of writing this sort of code for dictionaries of collections.
The usual way would be:

if (!dictionary.TryGetValue(key, out var list))
{
  list = new List<T>();
  dictionary.Add(key, list);
}

if (list.Contains(value))
{
  return;
}
list.Add(value);

Something like that.
If you don't need to perform any more additions or processing in the addition stage you could simplify it to something like:

if (!dictionary.TryGetValue(key, out var list))
{
  dictionary.Add(key, new List<T>() { value });
  return;
}

if(list.Contains(value)) return;
list.Add(value);
#

Similar thing for removal, use TryGetValue - exit early if there is nothing found.
Call remove, if the collection is empty, remove the key from the dictionary.

abstract folio
#

Trying to do cylindrical UV mapping to an arbitrary, but closed, polyhedron mesh. I’ve got it ALMOST right, but I’m having trouble with the area where triangles cross the UV.x=0 and UV.x=1 boundary.
I THOUGHT I had an algorithm that fixed it, as you can see the before & after results in this image.

#

Unfortunately, this does NOT work for ALL meshes, - you can see the before & after results with this mesh- clearly it didn’t work so well. And I’m not sure why it is failing in some cases.

#

The code I’m using takes a mesh as input, and attempts to compute the UV’s based upon the vertex positions, with the assumption that it is closed, and centered around 0,0,0 in modelspace. It’s just a few functions, you can find them here. https://hatebin.com/qnmarqmynf

#

^ other UV problems remain near the poles, but that's just the nature of cylindrical mapping onto a sphere-like shape. I'll deal with those later, if at all.

#

oh. I never actually asked a question, lol. Can anyone find the flaw in the linked algorithm?

long ivy
abstract folio
#

alas same result, just texture is rotated 90 deg

#

@long ivy good spot tho!

sturdy tartan
green rock
#

Has anyone ever encountered a bug in which runtime errors aren't displayed on the console even when you explicitly throw an exception at Start() function? Debug.LogError prints okay but not runtime errors. Kinda having a hard time testing/debugging things 😦

abstract folio
#

I HAVE had unity gobble up some exceptions I generated ! usually in GUI stuff.

green rock
#

It should have thrown an exception right away in the end of the console but it didn't

#

anyone knows how to look into this further? I can assure that this exception is not yet catched in anyway.

abstract folio
#

not caught by YOU.. but if you code is being called by unity itself.. that COULD be withing a TRY block

green rock
abstract folio
#

I used Debug.Error instead 😦

green rock
#

It happens also with NullReference exceptions when I try to invoke methods from null objects

green rock
abstract folio
#

@modest lintel it was you that pointed this out to me the other day, no? ^

echo holly
#

does anyone know how to render an RT with defaultHDR format into a png? doing the conversion from HDR rendertexture to texture2d format gives me conversion errors like "Remapping between formats 74 -> 52 is not supported" depending on the format.
i want to encode as a png, if the RT / texture are in LDR, then it all works fine, but when i try to use HDR RT then it breaks down. i came onto this when experimenting with switching the project to linear.

kindred tusk
#

Not an expert, but wouldn't you need to convert the HDR texture to the right depth first?

echo holly
#

yeah, so thats what im trying to do but it wasnt working. trying to convert the hdr RT to a texture and then encode that texture as a png

#

but not sure of the correct process that allows conversion of an hdr RT into a texture2d. seems like none of the texture formats are compatible

compact ingot
weak agate
#
for(int y = 0; y < height; y++){
   for(int x = 0; x < width; x++) { 
       Debug.Log(int2DArray[x,y]);
   }
}

O(n) or O(n^2) complexity?

#

settle a bet for me 😐

flint sage
#

Big O notation is relative to your array size, so it would be O(n) which would be O(height * width) while O(n^2) would be O((height * width) * (height * width))

weak agate
#

ty Navi, I agree lol

#

I'd love to know if anyone disagrees with that...

agile yoke
#

I think asking "O(n) or O(n^2)" is meaningless without talking about what "n" is supposed to be, because it does not appear in the code at all, and in this case it's imo not obvious enough to simply omit it.
Just say "O(height * width)" or "O(n) where n = height * width" and there's no need for any arguments.

weak agate
#

Can you give me an example for this case that would result in O(n^2)

flint sage
#

I mean it literally c an't be O(n^2) since n is one variable (unless width and hegiht are equal)😛

agile yoke
#

My point is only that "In O(n), the n refers to the array size" is a convention that is used because it's often convenient and obvious what is meant.
The fact that you asked this question here in the first place is, to me, sufficient evidence that it wasn't clear enough to someone what n is supposed to mean. The right answer is to just clarify, instead of semantic debate about how convenience conventions should be applied to this specific case.

#

(Other than that: If width and height are related, O(n^2) often becomes a reasonable answer; and that kind of context is obviously always missing from a short snippet like above)

kindred tusk
#

It's not really an algorithm though, it's just exactly n steps. (maybe this is debatable maybe, I just mean big O is not necessarily relevant here)

lapis mica
#

i thought it was more about the growth rate as n gets bigger, so if height is a function of width or visa versa it would be O(n^2) but if they're unrelated it would be just O(n).
edit- wait I am wrong unless you take width as n in the O(n^2) case, and array size as n in the O(n) case

kindred tusk
#

So a linear search is O(n) even if you're searching for the first element. That would be the best case which is a single comparison, but the algorithm is still O(n).

#

i.e.

var array = new [] { 0, 1, 2, 3, 4 };
Array.FindIndex(array, 0) // 1 step
Array.FindIndex(array, 4) // 5 (n) steps
#

But FindIndex is O(n)

#

Simply stepping through an array to the end is best-case, average-case and worst-case n operations.

#

So it's just not that useful a thing to discuss.

lapis mica
kindred tusk
#

Exactly.

weak agate
#

yes I agree

#

just wanted to make sure I'm not losing my mind

kindred tusk
#

Internally it's just a 1D array with a pretty accessor [a, b]

#

Your code is just a single linear read of that array (containing width x height elements)

orchid marsh
# weak agate just wanted to make sure I'm not losing my mind

The reasoning is relative to how often the elements are reoccurring. Since you're only iterating through each element once, your cost would be n where n is your number of elements. If you were to iterate each element twice, it would be O(2n). Thrice O(3n).

compact ingot
mossy vine
#

Hello this is my current code to zero out the position how can i make it more smooth
transform.localPosition = Vector3.zero;

sly grove
remote arrow
#

you should use lerp or slerp

mossy vine
mossy vine
#

ohh

sly grove
#

Remember you need to do it a little bit each frame, so it needs to happen every frame

mossy vine
#

in a method

sly grove
#

not just once

mossy vine
#

i forgot

#

thanks

vast meteor
#

so does try get value not return null if the value dosent exist?

#

that way it dosent break the compilation process

sly grove
#

the actual value that was found (if it was found) is given to you as an out variable

vast meteor
#

ah

vast meteor
#

nvm

#

i realized i could literally just plug the list = new list<T> into the dict val

mossy vine
native hinge
#

Is it possible to call a method with multiple arguments in the UI system? (UnityEvents)

native hinge
#

Alright, thanks for the answer

ember geyser
#

i have lots of arrays with 512 floats in them. I want to sum them all, into another array of 512 floats. What's the fastest way to do this?

compact ingot
#

But you can pass in a reference to a scriptable object that can contain many fields

native hinge
#

Might try that

compact ingot
urban warren
#

Yeah, no 'magic' way to do that. Just gotta loop over all of them.

native hinge
#

I was gonna say make an int 'i' and then do foreach(int i in array) { sum += i; }

#

don't think it would be lol

urban warren
native hinge
#

right

urban warren
#

(That is what through me off for a second there)

compact ingot
#

One-liner ```cs
float[] sums;
List<float[]> allMyArrays;
sums = allMyArrays.Select(it => it.Sum()).ToArray();

urban warren
#

It would require an additional loop through all of the items.

compact ingot
#

maybe not... since its lazily evaluated item by item... maybe the compiler is smart enough to figure out its a simple map-reduce.

urban warren
#

Also I could be wrong but it sounds like they want to sum by index across all of the arrays (sum all floats at 0, then all at 1, etc.)

native hinge
#

SceneManager.LoadSceneAsync(splitName[0], lsm??); Why does this give me an error?

#

Doesn't the '??' check if the value is null?

urban warren
native hinge
#

ah

#

thanks

urban warren
#

Sure thing.

compact ingot
urban warren
ember geyser
#

I have another suspicion that memcpy of each array onto the stack will make this even faster, as it will reduce memory accessing time.

compact ingot
ember geyser
#

pre warming doesn't seem to work nearly as thoroughly as would be nice for this, and memcpy is so fast that two can be put into two stackallocs, and summed, very fast.

#

and the order seems to matter, too. So creating the target stackalloc first, and then creatiing and copying into the two to be summed, and then doinig the summing, is faster than doing it the other way around. Then copy back out, if necessary, or keep cycling through the remaining arrays to the two input stackallocs, with memcpy

compact ingot
native hinge
#

I read up on it

native hinge
#

Cool, thanks

#

And does this check if target equals null?

#

target ?? null

#

To me it seems the same as ==

urban warren
#

So in that case it is exactly the same as just doing target

#

Because you are returning null if target is null

native hinge
#

oh thanks

#

So, if I wrote var c = a ?? b if a is null, it returns b, otherwise returns a?

long ivy
native hinge
#

And one last thing

#
if (TryGetComponent(out agent))
    agent = GetComponent<NavMeshAgent>();

This checks to see if the object has the NavMeshAgent component, and if it does, assigns it, is that right?

urban warren
native hinge
#

Oh

urban warren
#

If it has a component it will assign it to the out variable.

native hinge
#

Cool

#

So I don't even need the if

#

TryGetComponent(out agent); This auto assigns 'agent' to the NavMeshAgent if the object has it?

urban warren
#

Correct

native hinge
#

Awesome, now I wonder why every tutorial I've seen does not do this

#

Thanks again

#

I guess this is safer than a regular GetComponent because it first tries to get it?

#

It's like try{} catch{} ?

urban warren
native hinge
#

ah

urban warren
#

It is more performant in editor too because it does allocate memory if it does not have the component.

native hinge
#

Good to know

austere jewel
native hinge
#

Yup, that was mentioned to me earlier

#

Thanks

compact ingot
urban warren
compact ingot
urban warren
compact ingot
#

Those vector operations get executed with special instructions (single instruction multiple data = SIMD) on the CPU that can add/multiply/divide/... up to 8 single precision floats or integers at once... So you copy the values of a array slice into the vector, then add it to result vector, all in one operation, then shift the slice start index by vector size, and repeat... at the end you collapse the result vector, add the few array entries that didn't evenly fit into a vector and done. @urban warren

static void TestSIMD(int n, float[][] rows)
{
    Stopwatch sw = Stopwatch.StartNew();
    float[] result = new float[n];
    int vectorSize = Vector<float>.Count;
    for (int i = 0; i < n; i++)
    {
        Vector<float> accumulator = Vector<float>.Zero;
        float[] row = rows[i];
        int j;
        // SIMD add
        for (j = 0; j < row.Length - vectorSize; j += vectorSize)
            accumulator = Vector.Add(accumulator, new Vector<float>(row, j));
        result[i] = Vector.Dot(accumulator, Vector<float>.One); // reduce the vector
        for (; j < row.Length; j++) // add the odd tail to the result in case n % row.Lenth != 0
            result[i] += row[i];
    }

    double lap = sw.Elapsed.TotalMilliseconds;
    Console.WriteLine($"SIMD           {lap:0.000000} ms (vector size = {vectorSize})");
}
ember geyser
#

or imagining things.

compact ingot
ember geyser
compact ingot
#

but isnt burst supposed to use SIMD?

urban warren
ember geyser
#

Indeed, but Burst still has to do all that work of getting things sorted out its way, etc etc... whereas with stackalloc and memcpy and lining stuff up, I could easily beat its way of lining things up to get ripped through by the registers

#

And my initial tests with float4x4 have turned in StUNNING results.

compact ingot
ember geyser
#

For me, it's been far easier than ANYTHING i've done with unity in the 2.5 years I've been using it, because it reminds me of my C childhood.

compact ingot
#

tbh, i'd rather write that memory stuff in plain C somehow... but with Span<> and such, i guess one can get rather close in convenient/safe c#

ember geyser
#

I can kind of intuit where and how and what to do to get the SIMD and vectorisations to fly, and it's been super relieving to think outside of FARKING OOP!!!

#

AGREED! But it feels like having an illicit affair, doing low level in C#. Fun, fun fun!

compact ingot
#

true

#

OOP is for suits

ember geyser
#

OOP is just so slow. And ECS has so many bloody rules.

#

By OOP being slow... I mean me. It takes me forever to work out an OOP architecture, for anything. This stuff, I can do everything I want, and have cycles to spare. Literally.

compact ingot
#

my issue with ECS in unity is, that they have to try to make something that works well when fully optimized for a particular application (a specific game/team/pipeline say) and make it generic... so it will end up very low-level and provide little improvement over DIY, or it will have, as you say, so many rules and edge cases that it is out of reach of the normal people that are supposed to benefit from it

ember geyser
ember geyser
compact ingot
# ember geyser I need a billion thumbs up for this comment. You NAILED IT!!!

this is my new favourite aspirational video in regard to performance sainthood https://www.youtube.com/watch?v=Ur53sJdS8rQ

GDC

In this 2021 GDC talk, lead engine programmer Adrian Bentley examines the technology choices that made Ghosts of Tsushima’s fast load times and compact patch sizes possible.

Join the GDC mailing list: http://www.gdconf.com/subscribe

Follow GDC on Twitter: https://twitter.com/Official_GDC

GDC talks cover a range of developmental topics inclu...

▶ Play video
ember geyser
regal olive
#

I'm not able to use Deconstruction syntax with Dictionaries:

Assets\BottleShooter\Shop\Shop.cs(16,56): error CS1061: 'KeyValuePair<string, ShopDataAsset.GunData>' does not contain a definition for 'Deconstruct' and no accessible extension method 'Deconstruct' accepting a first argument of type 'KeyValuePair<string, ShopDataAsset.GunData>' could be found (are you missing a using directive or an assembly reference?)

Assets\BottleShooter\Shop\Shop.cs(16,46): error CS8129: No suitable 'Deconstruct' instance or extension method was found for type 'KeyValuePair<string, ShopDataAsset.GunData>', with 2 out parameters and a void return type.

So far from https://stackoverflow.com/questions/42549491/deconstruction-in-foreach-over-dictionary# I saw that I might need System.ValueTuple which needs to be imported by NuGet, but it's already available in the code.

I'm using VS Code 1.61.1 and C# Extension 1.23.16 which is supposed to support .NET Core.

gray pulsar
# regal olive I'm not able to use Deconstruction syntax with Dictionaries: ```autohotkey Asset...

Default Unity settings use .NET Standard 2.0. The last comment on your stackoverflow link seems to suggest it will be included in .NET Standard 2.1, which presumably means it is not in .NET Standard 2.0. This page confirms that hypothesis: https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.keyvaluepair-2.deconstruct?view=netcore-2.0

regal olive
#

Or 4.x

wise sandal
#

Has anyone here used discord Rich presence, whenever it changes scene it break and gets stuck on whatever the last thing was.

onyx blade
#

Is there merit in changing my node editor from XNode over to GraphView now that it has slightly matured or is GraphView still heavily subject to change and/or removal

sage radish
#

Which is still a bit too early to use, so....

onyx blade
#

or is GTF meant to unify the graph API including Bolt?

sage radish
#

I don't know much about GTF apart from what I've read about in the NodeGraphProcessor issues, but it sounds like ShaderGraph will migrate to it in the future. (alelievr is a Unity employee)
https://github.com/alelievr/NodeGraphProcessor/issues/188

GitHub

The Graph Tool Foundation is now at 0.11.2 and seems to be getting along pretty well. Is it maybe stable enough already to port NGP to this new framework? Any ideas or plans for when this will happen?

onyx blade
#

Unity never rly gives a timestamp anymore, or so i've noticed

#

The hardest part of the migration will be to migrate existing graphs but I dont think that ll be that big of a deal since my node system is a chain of scriptable objects 🤔

lusty iron
#

Hey friends, we've built out our game for Mac and are encountering some issues. The game runs fine in the main menu and even through some gameplay, but after introducing some effects to the scene (by way of firing weapons) the Mac client crashes.

It gives out a bug report log, but I'm unable to discern anything useful from it. Something I've read online is that pushing the GPU on a Mac too hard will cause this type of crash, but from reviewing the Profiler on PC I can't imagine it's something like that.

To me it feels like a certain element of some of the FX causes this issue, but so far through process of elimination we've been unable to find more clues. We're going to keep doing that, but I wanted to solicit any insight here in case this was a common issue. Cheers

flint sage
#

No one can guess that, you'll to post logs or errors

lusty iron
#

Happy to

flint sage
#

I thin kyou should be able to symbolicate the crash with the symbols from unity

#

And what about the normal player log?

lusty iron
#

normal player log, like the console?

flint sage
#

Yeah

lusty iron
#

unable to get a read from that prior to the crash, there's no prior warning

#

I will definintely check that again though

#

what do you mean by symbolicate the crash?

flint sage
#

4 UnityPlayer.dylib 0x0000000101897c67 0x101235000 + 6696039
These bits point to certain methods, if you have the symbols (dsym for osx iirc) you can match them up and fill them in automatically

lusty iron
#

hmmm

flint sage
#

You can output the log to a file

lusty iron
#

nothing in the log that I saw, I'll do the text output but IIRC there was nothing logged aside from networking logs

#

could've been there for like a frame and I missed it, will defintiely double check when I can

#

being able to translate those codes to methods to get a better sense of the specific issue would be really helpful

flint sage
#

I only know how to do it with xcode for mobile projects, which is really annoying to do

#

Since it relies on xcode finding your dsym, which it fails to do most of the time or just won't do until a few days later

lusty iron
#

very interesting, wasn't aware of that process. googling into it now, thanks for the guidance @flint sage

ember geyser
lusty iron
#

been tricky trying to get the symbols read correctly, not sure I know what I'm doing

#

there is indeed audio clips on some of the fx, do you think that could be it?

ember geyser
lusty iron
#

Righteous! We are currently pooling the SFX but have them paired (meaning on the same gObject) with many VFX. Appreciate the angle mate cheers

ember geyser
#

Be very careful moving them around fast (audio sources), it's best to completely disable them, move them, and then add whatever audioclip you need to them, then activate and play. And never use PlayOneShot, as it's a monster.

#

You can swap audioClips as often as you like, as fast as you like, as this is light and very fast. So you only need just enough AudioSources for the maximum number of sounds you'd ever play.

#

And there's a hard cap, by default, at 32 Audio Sources, too... I think. set this to 128 or something like that, just so you never hit it.

#

and the above line is in red, so I'm not sure if it got sent.

#

I haven't seen that before, haven't used discord a huge amount. The line is about setting audioSource limit to higher than the default 32 (for Mac), to something like 128

#

Also be very careful with reverb zones, make sure you have only ever got one impacting any audio source, as this gets weird, real fast.

lusty iron
#

hmmm, it very well might be an instance of too many audio sources, that seems very possible

#

so the scenario you're outlining would involve 2 object pools, one for VFX, and one for SFX

#

and rather than each SFX object being it's own per sound, you recommend using one pool of X amount of audio sources, and swapping the clips out by use?

ember geyser
#

Yes, exactly, try to use the fewest pooled AudioSources you can, as there's some efficiencies that come from this, and you'll get lots of other strictness in what you program that prevents other audio issues. Mac and Audio are bad, Mac and Unity Audio are very bad together.

#

Much of what I'm saying does not apply equally to iOS. It's much more of a Mac problem than anything else. This is partly because of the age and lack of updates to the FMOD version in Unity, Unity's ways of handling audio, Unity itself and the nature of security in modern Mac chips with regards Audio

ember geyser
# lusty iron so the scenario you're outlining would involve 2 object pools, one for VFX, and ...

The easiest way to solve this is to pool audioSources, in a manager, and have your VFX request audio sources sufficient to perform the entire FX for any given event. I know it's a pain to do this, but the gains are efficiency and stability. and you can be absolutely sure you're pulling deactivated audioSources, positioning them, adding the desired AudioClip and setting all values needed and THEN and ONLY then enabling the AudioSource, so it doesn't teleport through a bunch of reverb zones and other potential issues with rapid moving of them.

lusty iron
#

so right now we have a single VFX pool, with a bunch of different VFX that mostly all have Audio Clips on them

#

what we do is disable the gameobject, pool it, and turn it on when both the VFX and SFX are needed

#

I'm confused as to whether you think they should be separate GameObjects, or if the Audio Clip just needs to be specifically disabled

ember geyser
#

I know... I went through all this. It was a pain. It was a lot of work, but the result, for me, solved all problems of audio and Mac/iOS, and gave me better performance, as I used a lot less Audio Sources as a result.

lusty iron
#

so is the true goal to lower the amount of Audio Sources?

#

and by lower the amount, you mean lower the amount in the scene, not lower how many are active?

ember geyser
#

That's only one of the goals. It's more of a side effect, that you have lower numbers of them, and this is greatly beneficial.

sturdy tartan
#

Hi, is there any way to avoid having a state in the Animator not get stuck and not exiting the current state when the transition condition has been met already ?

lusty iron
#

alrighty, I think we can manage that. It's really just a matter of splitting up the prefabs, splitting the pools, and drastically limiting the pool number of the audio sources

#

@sturdy tartan not exactly sure what you mean, can you elaborate?

lusty iron
#

also someone's going to suggest posting in #🏃┃animation instead so I'll do it first lol

ember geyser
#

Yes, the drop in audio sources, although a side effect of this approach, will be a huge performance booster, and remove other issues. But the real goal is to get you absolutely sure that you have a stable movement of deeactivated audio sources to positions, and certain control over their state.

lusty iron
#

it also seems like having audiosources moving around is not optimal? we are doing that in one or two cases to create a doppler effect

sturdy tartan
#

here's an illustration first to explain

ember geyser
#

Use ScriptableObjects to store the state of the AudioSources and their clips, so that you can do this efficiently, or public structs on each VFX unit with all the details needed to set to the audioSources as it pulls them in.

sturdy tartan
#

it can get out of it if i hold the transition condition true for 3 s

ember geyser
#

A few situations where you move at game object moving speeds (audioSources on moving game objects like rigidbodies) is fine. It's the teleporting to a location to play a sound that's the real problem.

sturdy tartan
#

but doing so, ruin my combo

lusty iron
#

and just to make sure I understand, teleporting to a location is fine as long as the AudioSource is 100% disabled?

#

because I do plan to place the sounds based on location

urban warren
ember geyser
#

And I fully understand what you're doing, the doppler effect, in conjunction with the reverb, is surprisingly VERY good in Unity.

lusty iron
#

yeah right? we threw it on to just see how it worked and it was pretty nice out of the box

#

no worries @sturdy tartan they will likely be more helpful, I can't tell what your issue is

ember geyser
# lusty iron because I do plan to place the sounds based on location

Yes, fully disabled, and even preferably without any audioclip during its movement to be doubly sure. Just program super, super defensively with regards AudioSources. There are myriad issues with them. There's not just one or two cures, this is a VERY old system that's never had much in the way of bug fixes.

sturdy tartan
ember geyser
#

Also, the Echo Audio Filter, at very low numbers of delay (think 11 through to about 24) with decay set to 0.9 or above, is super interesting, too.

#

The echo filter has a bug in the delay, you can't animate it. Unity knows about it, but I don't know when they'll fix it. So if you want to animate it, you'll have to do it with code.

lusty iron
#

As we saw with my inability to help with that animator question I'm pretty clueless with the animator so code's fine by me lol

#

I think this whole rework will help us take a closer look at things like audio effects, we're not using many rn

ember geyser
#

AnimationLegacy is worth a billion times more than Animator! If you can, don't use Animator, for anything!

#

And since you're pooling effects and stuff, DO NOT USE ANIMATOR!!!

#

There's huge problems with pooling and restarting Animators, that have never been fixed, that will drive you insane.

lusty iron
#

I'm using Animator

#

I'm gonna keep using it, our implementation has no issues so far

#

I'm not using it for effects though, just the main player character

ember geyser
#

Once you get to an issue, consider switching. It'll be quicker than trying to correct the ways ANimator works.

#

Oh, that's different. if you need timelines for effects, use Legacy Animation.

urban warren
ember geyser
#

AnimationLegacy is VERY lightweight and very fast. And using Evaluate on it is often quicker than making custom ease maths.

urban warren
ember geyser
#

just add a component to any object: Animation is the name of it.

urban warren
#

Oh, it is Animation I thought it was previously called something like Animation - Legacy

ember geyser
#

othe rthan the docs, they dono't call it Legacy, because it's not really a legacy system, it's the old system, that's lighter and faster.

#

Unity's naming conventions are bizarre. The sequencer is called Timeline, the timelines in both Animator and Animation are called timeline.

fallen halo
#

Which is better for Object Pooling, Queue, Stack or List

#

performance wise

rocky zephyr
fallen halo
#

what is pooling classes in Unity?

fallen halo
rocky zephyr
fallen halo
#

not this, rolling my own

#

which one should i use for better performance stack, queue or linked list

compact ingot
fallen halo
#

i want to store particle systems that i will reuse as effects

compact ingot
#

I mean algorithmically, and quantitatively

fallen halo
#

30 objects in a minute

compact ingot
#

just use a list

fallen halo
#

i find queue easier. Is it favourable than list

compact ingot
#

it really makes no difference

#

a list is also a stack, backed by an array

#

and for pooling, a queue adds no benefit

fallen halo
#

understood

fresh salmon
#

Queue has methods ready for you that do all the job for inserting or taking an item from it

#

But that's pretty much it

fallen halo
#

let's say i have a character when it moves there are some effects that happens like dust effects on the ground. for things like that i don't need a object pooling right

fresh salmon
#

A simple particle system can do that

fallen halo
#

yep

fresh salmon
#

With enough modules enabled, mainly "rate over speed", you can mimic disabling it, as it will not be emitting if you don't move

fallen halo
#

got it

#

if i use queue, since it is FIFO(first in first out) then after dequeuing an object won't the program has to reshift all of it's other object indexes

compact ingot
fallen halo
#

i see

fresh salmon
#

Yeah if it is, it just has to redefine where the "head" is, without shifting

compact ingot
#

Overall, linked lists are terrible for performance though

#

much more efficient to implement the queue as a list/array with two pointers and resizing it occasionally instead of spreading individual nodes all over the heap

olive totem
#

Lists aren't great for performance either. If you're going to make a pool of objects then use an array and set up enough space so you never need to resize

compact ingot
olive totem
#

Most information I come across claim that the array is always the fastest option for read and write operations

lusty iron
#

if you're concerned about performance differences between arrays and lists you're likely better off making your own engine. simply by using Unity you're accepting so much overhead that those minor discrepancies aren't really relevant

#

IMO

olive totem
#

Just an opinion but I think when there's no need to resize then array would be the best option

lusty iron
#

I totally believe that

#

but I'm not sweating over the times I use a list anyways lol

compact ingot
#

it’s even better to use stackalloc on the array and do math only with simd, but you’ll spend 10 times the time writing the code… and first of all that has to be worth it IMO

olive totem
#

Right but in this particular case using an array over a list isn't that much work, but one should do what makes the most sense to them of course

urban warren
# lusty iron if you're concerned about performance differences between arrays and lists you'r...

I would disagree with this rather heavily.
There are plenty of times when you are making systems that built on top of Unity that either operate on a large number of items or are called very frequently and are relatively expensive where you would want to spend the time to optimize them where you can.
An example could be an AI system. Should you just built your own engine just because you want your AI you have good performance?

#

Of course I'm not saying you should always spend the time to optimize things, to the level of array vs list, it of course has to be worth it for the time.

compact ingot
#

and array vs list is also just a stopgap towards real optimized systems… hello SIMD, hello Parallelism, hello GPU compute, hello ML, hello pinned memory, hello structs of arrays, hello caching, hello branchless programming, hello better algorithms, hello better architecture …. Endless possibilities for PhDs 🕺

pliant geyser
#

looking for some help regarding pathfinding

#

im using astar pathfinding and im having trouble with gameobjects running into eachother and getting stuck

#

is there a way I can have the paths of 2 gameobjects not intersect with eachother?

urban warren
pliant geyser
#

using the astar pathfinding project

urban warren
pliant geyser
#

I think its reserved for pro users only

urban warren
#

Ah, yeah.

tough knoll
#

Hey y'all

#

I've recently turned to a one-texture-atlas-for-everything solution for my level meshes since it reduces the drawcalls from 2000 to like 20

#

But as a result of this I have no easy way to differentiate surface types by material anymore.

#

Are there any tricks that don't involve having seperate colliders per material surface?

#

This is for things like footsteps or impacts

tough knoll
kindred tusk
#

oh, sorry. wrong one!

#

It seems 2021 source is not live yet

austere jewel
#

It is handy they provide a ListPool, DictionaryPool, and similar more generic versions

kindred tusk
austere jewel
#

They stopped ages ago

kindred tusk
#

I know I was just poking around

#

It doesn't make it any less disappointing

#

More disappointing actually

austere jewel
#

Yeah, it blows hard

#

Could at least say that Unity's C# source was available, now you can't even say that

kindred tusk
#

I was hoping the C++ would be made public

#

Yeah, exactly.

#

UE is fully OSS right?

#

Wow it is. That's so cool

#

They even have a guide for building it.

#

I guess if you have licensing it doesn't really matter if you're open source.

#

Maybe Unity just do it for the paywall?

austere jewel
#

I'm not sure if I would use OSS, but it's certainly source avaliable

kindred tusk
#

Yeah, not free but open and you're allowed to modify it.

austere jewel
#

I have no idea why Unity does what they do. There's so many amazing things they could provide that'd add so much quality

#

stuff that the Needle guys provide without even having proper access

kindred tusk
#

UGUI had a live repo.

#

It was just a mirror too though, they weren't actually working on it.

#

It would be awesome if we could actually open PRs against their modules. I thought when they started using UPM that everything was going to broken out to GitHub

austere jewel
#

These are things I used actively, and still use albeit with a lot more caution

#

You can make PRs against stuff like the Graphics repo

#

Some packages are leading the charge on that sort of thing, but others are just becoming shut away again

kindred tusk
#

Oh, nice. Is that a DOTS repo?

austere jewel
#

SRPs

kindred tusk
#

The good thing about needle is that you can fork at least, and it still adds a nice way to merge it upstream changes.

austere jewel
kindred tusk
#

Ah that is great.

#

And they're working straight into this?

#

1,654 branches!

#

Holy jesus time to prune

kindred tusk
#

Wow you programmed Unity3d

austere jewel
#

Who knows, I may be in the credits now

kindred tusk
#

It should at least be on your CV 😉

#

"previous work"

austere jewel
#

"optimisations¹ for vfx graph"

latent moss
#

how can i project a scene onto an object

real blaze
#

hey. can someone please help me out on the two last arguments for the Crossfade animation ?

#

I wrote their help text in front of them

compact ingot
#

help with what?

real blaze
#

seeing them like this , I can't relate the last two arguments...

#

as to what they mean

#

the NormalizedTimeOfsset is the start of the second animation ? ( also start of the fade )

#

( also sorry for the light mode there. i hope its not night over there )

final kindle
#

I seem to have found a rather anoying compiler difference between VS and Unity. VS thinks this is fine, while Unity doesn't. It complains about casing lambda to lambda. If I remove the ternary so it's a direct setting, it compiles just fine. I'm guessing this is just straight up a bug and I should report it?

Comparison<Vector2Int> comparison = shiftAmount < 0 ?
    (hexA, hexB) => hexA.x.CompareTo(hexB.x) :
    (hexA, hexB) => hexB.x.CompareTo(hexA.x);
real blaze
#

it doesn't even have the keyword not , for exampel

#

so if(A is not B) is accepted in newer dotnet , but not in old version that Unity uses

flint sage
flint sage
final kindle
#

Maybe. It's weird that the lambda causes a casting problem though.

real blaze
#

if u want the newer fancy C# features, u can use .Net 4.x from player settings

final kindle
#

This makes it work UnityChanLOL

real blaze
flint sage
#

I think it's technically a bug that it's not casting the correct thing

real blaze
final kindle
#

Definately. Just felt like something maybe worth reporting if I've not done something wrong.

flint sage
#

Nah, you haven't done anything wrong. There's no point in reporting it, Unity has to update the compiler/clr to fix it (which they're likely already working on)

real blaze
#

@final kindlehey mate. I googled a bit and looks like I was wrong. then sorry. .Net standard 2.0 isn't necessarily older than .Net framework 4.x

#

the numbers are a bit misleading there

#

from what I figured, this column is what .Net standard 2.0 means

final kindle
#

Ah. Thanks.

compact ingot
real blaze
compact ingot
#

there is standard 2.1 for newer stuff

real blaze
compact ingot
#

standard 2.0 is probably C# 7.x

real blaze
#

oh

#

so it is old...

compact ingot
#

its the most compatible standard that still has sufficiently modern features... 2.1 causes all kinds of incompatibility issues with stuff that is stuck on framework472

real blaze
#

could be useful

compact ingot
#

i'd suspect with 6.0 things will get better... up to now many considered .net core and 5.0 "unfinished" and didnt port their stuff over

real blaze
#

things you could do with C# 8.0 ...

public static RGBColor FromRainbow(Rainbow colorBand) =>
    colorBand switch
    {
        Rainbow.Red    => new RGBColor(0xFF, 0x00, 0x00),
        Rainbow.Orange => new RGBColor(0xFF, 0x7F, 0x00),
        Rainbow.Yellow => new RGBColor(0xFF, 0xFF, 0x00),
        Rainbow.Green  => new RGBColor(0x00, 0xFF, 0x00),
        Rainbow.Blue   => new RGBColor(0x00, 0x00, 0xFF),
        Rainbow.Indigo => new RGBColor(0x4B, 0x00, 0x82),
        Rainbow.Violet => new RGBColor(0x94, 0x00, 0xD3),
        _              => throw new ArgumentException(message: "invalid enum value", paramName: nameof(colorBand)),
    };
tropic lake
#

net core 2.1 is in one of the beta's or alpha's i think

#

once a stable version of that releases i'm gonna switch over because default methods for interfaces would be nice

real blaze
frank bolt
#

Hey everyone! Do you know how to draw the basic inspector for custom PropertyDrawer ? I just want to be able to hide or show a variable when I use an enum

flint sage
#

PropertyField

tropic lake
carmine ermine
#

are there any pros for making multiple prefabs for basically the same gameobject with different variations, instead of making 1 base prefab that you can load data into at runtime?
I have right now a creature base gameobject, and all variations of that gameobject should be more or less the same in functionality. they would differ in models, collider size, and a script that defines that creature and loads data(+maybe other things I can't foresee).
right now I feel like making a lot of prefabs is "easier" since you don't have to write extra code to load in data, and some stuff might be annoying to handle.
but I feel like 1 base prefab will be more efficient overall and scale better.

does anybody have some insight on this?
thanks!

tropic lake
#

i think this could be a candidate for scriptable objects

#

it would mean having a serialized field for the SO, then on Awake taking the values from the SO and turning that into the object

#

having many prefabs might not be scalable if there are many types and the variations aren't that large

carmine ermine
#

yeah, thats exactly what i thought

steel leaf
#

Does anybody know if async unity messages are now supported? I mean Awake and so on. Unity's Cloud Code example mentions internal async Task Awake() inside MonoBehaviour which seems very suspicious.

flint sage
#

They are

compact ingot
steel leaf
#

Does this mean that C# multithreading is a reliable thing now? Or do they use custom scheduling sort of thing. And which Unity version does support this code?

compact ingot
#

mind though that async await without Task.Run stays on the main thread like a coroutine would

compact ingot
#

it has some issues due to a thread.sleep(1) each time await/yield is called

steel leaf
compact ingot
#

I’d like it to be easier to have background threads out of the box

#

but async await wouldn’t help much with that anyway

runic tendon
#

I have a State machine running in code. When does it make sense to use mecanim?
Like, say I have multiple possible parry States(like sekiro) with the only difference being the parry animation and what attack state it could enter from. Is mecanim better in my case?

steel leaf
#

From my experience it is better to design your system with playables and custom state machine.

#

We did a system with Animation Controllers at first but it quickly became very unmaintainable and full of bugs.

#

If you still want to keep using Animation Controllers, try making all of your transitions from Any State, not building complex graphs.

rocky zephyr
# tough knoll Also, now that I'm reading - what is the advantage of these built-in pooling cla...

Sorry for the delay in replying. Others have chimed in but here's my comment: What Unity is providing in Pooling classes isn't a replacement for a comprehensive pooling solution like (for example) Pool Boss (which I use myself for other things). What I found Unity's pool classes good for is when you have a subsystem that needs to create and destroy hashsets, lists, class instances and the like which are created on-the-fly. For example, my tilemap extension has a runtime library that would create a stream of stuff for the GC to handle. Unity's pool classes make it easy to set up myriad small pools to handle each of these - and you can customize the behavior of each pool for reinitializing the old class instance when you get a recycled one from the pool and also you can customize what happens when you return an instance of something to the pool - for example: nulling out old references to GOs etc. So they're useful for many things - you could build a general-purpose pooler for prefabs etc if you like but there's plenty of nice commercial pooling solutions for that already.

tough knoll
#

Hm

#

Interesting, it seems like such a basic task that it's odd to include it at all, I was wondering if maybe it made use of some secret native code optimizations or something fancy like that

dusk magnet
#

hey yall, I posted this in general earlier but it got buried- I'm trying to find the gradient and/or contour vectors of a noise-based mesh for use with vector and/or tensor fields, and I haven't found any information online about how to do this- I understand that the tangent function does a little bit of what I want, but I don't know how to constrain it to the right direction or if there's a better way to do this

misty glade
#

I have a piece of code to update some UI members of a game object, and I want to centralize it and be able to call it from a variety of places.. I wrote the code but forgot nullable types aren't supported (yet) in Unity. Any suggestions for the following? I'd like to not make one method for each UI element that needs updating, but also be able to only update a part of the UI element:

        public void SetBattleUIStateImmediate(string? statusText = null, Color? battleStateColor = null, Color? iconColor = null, float? percent = null, string? hpText = null)
        {
            if (statusText != null) BattleStateText.text = statusText;
            if (battleStateColor != null) BattleStateText.color = (Color)battleStateColor;
            if (iconColor != null) Icon.color = (Color)iconColor;
            if (percent != null) BattleHealthBar.Percent = (float)percent;
            if (hpText != null) BattleHealthText.text = hpText;
        }
untold moth
misty glade
#

They are? I thought that was a c#9 feature

#

I mean, I could just wrap this in a nullable region but i thought that wasn't supported..

untold moth
#

Nullable value types seem to be around since C#2

misty glade
#

I suppose what I remembered (incorrectly) is that unity doesn't support the null coalescing operator, but nullable types are fine

untold moth
#

You might need to change null checks to .HasValue though.

#

Hmmm... A null check should work probably.

misty glade
#

Hm.. MS VS doesn't seem to pick up the directory buildprops file

#

not sure I want to sprinkle that annotation everywhere i use it

#

also.. treat warnings as errors gang signs

untold moth
#

You're trying to do nullable reference types. That's not supported in current unity version.

misty glade
#

aye that's what i wanted.. nullable ref types

#

actually.. what's the difference in

string? nullable;
string nullable;

aren't those both the same thing?

#

oh well i suppose this works for me

        public void SetBattleUIStateImmediate(string statusText = null, Color? battleStateColor = null, Color? iconColor = null, float? percent = null, string hpText = null)
        {
            if (statusText != null) BattleStateText.text = statusText;
            if (battleStateColor != null) BattleStateText.color = (Color)battleStateColor;
            if (iconColor != null) Icon.color = (Color)iconColor;
            if (percent != null) BattleHealthBar.Percent = (float)percent;
            if (hpText != null) BattleHealthText.text = hpText;
        }
untold moth
#

A string is a reference type, not value type. I'm not completely sure what's the deal with nullable reference types, since reference types can be null as it is.

compact ingot
misty glade
#

I have an item that's de/serialized on the server/client that has a "GridItem" - an item that has some values like X, Y, hit points, etc. When I send the message to the client, it contains a serialized grid item that the client synthesizes (into a new GridItem) using a Clone() method on the serialized copy of the grid item (so that I can GC/discard the rest of the serialized message).

A) Is it leaky to not clone it and just use the entire message's instance of the GridItem (I realize it's going to keep the message around until the GridItem itself is discarded, but that's not leaky, per se, just not really efficient)?

B) If A, is there a way I can enforce cloning of the enclosed GridItem so that I know I've made a mistake forgotten to new() up a local GridItem as I am supposed to? Code below for reference.

#
    [MessagePackObject]
    public class BattleActionCreateFieldItem : BattleAction
    {
        [Key(0)] public GridItem newFieldItemClone;
        [Key(1)] public Guid whichPlayer;
        ... other stuff unrelated to the GridItem ...
    }

    ... on the server ...
            BattleActionCreateFieldItem bacfi = new();
            GridItem newFieldItem = new()
            {
                Contents = (GridSquareContents)crewUsing.ActiveAbilityValue,
                Owner = actingPlayerGuid,
                MaxHP = GameLogic.GetStartingHP((GridSquareContents)crewUsing.ActiveAbilityValue),
                CurrentHP = GameLogic.GetStartingHP((GridSquareContents)crewUsing.ActiveAbilityValue),
                IsSummoningSick = true,
            };
            bacfi.newFieldItemClone = newFieldItem;

    ... serializes the "bacfi" and sends to client ...

    ... on the client, deserialize and process the "bacfi" ... 
        private void ProcessAddNewFieldItem(BattleActionCreateFieldItem bacfi)
        {
            GridItem newGridItem = GridItem.Clone(bacfi.newFieldItemClone); // This or just reference directly?
            Battle.Fields[bacfi.whichPlayer].Add(newGridItem);
            OnCreateFieldItem?.Invoke(newGridItem);
        }
flint sage
#

I don't remember but doesn't messagepack support deserializing into an existing instance?

#

We're not cloning the object but we're also not doing something uber performance @misty glade

drowsy radish
#

Hey, so have a bit of a weird problem, I have a camera separate from the main one rendering a weapon, that goes to a render texture to display on my GUI. But as part of the GUI, I want lines to point to certain areas of different weapons. I have all the positions for the points and references and everything, my problem is figuring out how to get the exact point on the screen from one of my transforms, because the actual item and camera are far off screen below the map, and only appear on the screen from a render texture, so something simple like just using WorldToScreenPoint produces inaccurate results. Any ideas what to do about this?

long ivy
#

if your two cameras render to similar size destinations (i.e. the render texture and the screen have the same dimensions), just use the gun camera's transform for the w2s transformation

stable spear
drowsy radish
#

I tried that initially, thought that it might work, but it rendered in the wrong spot, tho it was close. Tried as an experiment fixing it with transform positions of the rendered image etc. But not only did I have a problem when the screen resolution changed, I also had issues with the points being close, but not quite lining up with the actual positions.

drowsy radish
stable spear
drowsy radish
stable spear
#

is the render texture displaying on a quad?

drowsy radish
#

it is displaying on a GUI image

stable spear
#

oh!

#

can you show me the element it is displaying on in Inspector.

drowsy radish
stable spear
#

hm

#

i think transform.TransformPoint will still work

#

but the result might need to be shifted

#

one thing you could do is compute this point, and then place a tiny cube there to see if it corresponds to the correct spot

drowsy radish
#

hmm I'll check that rn, I have a gizmo for displaying the point

stable spear
#

er, tiny square on the canvas

#

since it is on a canvas, you want to stay in canvas space

#

since I assume you'll be drawing the lines, etc on the canvas?

drowsy radish
#

Don't know if I'm using it right, I'm getting odd wild results. I also have the line position thing done so that points to the final position. Cause of issues for now I have it set to scale a few rect images to get the right position, and as far as I can tell, that part is working perfectly.

stable spear
#

start by placing something (like a small rectangle) on the canvas at the same location as the render. And then once that works, add the offset you get from WorldToViewportPoint, scaled by a certain amount. If nothing happens (the rectangle stays in the center of the render), you might need to increase the scale.

drowsy radish
#

I just did a double check, it seems that grabbing the render images transform.position gives me an accurate starter position.

#

Hmm actually managed to get a semi consistent result as a quick experiment, I'm concerned I brute forced it a bit, and had to scale by over 1700, and then recorrect with a vector3 after

            screenPos = imageRef.transform.position + camRefPos * scaleRef - camRefPositionCorrect;```
#

hmmm, maybe the extreme scaling came from converting to screen resolution? rather than the rects transform scale

stable spear
#

yea there's some conversion between the camera resolution, canvas resolution, scale. I'm running on no sleep, can't think about it too precisely

drowsy radish
#

hmm so I need like a canvas point to screenpoint conversion essentially

kindred tusk
tropic lake
rugged sigil
#

Hello guys. Instantiating object will also create new Instance of the material and I don't want that. How my objects can have same instance of the material on Instantiating?

untold moth
rugged sigil
#

Hmm, that is less optimized compared to the example where they have same Instance of the material?

untold moth
#

Hmm... Maybe I'm wrong.

#

Are you accessing the instantiated object material via renderer.material?

rugged sigil
untold moth
#

That's why it creates an instance

#

You can do renderer.sharedMaterial to change color to avoid creating an instance of the material. But then all of the objects with that material would get affected.

rugged sigil
#

sharedMaterial still creates instances

#

And this is what I actually care the most

#

Since I have like 200 objects in the scene with different instances

untold moth
#

Donno. Maybe you're accessing .material somewhere else..?

rugged sigil
#

Yea, because I have to access them. I will try to make multiple Instances and instead of changing color I will try to assign new Instance of the material. Hope that this can work

#

Thanks for the help!

#

But yea, he still creates instance even when its not assigned

pulsar isle
#

I have a list of files I download from a server and instantiate them in the scene as GameObjects. All of them are disabled at start. Next I want to activate and deactivate one by one in a continuous order. with every time only one gameobject being active in the scene. The thing is the ammount of gameobjects is huge as seen in the profiler tab. This causes problems such as crashes when running on mobile. What are my options for object handling so the memory usage is lower/better managed ? Have in mind that every mesh is different from the other.

raw path
#

Hi there
does anyone know if accessing a singleton instance of a script is better by variable or by getter function?
Like if I had this

public static EnemyController instance;
private void Awake(){
  if (!instance) instance = this;
}

is it better to access it at runtime with a variable or with a getter function?

austere jewel
#

A property is better purely because you then cannot set the instance from outside the class

raw path
#

how come I can't?

onyx blade
raw path
#

what I meant was for that bit of code which is better?
this?

private EnemyController EnemyController => EnemyController.instance;

or this?

private EnemyController _enemyController;
void Start(){
  _enemyController = EnemyController.instance;
}
#

for access in other scripts

onyx blade
#

you cant access private stuff in other classes tho

raw path
#

not sure if something is going on in the back regarding memory management or something when accessing this dozen of times at runtime

onyx blade
#

so neither will work

raw path
#

the precise question would be, does it make a difference performance wise if I do/dont store the instance of a singleton in another scripts variable?

#

and instead access the instance with a getter

untold moth
#

It probably costs a tiny bit, unless the compiler inlines the calls. Even if it doesn't you probably not gonna notice any difference.🤔

raw path
#

fair enough

buoyant spear
#

Hey guys, I need some help with a problem the easiest place to start is asking if something is changed about the data when something is stored as a "string" compared to being used as ("say something");

carmine ermine
#

are you trying to understand when string objects are made?

#

or what

buoyant spear
#

So I have a script that types out text that I have as a queue, I added a list to the script and when I add the strings from the list to the queue, for some reason it doesn't type them.

carmine ermine
#

and i assume you call a function to also type out text from the queue?

#

like i dont really understand what your asking maybe im missing something

#

send the code maybe

buoyant spear
#

Okay, I guess I'm terrible at explaining. I don't know how to format script in discord

carmine ermine
#

copy it to hastebin

#

or something

#

then send the url here

buoyant spear
#

I was kind of just hoping someone had an answer about strings

carmine ermine
#

we cant know what to answer if you dont explain it well

olive totem
#

You're not being specific at all so nobody can help

hard lily
#

The problem might not be about strings at all, it might be about the logic of the script that is wrong

buoyant spear
#

Well the question was do strings store data differently to straight out typing the string in a variable? Because that is literally the only thing different between the scripts

olive totem
#

It's being stored the same

#

Something is wrong with the logic you applied but you're not giving any insight into this and I don't have a crystal ball

minor girder
#

Not sure if this is the right channel to ask things 😅
But i've made a chess game and I would like to be able to save the game at the end and then be able to watch a replay of saved games. Is there any good tutorial I can follow to figure this out or is someone willing to help? Thank you in advance!

olive totem
#

There are tutorials on how to save data but the other part of your question is very specific so the chance of finding something about this is very small

#

If I can make a suggestion, I'd say figure out first how to save things. Then find a way how you're going to represent the data you want to save. In theory you probably want to store information about every piece on the board and the moves it has made in a chronological order

minor girder
#

If this helps, it doesn't have to be like a video. Replay could possibly be like an iteration over the moves

olive totem
#

Yes I was thinking something like that myself and that would be the easiest way imo

minor girder
#

Yeah, that's kind what i want to achieve

hard lily
#

A video would be far more difficult and less intuitive than how replays actually work in video games

minor girder
#

Two simple ⏪ ⏩ buttons and it goes through list, and as you go, the moves change

olive totem
#

Saving data isn't the hard part itself though, it's how you're going to represent it is what might take some time to get down right

hard lily
#

Even games like Dota store replays as commands taken per tick

buoyant spear
minor girder
buoyant spear
#

theres the code, it's pretty basic so it doesn't leave a lot for me to guess at, the only real difference is me adding strings to the queue compared to straight up enqueing("");

olive totem
#

Yeah probably for each move store which piece it was, it's initial position and it's target position, maybe even store how much time it took before the move was made if you want to be able to do a real time replay

buoyant spear
#

both examples that i tried with string variables don't work, for some reason it just doesn't read strings coming from the queue

#

they get added successfully to the queue, though.

olive totem
#

@buoyant spear You don't show what the typer does

minor girder
olive totem
#

Alright then you should be ready to find out how exactly you're going to save the data. You can save it as text such as JSON or XML or save it as binary files

minor girder
#

I'll probably return here once i figure the save first 😂 for some help with the loading

olive totem
#

The loading shouldn't be much more difficult than the saving

minor girder
#

I meant replaying 🤦‍♂️

carmine ermine
#

your idea seems solid, honestly

scarlet copper
#

@buoyant spear just an idea from me. Do that with scriptable objects, it will make your live easier.

public class ScriptableDialogue : ScriptableObject
{
    public string DialogueText;
    public string TalkersName; // Or just use another (scriptable)object reference that holds image, name etc.
}

public class ScriptableConversation : ScriptableObject
{
    public ScriptableDialogue[] Conversations;    
}

// your manager script
public Conversation : MonoBehaviour
{
    
    public ScriptableConversation conversation;
    public Queue<ScriptableDialogue> dialogues = new Queue<ScriptableDialogue>();
    
    void Start()
    {
       foreach(var dialogue in conversation.Conversations)
       {
           dialogues.Enqueue(dialogue);
       }
    }
}

OR if you don't reuse dialogues some where else just use normal class:

[Serializeable]
public class Dialogue
{
    public string DialogueText;
    public string TalkersName; // Or just use another (scriptable)object reference that holds image, name etc.
}

public class ScriptableConversation : ScriptableObject
{
    public Dialogue[] Conversations;    
}

// your manager script
public Conversation : MonoBehaviour
{
    
    public ScriptableConversation conversation;
    public Queue<Dialogue> dialogues = new Queue<Dialogue>();
    
    void Start()
    {
       foreach(var dialogue in conversation.Conversations)
       {
           dialogues.Enqueue(dialogue);
       }
    }
}
#

The pro is you can configure the asset in the asset folder directly without working on the scene itself.

queen plover
#

I like to display dialogue in an editor graph, because when you introduce choices into something like this you'll end up with a horrible list to edit (though a graph can get equally messy)

scarlet copper
#

Should be possible here too I think. I never made a graph but for my own event system I would like. Had no time yet for it.

queen plover
#

Especially since it was built for a text-conversation kinda deal- rather than full on animations and storyboard

regal olive
#

Anyone getting this error in 2020.3.21f1?

ArgumentException: Could not find item with id: 0. FindItem assumes complete tree is built. Most likely the item is not allocated because it is hidden under a collapsed item. Check if GetAncestors are overriden for the tree view.

Happens whenever I double click on any item in the Hierarchy. It's triggered by the FindItem call.

#

Only happens in a specific project.

#

git clean didn't work.
Affects all scenes.

kindred tusk
#

@regal olive try resetting your layout.

regal olive
#

@kindred tuskokok

#

just choose anything here?

kindred tusk
#

"reset all layouts"

#

It's also possible it's coming from some custom editor window

#

I've never seen it, but I'm not on that specific patch version of 2020.3

regal olive
#

okay, that fixed it
what a weird bug

kindred tusk
#

It's just some bug in the editor UI

regal olive
#

rip

minor girder
leaden jungle
#

@minor girder just saving a list of moves should be enough to recreate the game that was played. I've even seen chess games where you could take back moves and change to something else. The only way to recreate it this way though is to do it turn by turn, which should not be difficult.

minor girder
leaden jungle
#

well you could do it by using the same functions you use to move your pieces in the game, but supply the information from the move list

minor girder
#

yeah, i'm looking into that, just thinking should i do it in a separate scene maybe?

sturdy edge
minor girder
leaden jungle
#

I would say not a loop because you want to be able to do one step at a time.

#

I would have an index variable for your moveList, int currentMove and then have it so your move method for when a player moves, make it so it can take parameters that would be supplied moveList[currentMove]

minor girder
#

yeah, i don't think loop would work as I would need to go in both directions

misty glade
misty glade
#

@minor girder : One design pattern I've implemented for my game is the concept of a "BattleAction" (with a number of derived classes, each for one kind of battle action) that, if played in order, could recreate a game state entirely from the starting state. You could probably do something similar to that, if you needed replay/rewind/etc functionality.

#

My use case is a tiny bit different than yours (I use the battle actions instead of just sending the "game state" because I want to animate the actions one by one) but .. you could have rewind functionality pretty easily with an approach like that.. Additionally (and this is giving me feature ideas for my own game) you could pretty easily save the entire game and play it out later

#

I do have the ability to send the comprehensive game state but I don't do that often (only at the start of a battle and on-demand while developing and debugging state desync issues)

#

(the comprehensive game state for my game is 1700 bytes or so - too large to send repeatedly - but each battle action is mostly 20 bytes or less, when serialized)

minor girder
misty glade
#

OK.. lemme prep some source code and toss it in pastebin for you so you can sorta wrap your head around how i'm doing it for my game

minor girder
#

Just one question, would it be possible to modify it to go backwards and forwards in the moves?

misty glade
#

bear in mind I'm using MessagePack for serialization so there's some attribute annotations in there you might not recognize or whatever, but the basic idea is that i'm just serializing it into a byte[] and deserializing it on the receiving end

#

i mean, probably? i just haven't built that into my game - that's some logic you'd need to figure out and build for yourself, depending on your game

cursive horizon
#

is the game multiplayer?

minor girder
#

my chess? yeah, but local mp, no AI implemented

cursive horizon
#

I meant Sharping's (i'm curious why there's that byte serialization/deserialization phase). You probably won't need that if you're doing local MP, unless it's serving some other purpose which would be interesting!

misty glade
#

yes, my game is multiplayer

#

i just grabbed these snippets super quick, if you have any questions, don't hesitate to ask

#

lemme take a quick snap of gameplay so this maybe makes more sense

#

Basically a player can move grid items around on their grid to try to merge them up into stronger items; move items onto their field where they can attack enemy crew (which is how you win), use active abilities, etc

cursive horizon
#

thanks for sharing this btw! hard to find real implementations for stuff out there

misty glade
#

but it's all based on a series of Player Actions and Battle Actions modifying a Battle state object

#

theoretically i could rewind/fastforward/replay an entire battle with just those items

#

pardon the developer art 🚧 we're still hiring our UIUX and art guys

minor girder
#

I will def share mine once I figure mine out

#

seems like something people could use

#

I appriciate this a lot kind person! I will look into it later!

leaden jungle
#

@minor girder maybe look into the command pattern with an undo/redo ability

urban warren
#

What would be the 'best' way to access items from a collection in a random order without getting the same item twice?
I can think of several ways, but was hoping for some insight.

I could generate a random int between 0 and Count, and keep a HashSet of the indexes already used, checking if it already contains the generated number, if it does, regenerate it. This feels more like a brute force approach though.

Could generate a random int between 0 and Count, removing that item from the list. However this doesn't let you 'start over', so if I wanted that I would need to keep a second list that the items are added to once they are accessed.

The only other thing I can think of is maybe some type of cool math thing that I don't know about?

cursive horizon
#

It depends what you need for your game, really. You could also keep track of how often each option has come up and increase or decrease its chances based on that

urban warren
cursive horizon
#

this is a good article on it

cursive horizon
#

then draw as many cards as you need

#

ultimately it's more of a design question than a code questoin

urban warren
cursive horizon
#

then i'm not sure what you're looking for i guess

urban warren
#

My specific use case is that I have a number of flowers on a GameObjec that can be eaten by a creature, I want to just select which flower to 'eat' randomly.

cursive horizon
#

but you don't want it to eat the same flower twice?

cursive horizon
#

why not?

urban warren
cursive horizon
#

ah, so your problem has nothing to do with randomness, and everything to do with keeping track of which flowers are available to be eaten

urban warren
cursive horizon
#

are you sure?

#

it's not that you don't want the same flower twice...it's that you only want to eat flowers which aren't already eaten

#

you should have a list of flowers which are edible and a list of flowers which are eaten, and you can pull from one and place in the other accordingly...or you could have a list of flowers with some kind of eaten boolean which you toggle, and filter them before selecting

#

regardless, your 'randomness' should just be 'select a flower from the list of edible flowers' and the thing you need to decide on is how you want to manage that list

urban warren
cursive horizon
#

right, which is why i said i'm not sure what you're looking for

#

you asked what the 'best' way is, so we talked about some of the context around what you're doing...but there is no 'best' without some more explicit values to judge against

carmine ermine
#

whats the problem with shuffeling the list once and then getting them in order?

urban warren
cursive horizon
#

solutions always have tradeoffs

#

it's not a difficult implementation to swap out either way, right?

#

i guess the answer to your question is 'it probably doesn't matter'

urban warren
cursive horizon
#

that's cool, but the most efficient system is always the one that fits into its context most efficiently

#

that's kind of what i was getting at originally

#

the most efficient representation of this is the one that your game already needs to have for gameplay reasons

#

because you need that anyway

#

optimization is something you do for specific cases because otherwise...what are you optimizing for?

#

so we need to build those specific cases first

carmine ermine
#

maybe @compact ingot will have a random formula for your case 😂

urban warren
urban warren
carmine ermine
#

if you eat a flower, shouldnt you take it out of the collection?

#

or is it still there

misty glade
#

i just built a quick randomizer for a list if you don't care that the list stays sorted

#

You could shuffle your list then just eat and RemoveAt(0)

urban warren
misty glade
#
           List<GridItem> ret = Enumerable.ToList(_gridItems.Values);
            var count = ret.Count;
            var last = count - 1;
            for (var i = 0; i < last; ++i)
            {
                var r = NumberUtils.Next(count-i) + i; // 0-9 on first call with count=10; 1-9, 2-9, 3-9
                var tmp = ret[i];
                ret[i] = ret[r];
                ret[r] = tmp;
            }
#

(replace ret with your list)