C
C#9mo ago
nomoney4u

Trying to understand this abstract code (and use it elsewhere)

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public abstract class AbstractDungeonGenerator : MonoBehaviour
{
[SerializeField]
protected TilemapVisualizer tilemapVisualizer = null;
[SerializeField]
protected Vector2Int startPosition = Vector2Int.zero;

public void GenerateDungeon() {
tilemapVisualizer.Clear();
RunProceduralGeneration();
}

protected abstract void RunProceduralGeneration();
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public abstract class AbstractDungeonGenerator : MonoBehaviour
{
[SerializeField]
protected TilemapVisualizer tilemapVisualizer = null;
[SerializeField]
protected Vector2Int startPosition = Vector2Int.zero;

public void GenerateDungeon() {
tilemapVisualizer.Clear();
RunProceduralGeneration();
}

protected abstract void RunProceduralGeneration();
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Random = UnityEngine.Random;

public class SimpleRandomWalkDungeonGenerator : AbstractDungeonGenerator
{
[SerializeField]
private int iterations = 10;
[SerializeField]
public int walkLength = 10;
[SerializeField]
public bool startRandomlyEachIteration = true;

protected override void RunProceduralGeneration()
{
HashSet<Vector2Int> floorPositions = RunRandomWalk();
tilemapVisualizer.Clear();
tilemapVisualizer.PaintFloorTiles(floorPositions);
}

protected HashSet<Vector2Int> RunRandomWalk()
{
var currentPosition = startPosition;
HashSet<Vector2Int> floorPositions = new HashSet<Vector2Int>();
for (int i = 0; i < iterations; i++)
{
var path = ProceduralGenerationAlg.SimpleRandomWalk(currentPosition, walkLength);
floorPositions.UnionWith(path);
if (startRandomlyEachIteration)
currentPosition = floorPositions.ElementAt(Random.Range(0, floorPositions.Count));
}
return floorPositions;
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Random = UnityEngine.Random;

public class SimpleRandomWalkDungeonGenerator : AbstractDungeonGenerator
{
[SerializeField]
private int iterations = 10;
[SerializeField]
public int walkLength = 10;
[SerializeField]
public bool startRandomlyEachIteration = true;

protected override void RunProceduralGeneration()
{
HashSet<Vector2Int> floorPositions = RunRandomWalk();
tilemapVisualizer.Clear();
tilemapVisualizer.PaintFloorTiles(floorPositions);
}

protected HashSet<Vector2Int> RunRandomWalk()
{
var currentPosition = startPosition;
HashSet<Vector2Int> floorPositions = new HashSet<Vector2Int>();
for (int i = 0; i < iterations; i++)
{
var path = ProceduralGenerationAlg.SimpleRandomWalk(currentPosition, walkLength);
floorPositions.UnionWith(path);
if (startRandomlyEachIteration)
currentPosition = floorPositions.ElementAt(Random.Range(0, floorPositions.Count));
}
return floorPositions;
}
}
I am trying to get access to SimpleRandomWalkDungeonGenerator.RunProceduralGeneration from another class but I understand that it is protected. I'm not sure how get access to that function without removing abstract from AbstractDungeonGenerator (stuffs will break). Does anyone have any suggestions?
9 Replies
Angius
Angius9mo ago
Just make it public instead of protected?
nomoney4u
nomoney4u9mo ago
But that introduces: SimpleRandomWalkDungeonGenerator.RunProceduralGeneration()': cannot change access modifiers when overriding 'protected' inherited member 'AbstractDungeonGenerator.RunProceduralGeneration()'
Angius
Angius9mo ago
Can't you make it public in the abstract class as well?
nomoney4u
nomoney4u9mo ago
When I did that I had to make other changes to for things to be public but then I now get this error:
NullReferenceException: Object reference not set to an instance of an object
SimpleRandomWalkDungeonGenerator.RunProceduralGeneration () (at Assets/Scripts/SimpleRandomWalkDungeonGenerator.cs:20)
PortalBehaviour.OnCollisionEnter2D
(UnityEngine.Collision2D portal) (at Assets/Scripts/PortalBehaviour.cs:13)
NullReferenceException: Object reference not set to an instance of an object
SimpleRandomWalkDungeonGenerator.RunProceduralGeneration () (at Assets/Scripts/SimpleRandomWalkDungeonGenerator.cs:20)
PortalBehaviour.OnCollisionEnter2D
(UnityEngine.Collision2D portal) (at Assets/Scripts/PortalBehaviour.cs:13)
Angius
Angius9mo ago
Something you're trying to access is null
nomoney4u
nomoney4u9mo ago
tilemapVisualizer.Clear(); tileMapVisualizer is now null I'm guessing the protected override void RunProceduralGeneration() on that function caused the tilemapVisualizer to be null?
Angius
Angius9mo ago
Maybe? I'd use a debugger to see when and what becomes null
nomoney4u
nomoney4u9mo ago
It seems that protected TilemapVisualizer tilemapVisualizer = null;, when making things abstract/protected/private, the variable tilemapVisualizer was not null, but now that I made things public, this variable is Null and is not being set like before.
Angius
Angius9mo ago
Nullability rarely has anything to do with accessibility
Want results from more Discord servers?
Add your server