#.
1 messages · Page 1 of 1 (latest)
only 1 working sounds like setting listener instead of adding
can I record a video of the project so you can see it?
you could also just use observer pattern, add the units selected to list and on event give them commands.
can I quickly edit some stuff and then show you the one guy working?
just send the script screenshots
yeah I will, I just need to edit someth real quick
Ok he works again as just 1
let me record it real quick so you have an idea
this'll be easier as well as the code
@plush hedge
generic rts stuff
so basically, the goal that i cant seem to get working atm is that when you click the NPC and then click the stuff he goes etc and clicking a diff npc commands them etc
private void NPCObject_OnGathererClicked(object sender, EventArgs e)
{
GathererAI gatherAI = sender as GathererAI;
Debug.Log(gatherAI.name);
if(SelectedGathererAI != null)
{
SelectedGathererAI.HideSelectedLight();
}
SelectedGathererAI = gatherAI;
SelectedGathererAI.ShowSelectedLight();
}
this is the event for the NPC click
ok that already doesn't make sense
you're calling a method name knowing you clicked gatherer, but then type matching object to gathererAI?
is there a reason for that?
i was just debugging the name
cuz it prints all of the names when clicking on either one of em
{
Debug.Log(gatherAI.name);
if(SelectedGathererAI != null)
{
SelectedGathererAI.HideSelectedLight();
}
SelectedGathererAI = gatherAI;
SelectedGathererAI.ShowSelectedLight();
}```
ill record the issue atm
it's not like it changes anything, just that it doesn't make sense without context
what calls the method you sent?
public void RegisterGathererClick()
{
if(OnGathererClicked != null)
{
OnGathererClicked(this, EventArgs.Empty);
}
}
and what calls this?
public void DetectObject()
{
Ray ray = mainCamera.ScreenPointToRay(mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))//Only works for single target selection. Use Shape cast for group selection!
{
if (hit.collider != null)
{
if (hit.collider.CompareTag("MiningNPC"))
{
RegisterGathererClick();
}
else if (hit.collider.CompareTag("StoneResource"))
{
RegisterStoneClick();
}
else if (hit.collider.CompareTag("ToolShed"))
{
RegisterToolShedClick();
}
else if (hit.collider.CompareTag("MiningNPC"))
{
RegisterGathererClick();
}
}
}
}
also the names don't match
controls.Mouse.Click.performed += _ => EndedClick();
you're calling OnGatherer, not NPCObject_OnGatherer
wdym
public static event EventHandler OnGathererClicked;
well where am i supposed to see that
i know i know sry
send it the exact way the CPU will see it
what i see is 6 1 3 4
not 1 2 3 4 5 6
the code?
it's not a new to unity issue
XD
you're supposed to be able to follow what you're doing
does that work
should have used hastebin
Hastebin is a free web-based pastebin service for storing and sharing text and code snippets with anyone. Get started now.
ok, now gathererAI
Hastebin is a free web-based pastebin service for storing and sharing text and code snippets with anyone. Get started now.
its a bit messy I know
so messy you're doing exact same thing twice
ok so i'm gonna tell you how to debug now...
you click something,
which calls a kinda pointless method for now... what does selecting even do?
nope, dead variable
;-;
it was gonna do something but it never did about 20 mins ago
a ray is sent
for some reason you check if it's null, and not using layers...
it checks tag...
and send an event that you clicked A miner, without specifying anything...
if nothing has subscribed before that, that's a dead end.
now,
in the other script
GathererAI.StoneResourceClicked += NodeObject_StoneResourceClicked;
GathererAI.ToolShedClicked += NodeObject_ToolShedClicked;
GathererAI.OnGathererClicked += NPCObject_OnGathererClicked;
```well thjere is this
you subscribe for the gatherer click event
so that it does the action on ANY gatherer click
it doesn't know which gatherer you clicked.
just that you clicked a gatherer.
o h
anyways, now that you understood how to read from left to right, it'll be easier to debug
how much did you understand from what i said
well the event is only detecting that a gather is sent meaning any with that class but never the type of which one so it'll just pick the first 1 in order
the problem is that they're listening from the moment they spawn
like imagine you create a unit
it's now selected for some reason
so
GathererAI.StoneResourceClicked += NodeObject_StoneResourceClicked;
GathererAI.ToolShedClicked += NodeObject_ToolShedClicked;
GathererAI.OnGathererClicked += NPCObject_OnGathererClicked;
shouldnt be where it is
https://www.youtube.com/watch?v=5e3gUuzAwKA&list=PLzDRvYVwl53t1vBNhjHANpXXz5M6EuT1q&index=6&ab_channel=CodeMonkey
I was tryna follow this 1 video for the multiple aspect actually and thats why I tried it that way
✅ Series Playlist: https://www.youtube.com/playlist?list=PLzDRvYVwl53t1vBNhjHANpXXz5M6EuT1q
Grab the Project files and Utilities at https://unitycodemonkey.com/video.php?v=5e3gUuzAwKA
Continuing the Resource Gatherer AI let's add support for controlling multiple Gatherers.
If you have any questions post them in the comments and I'll do my best ...
it worked for them the same way lol
you basically need to look up how delegates work and the observer pattern
so why does that work the same way ive done it
thats different though
you didn't understand how it works, copied half of it
thats for 2D
in his case everything is fine
ofc it is his works
he sends a delegate that returns the object clicked on
is the code I've wrote like for starters in the wrong place
like the awake for handler
basically this is what i keep saying every time about tutorials, if you don't understand any single part of it, you already managed to hurt yourself
https://unity.com/how-to/create-modular-and-maintainable-code-observer-pattern
was just looking through this
well thats the thing I dont always follow tutorials I mostly just figure it out but i wasn't sure on this one
if you're asking that, you already don't understand something.
you're not supposed to ask "is it in wrong place"
you're supposed to ask "why is it here"
so, why did you put it there?
I put it there so that I could have the events waiting for when the clicks happen which works for 1 unit
gathererAI is like 80% fine, you just need to finish copying codemonkey's script
wdym "copying" though
let me check
if(OnGathererClicked != null)
{
OnGathererClicked(this, EventArgs.Empty);
}
```?
no
thats what that method does
this one
thing is, he's getting event from click on sprite, you're getting it from a raycast
but you've put it in a wrong place
entirely
are you even trying to understand
yes
yes I am
you've said it is in the wrong place meaning I didnt follow correctly
this is the dead end you made
i can see that its not subscribing where I should
should be subscribing in the detect method
basically if from that point you'd copy codemonkey it'd work
but only if you actually try to understand what he does
well ofc i dont just copy and paste it and hope for the best
basically you clicked on something, you call same OnGathererClicked, but the difference is that CM does it from the object, you're doing it from the manager
doesn't change what you should've done, pass the object you clicked to next method regardless of what it is
so you're saying that the listeners need to be added in the actual object not the game handler class
detectObjects only gets called when the mouse click is released
public void DetectObject()
{
Ray ray = mainCamera.ScreenPointToRay(mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))//Only works for single target selection. Use Shape cast for group selection!
{
if (hit.collider != null)
{
if (hit.collider.CompareTag("MiningNPC"))
{
if (OnGathererClicked != null)
{
OnGathererClicked(this, EventArgs.Empty);
}
}
else if (hit.collider.CompareTag("StoneResource"))
{
if (StoneResourceClicked != null)
{
StoneResourceClicked(this, EventArgs.Empty);
}
}
else if (hit.collider.CompareTag("ToolShed"))
{
if (ToolShedClicked != null)
{
ToolShedClicked(this, EventArgs.Empty);
}
}
}
}
}
isnt that how you would "call" the clicks though
"controls.Mouse.Click.performed += _ => EndedClick();"
is in the awake for now
does this fulfill the objective?
I moved this there because thats when the raycast sees what object was clicked, unless you mean to put it in the actual resource class?
thats what the aim is yeah
what does it actually accomplish?
clearly nothing if it doesnt work xd
you have no listeners, and you're saying that you clicked on a listener, and pass the manager now, to the aforementioned 0 listeners.
the listeners need to be first
you have to sunscreen to them before you can even try registering a click
wtf sunscreen
no
subscribe*
that's completely backwards thinking
sunscreen wasn't on purpose
GathererAI.StoneResourceClicked += NodeObject_StoneResourceClicked;
GathererAI.ToolShedClicked += NodeObject_ToolShedClicked;
GathererAI.OnGathererClicked += NPCObject_OnGathererClicked;
arent these the listeners
NPCObject is the listener, yeah
correct
you perform that action, whenever the "Clicked" is called
i thought i said that ;-;
here
so, where is "Clicked" called?
atm nowhere
just had to go through the chat to find them cuz i somehow deleted them
then why is it there
you're basically stumbling on complete beginner stuff
I mean I've done way harder coding before with no problems
doing an advanced programming diploma and passing everything fine
and you don't know how to deal with delegates?
never used them
where the hell did you get diploma then
TAFE
I've used event handlers like this before in C# WPF applications fine
doesn't mean im bad, just confused and tired atm
that's weird but i guess they just used events instead of delegates then
yes they do
they use events
so ive never needed to use delegates
since the events are automatically done and run for you in the modern app development
if (hit.collider != null)
{
GathererAI.StoneResourceClicked += NodeObject_StoneResourceClicked;
GathererAI.ToolShedClicked += NodeObject_ToolShedClicked;
GathererAI.OnGathererClicked += NPCObject_OnGathererClicked;
if (hit.collider.CompareTag("MiningNPC"))
{
if (OnGathererClicked != null)
{
OnGathererClicked(this, EventArgs.Empty);
}
}
Can I place them here then
and also why im confused about this is because he puts
GathererAI.StoneResourceClicked += NodeObject_StoneResourceClicked;
GathererAI.ToolShedClicked += NodeObject_ToolShedClicked;
GathererAI.OnGathererClicked += NPCObject_OnGathererClicked;
in the awake of game handler
he doesnt put those in gatherer
yeah you really need a break
you're just randomly doing stuff now
99% of coding is done in your head

you should know the answer before you even get a chance to write something
This is the only thing ive ever struggled with, with coding
if you can't get to answer, you ask why
yes thats what flow diagrams are for
no, these are just a distraction
flow charts help tbh
they're fine once you get large enough but you're literally fucking up the most basic stuff possible
Dunno how then
but thats not really helping someone is it though, just saying they're not doing it right
i mean, i've literally done the un-spaghetti'ing for you back there
i have no idea what that means
^
why would you need a flow chart, you can just read the code linearly and you can already see the problem
program executes code linearly too, until you get to multi-threading
i mean when starting a new program
you can basically comment out everything that you didn't reach and start over
honestly idk what to say Ive never struggled with code like this before
yeah, that's why it's so weird that you mentioned diploma
the first language i started with was C++ and i was learning the win32 API
i understood that perfectly fine
But Qt is a lot better than win32
yea but it's just basic debugging, you learn that literally straight away
win32? that api is a mess
i mean the problem you have
then why not rest lmao
cuz i wanna try fix it first xd
i usually do the opposite
at least find the problem then figure it out while i'm going to sleep
yes I have literally forgotten how to CODE right now
then when i wake up with clear head i write down the solution real fast
if i remember it.
sometimes I have done that
this is why you write the problem summarised where it is
well, the problem is that you've done the first part correctly but then got completely thrown off by delegates
codemonkey did his stuff out of his ass as usual, but it explains why he used delegates the way he did
i know it does and i dont know why i cant do it
for the life of me i dont understand whyy my brain wont fkn work
literally all you need to do is get component and make a method for subbing there
i don't get it either, i've done more than 24 hours without sleep coding and it was never that bad
you got mental problems though?
adhd, yeah
cuz i got intrusive thoughts like theres no tomorrow lol
i was thinking more mdd
it comes along with it
it can do yea
idk why I cant do this but what am I supposed to do cuz my brain just
idek anymore
^
yeah but where is there lol
you clicked on thing -> thing.GetComponent<unit script>().SirYouHaveBeenClicked
is this in game handler or gatherer
or just click ->
OnClickEvent += thing.GetComponent<>().WhatDO
im assuming this is gh
you can go to sleep now
why
because i'm literally telling you the exact solution and you're still asking questions
there's no where, one of your scripts was essentially inaccessible
I have about 10 scripts in front of me
then start reducing them
7 are for the A*
anyways yeah. better take a step back, if you still can't understand what you're doing after you wake up at least
thing.GetComponent<>().WhatDO
its this bit
for me the worst thing that happens from coding without sleep is that i don't remember what i've done yesterday
cant access gameHandler.getcomponent
oof
but i've learned to write stuff so easily understandable that it takes no time to catch up, so it's all good
yeah thats fair
GathererAI.OnGathererClicked += NPCObject_OnGathererClicked;
I know i have to run this after the click function
i know that much is all
by this point i just expect i'll re-read code at least 10 times
oop
anychance you can edit my file since i apparently cant function like a normal person
get sleep
you don't
we can close this thread now if you want
yeah let's just bring this to DM and start over
I did the same diploma