#.

1 messages · Page 1 of 1 (latest)

plush hedge
#

this is better

zenith tendon
#

ahh

#

thanks

plush hedge
#

only 1 working sounds like setting listener instead of adding

zenith tendon
#

can I record a video of the project so you can see it?

plush hedge
#

you could also just use observer pattern, add the units selected to list and on event give them commands.

zenith tendon
#

can I quickly edit some stuff and then show you the one guy working?

plush hedge
#

just send the script screenshots

zenith tendon
#

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

plush hedge
#

generic rts stuff

zenith tendon
#

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

plush hedge
#

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?

zenith tendon
#

i was just debugging the name

#

cuz it prints all of the names when clicking on either one of em

plush hedge
#
    {
        Debug.Log(gatherAI.name);

        if(SelectedGathererAI != null)
        {   
            SelectedGathererAI.HideSelectedLight();
        }   

        SelectedGathererAI = gatherAI;
        SelectedGathererAI.ShowSelectedLight();
    }```
zenith tendon
#

ill record the issue atm

plush hedge
#

it's not like it changes anything, just that it doesn't make sense without context

zenith tendon
#

yeah I know

#

alright recording issue now

#

this is the issue

plush hedge
#

what calls the method you sent?

zenith tendon
#
    public void RegisterGathererClick()
    {
        if(OnGathererClicked != null)
        {
            OnGathererClicked(this, EventArgs.Empty);
        }
    }
plush hedge
#

and what calls this?

zenith tendon
#
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();
                }
            }
        }
    }
plush hedge
#

also the names don't match

zenith tendon
#
controls.Mouse.Click.performed += _ => EndedClick();
plush hedge
#

you're calling OnGatherer, not NPCObject_OnGatherer

zenith tendon
#

wdym

plush hedge
#

different things

zenith tendon
#
public static event EventHandler OnGathererClicked;
plush hedge
#

well where am i supposed to see that

zenith tendon
#

i know i know sry

plush hedge
#

send it the exact way the CPU will see it

zenith tendon
#

wdym by thart

#

(new to unity)

plush hedge
#

what i see is 6 1 3 4
not 1 2 3 4 5 6

zenith tendon
#

the code?

plush hedge
#

it's not a new to unity issue

zenith tendon
#

XD

plush hedge
#

you're supposed to be able to follow what you're doing

zenith tendon
#

should have used hastebin

plush hedge
#

ok, now gathererAI

zenith tendon
#

its a bit messy I know

plush hedge
#

so messy you're doing exact same thing twice

zenith tendon
#

pft i didnt see thatr

#

dont think that'll fix the issue ofc

#

removed that secti

plush hedge
#

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?

zenith tendon
#

nope, dead variable

#

;-;

#

it was gonna do something but it never did about 20 mins ago

plush hedge
#

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

zenith tendon
#
        GathererAI.StoneResourceClicked += NodeObject_StoneResourceClicked;
        GathererAI.ToolShedClicked += NodeObject_ToolShedClicked;
        GathererAI.OnGathererClicked += NPCObject_OnGathererClicked;
```well thjere is this
plush hedge
#

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.

zenith tendon
#

o h

plush hedge
#

anyways, now that you understood how to read from left to right, it'll be easier to debug

zenith tendon
#

~<

#

so how do we actually fix this chaos hell xd

plush hedge
#

how much did you understand from what i said

zenith tendon
#

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

plush hedge
#

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

zenith tendon
#

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 ...

▶ Play video
#

it worked for them the same way lol

plush hedge
#

you basically need to look up how delegates work and the observer pattern

zenith tendon
plush hedge
#

it doesn't

zenith tendon
#

thats different though

plush hedge
#

you didn't understand how it works, copied half of it

zenith tendon
#

thats for 2D

plush hedge
#

in his case everything is fine

zenith tendon
#

ofc it is his works

plush hedge
#

he sends a delegate that returns the object clicked on

zenith tendon
#

is the code I've wrote like for starters in the wrong place

#

like the awake for handler

plush hedge
#

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

zenith tendon
zenith tendon
plush hedge
#

so, why did you put it there?

zenith tendon
#

I put it there so that I could have the events waiting for when the clicks happen which works for 1 unit

plush hedge
#

gathererAI is like 80% fine, you just need to finish copying codemonkey's script

zenith tendon
#

wdym "copying" though

plush hedge
#

codemonkey:

zenith tendon
#

let me check

#
        if(OnGathererClicked != null)
        {
            OnGathererClicked(this, EventArgs.Empty);
        }
