How do I create a list of GameObjects & Integers?
Alright, so- first of all, I'd like to apologize because I'm not the most patient with tutorials and I'm a little rusty with Unity.
The scenario is, I want to create a system where enemy "guards" will explore a list of rooms for the player- prioritizing rooms they haven't explored in a while. I figured a way to do this was to have each Guard assign a number to each room, and increase that number while they are inside it and decrease it when they're outside it. That way, to get to the least explored room all they'd have to do is go to the room with the highest number on it (and if a guard spots a player, you could increase the number of that room so it prioritises searching that area). For references sake I refer to this number as the rooms "temperature", a guard will always go to the hottest room in order to cool it down.
So, I follow the Navmesh tutorial, bake the maze, and make a basic enemy that can navigate the maze to a target- so I figure all I need is a way to set the current target to the floor of the room the guard needs to go to and I'm good. So I tag all the floors and have a script that can find objects with that tag.
I think what I need is an array or list that holds all the rooms (there are only 9 rooms in a 3x3 grid separated by walls) as well as an integer for each room that I can increment. I've tried looking up tutorials for arrays (which are apparently outdated), lists and structures for a way of doing this. It's possible I'm not using the correct method since the main problem I'm having is trying to combine several tutorials which results in errors- so I'm not entirely sure where to start for this specific problem.
I'd appreciate any help whatsoever, this is for a college project I'm working on.
Answer by AlwaysSunny · Dec 28, 2016 at 01:58 PM
Sounds like a cool idea! I'm always disappointed by games whose agents fail to do interesting things.
It sounds like your game would benefit from a Room class. Then, your game manager or map manager stores a list of rooms. Each room can know its center, temperature, even its own shape / boundaries if you want to get fancy.
Often when you want to store multiple pieces of data that are all related, it's best to chuck them in a class. After all, a class is for handling multiple pieces of data that are all related. ;)
To satisfy the actual question in the title - assuming I comprehend what you're asking for - without a Room class, you'll be storing gameobjects (the rooms, I take it?) and integers (temperature?) in separate collections.
I still recommend a Room class for your case, but there is nothing wrong with this "separate collections" approach. I do it all the time. You must simply be consistent in how you index them. In other words, the first element of the gameobject collection must always correspond to the first element of the temperature collection.
Here are some shortcuts for syntax for declaring lists / arrays:
using System.Collections.Generic // required for generic lists
public List<GameObject> myRooms; // lists
public GameObject[] myRooms; // arrays
This assumes you'll populate the arrays in the inspector. They'll already be initialized that way. If you don't expose & populate them in the inspector, you'll have to manually initialize them with code before using them.
myRooms = new List<GameObject>();
myRooms = new GameObject[9];
I'll leave it to you to learn how to read / write the elements.
Here's a good resource for those fundamentals:
https://www.dotnetperls.com/
update: Thanks, managed to get past that roadblock just in time for the end of the holidays- just needed to get my head around the gameobject.getcomponent function ^^
having a room manager definitely helps, although I won't be putting the temperature in the manager- that way each individual guard checks the room they themselves checked last first, if that makes sense
thanks again!
Your answer
Follow this Question
Related Questions
Remembering list of camera positions is not working 0 Answers
Merging multiple array's from a specific class without manually targeting them 0 Answers
why does my enemy attack backwards? 0 Answers
How to store objects and then call them? 1 Answer
How to make all enemies attack after spawning? Not only 1. 1 Answer