I’m actually having a bit of trouble even explaining this idea as I’m still grappling with the theory nevermind the syntax of what I’m trying to do but I’ll try and make a sensible post on it.
What I’m trying to do is create an AI that checks the ownership of a list of gameobjects in the scene using an integer. The reason that I’m doing this is because the integers are in fact villages that can be captured by players and other computer AI and I figured having a simple integer and list would be the best way to keep updating all of this in runtime.
There’s a problem though, while my implementation of getting a list of my tagged gameobjects within the scene that I wanted the AI to search for works fine with a foreach loop I’m struggling to come up with an idea of how to handle getting the script component on each individual gameobject that contains my ‘owner’ integer and then running an if statement on each gameobject to see if they are owned or not by the AI in question.
To top it off, I then need to be able to need to make it so that the AI travels to or interacts with the gameobjects that are owned. So run a check through the gameojects that come up as 1 for example and then the AI will get the gameobject position and travel there. I thought about doing a second list which would be taken from the first and running checked through that second list but I have no idea how to go about this.
Okay, so what I’ve got is actually a workshop and a tree, the AI already has a list of trees it can go after and pickup which works fine but what I need is when 1 tree is picked up I need it to search for a list of workshops in the scene and check which workshops it actually owns so that the AI can then move to the workshop with the tree and add it to the workshop once it reaches the workshop it owns. I was thinking the right way to go about this would be to run a check through each of the workshops in the scene and see if my owner integer is set to a certain number with an if statement and have those workshops added to an ‘owned’ list for the AI.
Ideally I think this should all be done in a list for the sake of accuracy because you can build new workshops and the AI will be able to take over towns so it needs to have that information available. I’m open to other suggestions though if anyone has any ideas.
Just to clarify if i understand correctly,
Game with realtime village(s) managing, where for example you build a lumberjack workshop, there is a worker assigned to that workshop and after he chops a tree you want him to go his current workshop with the tree and not some other if there are multiple workshops?
OR
any worker can go to any of his workshops, for example the closest one?
No the workers here aren’t the issue, the AI is a separate entity that can grab objects and pick them up, in this case it’s taking a tree and then putting it into the workshop that the AI owns. The villagers have their own AI and do their thing independently, not sure why you’re under the impression the villagers matter here.
Hang on, I should just give some gameplay so you can see what’s gong on, this is from the player’s perspective.
What I’m setting up is an AI that will pick the tree up and take it to the workshop automatically but it needs to know which workshop it owns and which it doesn’t. I was thinking the best way to do this would be with a simple integer.
I mean i’ve put some quick code together, though it’s just to illustrate…
To answer question about integers, yes use IDs why not.
also, you say AI is not a issue here but it doesn’t work as expected,…
i don’t know what to do to help.
You’ll need to have int MyID in AI script, loop through workshops check their IDs,
compare it and then set it as their current workshop based on closest distance for example : - )
The question is what loop to use in order to get the script components and check the integer within the list I have? I tried putting this into the foreach loop but it just wasn’t working. You’re completely misunderstanding my post, the villagers are not the AI I am working on, the AI is an entirely separate skirmish computer that builds and places gameobjects within the scene.
Ignore the villagers, I don’t know where you got that idea from that I was posting about that.
hahah,well i misread the OP for the “villagers can be captured”…
But if you look back i asked you about this, AI as in single unit or AI as RTS side AI.
I am sorry, guess today is not my great day of ultimate understanding.
Well this is what i got, although i already hear “no that’s not what i mean” echoing and coming closer…
[System.Serializable]
public class Workshop : MonoBehaviour
{
public int Owner_ID;
}
public class AI : MonoBehaviour
{
public int My_ID;
public List<Workshop> Workshops;
public Workshop CurrentWorkshop;
public GameObject WorkshopObject;
void GetWorkshop()
{
foreach (var workshop in Workshops) {
if (workshop.Owner_ID == My_ID) {
//Do distance check or some other checks
//otherwise you'll always get the last one in this loop
CurrentWorkshop = workshop;
WorkshopObject = CurrentWorkshop.gameObject;
}
}
}
void Update()
{
if (CurrentWorkshop != null) {
// go there or send someone there or what...
}
}
}
Actually I think you nailed it, thank you, it looks like I was just writing it the wrong way and I had the right idea I hadn’t realised you needed to convert so much to get each gamobject, I’ll give this a try and implement it and we’ll see what happens.
It looks like I need to learn more about how foreach loops work generally, I’m still trying to work out properly how you’re getting the owner ID from the gameobject but I think that’s just me needing to understand foreach.
Edit: I should explain just to clarify, the villagers themselves are an entirely separate system and they can be interacted with my the player and the computer AI. I’ve already done most of the behaviour for the villagers and now I’m doing up a skirmish AI so there’s somebody to play against in my game kind of like with RTS games like Age of Empires and Starcraft.
You don’t directly control the villagers but you can pick them up and so on I may not have been clear enough about that in my post because I was thinking about the specific coding problem I was having.
it’s cool, i’d just like to point out, doing AI for RTS can surely be darn difficult, and if you really think about it many, and i could even say most games are made for people to have fun with each other.
Games in general whether football,hockey,chess,tic tac toe, are made for more than one player.
And yes there are a lot of singleplayer only games, but imagine for example call of duty or unreal tournament having only singleplayer. What i mean is perhaps first try to make multiplayer and eventually skirmish. : - )
that’s how i’d approach it. Of course it’s entirely up to you.
just a question , what do you mean when you say “pick them up”? select them (pick them)
or literally pick them up in the air? : - )
that confused me in your posts.
If you look at the gif I posted where the hand is picking up the tree you can do the same with the villagers. I’m actually using for the skirmish AI a navmesh agent and I’m going to have it mimic this behaviour by searching through lists of villagers and trees etc. in the scene to pick up and behave just like the player.
You will also be able to pick up other objects, as for multiplayer I’d love to get it working but honestly I have found many of the unity multiplayer options incredibly frustrating to use and I’m waiting on Connected Games as I’m hoping that will have proper support and really up to date documentation.
If you have any current options I’d love to check them out but I tried stuff like Photon and the lack of support just drove me mad, nothing wrong with it but you could spend a whole year researching it and dealing with the problems just because there aren’t enough up to date basic tutorials to get you started.depending on what sort of game mechanics you want.
For example I was having a sync issue with photon when making a basic multiplayer FPS and a respawn button and I just could not for the life of me find anything that would demonstrate out to keep the players all synced to one room after clicking the button. There was lots of stuff on how to do it without a button but obviously you want the ability for players to control when they can spawn in rather than just have everything happen automatically sometimes.
I may contact Unity about getting Connected Games because I’ve heard that looks promising or looking for other options again but right now I may as well focus on getting singleplayer done.
Oh I see it now! This all makes sense! I should have been creating a list of scripts rather than gameobjects which is why I was struggling! I’ll post the results up when I get it all working you’re also having the foreach loop take that serializable public integer and assigning it to a local one in the AI script itself and that means we can do if checks on each gameobject.
Hi about photon, i did 2 player test shooter back on Unet or what that was called, i didn’t try out photon yet, but my guess is (really just a guess) there surely can be found all you need to know somewhere online, it’s kinda like archeology… hah
About script foreach loop, well you could also make a list of gameobjects and get the component when needed i just showed it like this. “Serializable” so if you have a many instances of that class(workshop.cs) for example in a list, you could see that in the inspector, maybe it’s not needed (not sure) since it contains just an integer but for future perhaps…if you’d wanna save it to json or something…
I’ve actually just come up with a much simpler idea because I didn’t realise I was being an idiot and overcomplicating things in typical programmer fashion. It was completely unnecessary for me to try and do anything from the list itself and have an if statement within the list because I already had the scripts with the owner integer attached to the workshops that I needed.
What I’ve done instead of all that complicated crap is I’ve done an if statement on the workshops themselves and checked if the integer is 1 then added that to the list the AI has so I can do a much simpler check through the list and have the AI move towards one of the owned workshop when it has picked up a tree.
I don’t know why I didn’t do this before but I’ll post up the results once I’ve got everything working. So to summarise rather than making everything complicated and going through a list of workshops and trying to do an if statement on each of them I’ve done an if statement on the workshop scripts itself which gives the exact same result.
I have the workshops searching for the 1 skirmish AI gameobject rather than the AI trying to unnecessarily search for all these other gameobject, I may also do this with the trees just to optimise things a bit better.
Edit: Just a note for the multiplayer I really did try, but it was too damn frustrating looking through the tutorials and so on, most of it is all out of date and there hasn’t been much updating done. The photon devs have posted up some basic tutorials which are up to date but they’ve mostly just dumped demo projects on their site which isn’t helpful for people who are looking to do things from scratch.
That’s just my criticism though, I think I’m going to concentrate on singleplayer exclusively until Connected Games becomes more available.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GetWorkshopOwner : MonoBehaviour
{
public int localWorkshopOwner;
public GameObject localWorkshopOwnerGameobject;
private void Start()
{
localWorkshopOwnerGameobject = gameObject;
localWorkshopOwner = GetComponentInParent<GetVillageOwner>().owner;
GameObject skirmishAI1 = GameObject.FindGameObjectWithTag("SkirmishAI1");
if (localWorkshopOwner == 1)
{
skirmishAI1.GetComponent<SkirmishAICollect>().currentOwnedWorkshops.Add(GetComponent<Transform>());
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using System.Linq;
public class SkirmishAICollect : MonoBehaviour
{
NavMeshAgent agent;
public List<GameObject> Trees;
public List<Transform> currentOwnedWorkshops;
public bool isTreePickedUp = true;
private void Start()
{
agent = GetComponentInParent<NavMeshAgent>();
Trees = new List<GameObject>();
foreach (GameObject Tree in GameObject.FindGameObjectsWithTag("Tree"))
{
Trees.Add(Tree);
}
agent.SetDestination(Trees[1].transform.position);
}
private void Update()
{
if (isTreePickedUp == true)
{
GetComponent<Collider>().enabled = false;
}
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Tree")
{
Transform localTreeTransform = other.transform;
localTreeTransform.transform.SetParent(gameObject.transform);
isTreePickedUp = true;
agent.isStopped = true;
agent.ResetPath();
agent.SetDestination(currentOwnedWorkshops[0].transform.position);
agent.isStopped = false;
}
}
}
This is what I’ve come up with and thought I’d share it just to give people a proper idea of what’s going on. Rather than do it the complicated way I was trying to I decided instead to do a simple if statement on the workshop itself and check whether or not it was owned by the AI. This way I can simply add to the list of owned workshops without too much bother, after that I can then have the AI just pick from the list easily and I’m using a navmesh agent to do all the movement and so on.
The AI also searches for a tree to pick up so it can take it to the workshop which is way simpler because it doesn’t really have an owner or anything that the AI needs to worry about. So that I don’t end up grabbing multiple trees I disabled the collider that does a check for the trees in existence when the AI goes to the tree to pick it up. I think I’ll need to come up with something better though I may in fact do a list of picked up objects for the AI because at the moment it can still end up picking up multiple trees when I don’t want it to.
So far though I’ve been very happy with how this has worked out and now I need to think about how I’m going to handle the AI picking up my scaffold blueprint and then placing that at a fairly random point in the village that it owns.