```?
plush hedge
#

no

zenith tendon
#

thats what that method does

zenith tendon
plush hedge
#

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

zenith tendon
#

his is 2D

#

remember

plush hedge
#

are you even trying to understand

zenith tendon
#

yes

#

yes I am

#

you've said it is in the wrong place meaning I didnt follow correctly

plush hedge
zenith tendon
#

i can see that its not subscribing where I should

#

should be subscribing in the detect method

plush hedge
#

basically if from that point you'd copy codemonkey it'd work

#

but only if you actually try to understand what he does

zenith tendon
#

well ofc i dont just copy and paste it and hope for the best

plush hedge
#

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

zenith tendon
#

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

plush hedge
#

you didn't understand again

#

try to explain why you did the change you did

zenith tendon
#

?

#

which change

plush hedge
plush hedge
zenith tendon
# plush hedge

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?

plush hedge
#

what does it actually accomplish?

zenith tendon
plush hedge
#

you have no listeners, and you're saying that you clicked on a listener, and pass the manager now, to the aforementioned 0 listeners.

zenith tendon
#

the listeners need to be first

#

you have to sunscreen to them before you can even try registering a click

#

wtf sunscreen

plush hedge
#

no

zenith tendon
#

subscribe*

plush hedge
#

that's completely backwards thinking

zenith tendon
#

sunscreen wasn't on purpose

#
        GathererAI.StoneResourceClicked += NodeObject_StoneResourceClicked;
        GathererAI.ToolShedClicked += NodeObject_ToolShedClicked;
        GathererAI.OnGathererClicked += NPCObject_OnGathererClicked;

arent these the listeners

plush hedge
#

NPCObject is the listener, yeah

zenith tendon
#

correct

plush hedge
#

you perform that action, whenever the "Clicked" is called

zenith tendon
#

i thought i said that ;-;

plush hedge
#

so, where is "Clicked" called?

zenith tendon
#

atm nowhere

#

just had to go through the chat to find them cuz i somehow deleted them

plush hedge
#

then why is it there

zenith tendon
#

it wasnt there though

#

i somehow literally lost them

plush hedge
#

you're basically stumbling on complete beginner stuff

zenith tendon
#

I mean I've done way harder coding before with no problems

#

doing an advanced programming diploma and passing everything fine

plush hedge
#

and you don't know how to deal with delegates?

zenith tendon
#

never used them

plush hedge
#

where the hell did you get diploma then

zenith tendon
#

TAFE

#

I've used event handlers like this before in C# WPF applications fine

#

doesn't mean im bad, just confused and tired atm

plush hedge
#

that's weird but i guess they just used events instead of delegates then

zenith tendon
#

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

plush hedge
#

why

#

i wonder how they teach in TAFE if you're not even thinking like a programmer

zenith tendon
#

I mean it's literally 5 am

#

I've been up since 8 AM the day before

plush hedge
#

yeah you really need a break

#

you're just randomly doing stuff now

#

99% of coding is done in your head

zenith tendon
plush hedge
#

you should know the answer before you even get a chance to write something

zenith tendon
#

This is the only thing ive ever struggled with, with coding

plush hedge
#

if you can't get to answer, you ask why

zenith tendon
plush hedge
#

no, these are just a distraction

zenith tendon
#

flow charts help tbh

plush hedge
#

they're fine once you get large enough but you're literally fucking up the most basic stuff possible

zenith tendon
#

Dunno how then

#

but thats not really helping someone is it though, just saying they're not doing it right

plush hedge
#

i mean, i've literally done the un-spaghetti'ing for you back there

zenith tendon
#

i have no idea what that means

plush hedge
#

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

zenith tendon
#

i mean when starting a new program

plush hedge
#

you can basically comment out everything that you didn't reach and start over

zenith tendon
#

honestly idk what to say Ive never struggled with code like this before

plush hedge
#

yeah, that's why it's so weird that you mentioned diploma

zenith tendon
#

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

plush hedge
#

yea but it's just basic debugging, you learn that literally straight away

zenith tendon
#

win32? that api is a mess

plush hedge
#

i mean the problem you have

zenith tendon
#

not when you're awake for like 24 hours

#

lmao

plush hedge
#

then why not rest lmao

zenith tendon
#

cuz i wanna try fix it first xd

plush hedge
#

i usually do the opposite

#

at least find the problem then figure it out while i'm going to sleep

zenith tendon
#

yes I have literally forgotten how to CODE right now

plush hedge
#

then when i wake up with clear head i write down the solution real fast

#

if i remember it.

zenith tendon
#

sometimes I have done that

zenith tendon
plush hedge
#

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

zenith tendon
#

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

plush hedge
#

literally all you need to do is get component and make a method for subbing there

zenith tendon
#

i swear im not retarded

#

ive done coding since i was 7 years old

#

im 19 now

plush hedge
#

i don't get it either, i've done more than 24 hours without sleep coding and it was never that bad

zenith tendon
#

you got mental problems though?

plush hedge
#

adhd, yeah

zenith tendon
#

cuz i got intrusive thoughts like theres no tomorrow lol

zenith tendon
plush hedge
#

it comes along with it

zenith tendon
#

it can do yea

#

idk why I cant do this but what am I supposed to do cuz my brain just

#

idek anymore

plush hedge
#

you clicked on thing -> thing.GetComponent<unit script>().SirYouHaveBeenClicked

zenith tendon
plush hedge
#

or just click ->
OnClickEvent += thing.GetComponent<>().WhatDO

zenith tendon
#

im assuming this is gh

plush hedge
#

you can go to sleep now

zenith tendon
#

why

plush hedge
#

because i'm literally telling you the exact solution and you're still asking questions

zenith tendon
#

to be fair you didnt say where

#

could be any class for all I know

plush hedge
#

there's no where, one of your scripts was essentially inaccessible

zenith tendon
#

I have about 10 scripts in front of me

plush hedge
#

then start reducing them

zenith tendon
#

7 are for the A*

plush hedge
#

anyways yeah. better take a step back, if you still can't understand what you're doing after you wake up at least

zenith tendon
#

thing.GetComponent<>().WhatDO
its this bit

plush hedge
#

for me the worst thing that happens from coding without sleep is that i don't remember what i've done yesterday

zenith tendon
#

cant access gameHandler.getcomponent

plush hedge
#

but i've learned to write stuff so easily understandable that it takes no time to catch up, so it's all good

zenith tendon
#

yeah thats fair

#

GathererAI.OnGathererClicked += NPCObject_OnGathererClicked;
I know i have to run this after the click function

#

i know that much is all

plush hedge
#

by this point i just expect i'll re-read code at least 10 times

zenith tendon
#

oop

#

anychance you can edit my file since i apparently cant function like a normal person

plush hedge
#

get sleep

zenith tendon
#

we can close this thread now if you want

plush hedge
#

yeah let's just bring this to DM and start over

zenith tendon
#

pfft start what over

#

the whole thing? xD

uncut heath