#so i have to get the script from the

1 messages · Page 1 of 1 (latest)

solemn crystal
#

thread

#

You need get a component from the game object

#

I don't like using the term "script"

#

it's kind of vague

#

the .cs files are scripts, but those scripts aren't literally attached to the objects in your scene

#
var oreScript = _ore.GetComponent<BasicOreScript>();
#

This will retrieve the component from the object you hit

#

you can then access the _worth field from it

fair frigate
#

what if i have multiple ores?

#

would i have to mention the script for each?

solemn crystal
#

It doesn't really matter how many ore objects there are in your world

#

The method will be called each time you collide with something

#

If 100 pieces of ore crash into you, you'll get 100 messages

fair frigate
#

ah

solemn crystal
#

Now, there ARE places where you might need to handle many pieces of ore

#

Imagine if you had a component that tracked all of the ore in the world

#

It would need a List<BasicOreScript> field in it

#
public class OreTracker : MonoBehaviour {
  public List<BasicOreScript> ores;
}
#

you could then put lots of pieces of ore into that list

solemn crystal
solemn crystal
fair frigate
#

yeah lol

solemn crystal
#

BasicOre and WeirdOre and SpecialOre

#

There are two options here.

#

One: you handle each one totally differently

#
void OnCollisionEnter(Collision collision) {
  var obj = collision.gameObject;

  if (obj.TryGetComponent(out BasicOre basicOre)) {
    // do basic ore stuff
  } else if (obj.TryGetComponent(out WeirdOre weirdOre)) {
    // do weird ore stuff
  }
}
#

TryGetComponent is useful for this kind of thing. It returns true if it found the component

#

and the component gets stored into that basicOre / weirdOre variable

#

This is what you'd do for completely different kinds of things, where there's no common behavior at all

fair frigate
#

so i'd make a if statement for every type of ore?

solemn crystal
#

Alternatively, you might have some shared behavior

#

Maybe every kind of ore has a value

#

In that case, you'd do a little inheritance

#
public abstract class Ore : MonoBehaviour {
  public float worth;
}

public class BasicOre : Ore { }

public class WeirdOre : Ore { }
#

Then you could do this

#
if (obj.TryGetComponent(out Ore ore)) {
  // do stuff that works for any ore here
}
#

notice how BasicOre and WeirdOre don't actually have any code in them

#

It wouldn't really make sense to have entire classes for each kind of ore if they only differ in value

#

you can just change the number on each ore prefab you make

#

But suppose they're a little more complicated. You might do this...

#
public abstract class Ore : MonoBehaviour {
  public float worth;

  public virtual void Collect() { // do nothing }
}

public class BasicOre : Ore { }

public class WeirdOre : Ore {
  public override void Collect() {
    base.Collect();
    
    GameController.weirdness += 1000;
  }
}
#

If we have an Ore, we can call ore.Collect();

#

and the appropriate method will be called

#
if (obj.TryGetComponent(out Ore ore)) {
  money += ore.worth;
  ore.Collect();
}
#

If we hit a BasicOre, we just get some money

#

if we hit a WeirdOre, we get some money, and then its Collect method, uh...increases the weirdness

fair frigate
#

i see

solemn crystal
#

This is called polymorphism

fair frigate
#

(i do not see)

solemn crystal
#

All we care about is that we have an Ore

#

It's a lot to throw at you all at once, haha

#

Can you tell me more about how your game will work?

#

That might help.

fair frigate
#

basically a type of factory processing game where the player can place miners which drop ores onto these conveyors which can then be processed to increase their value

#

and use that money to expand

#

and im trying to figure out how to have the collision detector do something for each specific piece of ore that passes through

solemn crystal
#

okay, so different kinds of ores might require different buildings to refine

fair frigate
#

kinda yes

solemn crystal
#

Basically, you need to decide whether each kind of ore:

  • gets its own class, like IronOre and CopperOre
  • is just an Ore with some data on it
#

Here's what the second might be like

#
public enum OreKind {
  Copper,
  Iron,
  Gold
}

public class Ore : MonoBehaviour {
  public string label;
  public float value;
  public Color color;
  public OreKind kind;
}
#

You'd just have Ore.

fair frigate
#

this would all go in 1 script?

solemn crystal
#

yeah, one script file

fair frigate
#

ok

solemn crystal
#

You can only define one MonoBehaviour per script file

#

But you can have other stuff

fair frigate
#

ok i see this one

solemn crystal
#

You could take this a step further, of course...

#
public enum ItemType {
  Ore,
  Bar
}

public enum Material {
  Copper,
  Iron,
  Gold
}

public class Item : MonoBehaviour {
  public string label;
  public float value;
  public Color color;
  public ItemType type;
  public Material material;
}
fair frigate
#

woah

solemn crystal
#

There is one annoying problem here, though

#

notice how stuff like label, value, and color are all part of Item

#

we don't really know how much an iron bar should cost, or what we should call it, or what color it should be

#

we'd have to manually define it

#

Have you ever heard of "scriptable objects" before?

fair frigate
#

i just saw a video on it

solemn crystal
#

in short, they let you make unity objects that aren't in a scene

#

You could do something like this

#
[CreateAssetMenu(fileName = "New Material", menuName = "Material")]
public class Material : ScriptableObject {
  public string label;
  public Color color;
  public float valueFactor;
}

[CreateAssetMenu(fileName = "New Item Kind", menuName = "Item Kind")]
public class ItemKind : ScriptableObject {
  public string label;
  public Mesh mesh;
  public float baseValue;
}

public class Item : MonoBehaviour {
  public ItemKind itemKind;
  public Material material;
}
#

These would go in three separate script files.

#

You would create assets for each kind of material and for each kind of item

fair frigate
#

gah damn

solemn crystal
#

Then you'd just plug them in

#

This might be too generic for your game, though

#

it means you can make a gold spring or an iron necklace

fair frigate
#

and ive plugged it in and it worked

solemn crystal
#

Yeah

fair frigate
#

tysm

solemn crystal
#

From my experience: the more classes you make, the more specific behavior you can create

fair frigate
#

i see

solemn crystal
#

The fewer classes you make, the more variety you can get

solemn crystal
#

And if you want some items to have special behaviors, then you have to figure that out

fair frigate
#

i will

#

thank you for your time

#

👍

solemn crystal
#

no problem (:

#

i'm making a soulslike game with a big pile of scriptable objects defining everything

#

so i've been going through this too lol

fair frigate
#

nice