#Signals for Scenes wich aren't in the main scene

93 messages · Page 1 of 1 (latest)

static wedge
#

So hi there, hello...
I changed from Unity to Godot a few days ago and my first project is a simple 2D platformer with shooting mechanics.
I've implemented basic movement and a button to shoot bullets with. The most recent feature I've implemented is to flip the player while moving left and right by inverting the scale. Everything went well, but I noticed that, because I set the velocity value of the bullets to a Vector2(1,0), all of them are shot in the players direction if he is facing left. So I thought by changing the velocity to Vector2(-1,0), the bullets would move to the left side of the screen after being instantiated, wich is the case.
However, now I need to change the velocity value depending on the way the player faces. The problem is, the if-Statements wich determine when the player faces left or right are in my Player script, wich is attached to the player node in my scene. The velocity value of the bullets is in the bullets script, wich is attached to the bullet "Prefab"(scene or whatever).
So I did some research and as it turns out: I need to make use of something called signals. But no matter what tutorial I watch or documentation I read, they always use the Editor feature to connect the signal to the receiver node. My problem is that the bullets are to be instantiated while playing the game, not at the beginning of it. So I somehow need to make the connection of the signal to a script rather than to a node...I think...

Does anyone with more expirience than me know what to do here?

unique ravine
#

Aren’t the bullets instantiated by your player script? And if so, why not just set the direction when you instantiated the bullet?

#

From your explanation feels weird to use signals

static wedge
#

on the left is the player script and on the right is the bullet script. Because the physics of the bullet movement are in the bullet script, I need to set the "vvalue" variable to Vector2(-1,0) if the player changed his moving direction to left and to Vector2(1,0) if he changed back to right. So basically I need to set the value of this external variable in the if-Statements in line 17-24 each.

mystic wharf
#

its gonna be something like changed_left.connect(bullet.(some_function)) or something, I cant remeber the exact syntax.

#

but you have to also connect the changed_right signal too, if you are gonna go that route, it might be better to have a single signal that emits a boolean, not sure though.

static wedge
#

yeah I've tried using this connect thingy before, but always fail at the "Callable" part. Anyone know what that's about?

slate belfry
#

How about set a bullet vvalue to the same value as a player scale.x ? That way bullet will travel in correct direction.

mystic wharf
#

you might want to use the new syntax though which is

[Signal].connect(...
and not
connect ( [signal],...

unique ravine
#

Does your bullet change directions after it was fired?

static wedge
# unique ravine Does your bullet change directions after it was fired?

the "spawn point" of the bullet obviously scales with the player bc it is a child of it, but the velocity of the bullet remains Vector2(1,0) because that's how I coded it. So if I fire while facing to the left the bullet gets instantiated before the player, but will move to the right

unique ravine
#

Why not set bullet.vvalue = scale.x

static wedge
static wedge
mystic wharf
static wedge
#

but it outputs the following error

unique ravine
#

But you just need to set when you instantiate the bullet. I don’t know what I am missing.

unique ravine
#

Do what I told you. After instantiating the bullet set the vvalue

mystic wharf
static wedge
#

no the connect command must be in the bullet script, right?

#

that's where the vvalue is

unique ravine
#

A signal would be needed only if the bullet had to update every time the player flips after it was shot

mystic wharf
#

so what you want to do is make a function in the bullet that is called when the player changes direction, from the player, you connect the signal, and then when you change direction you do signal.emit()

#

at least from what I can gather

#

although Idk if signals are even needed here.

static wedge
#

what I'm trying to do is to change the vvalue wich is located in the bullet script (on the right) if the player changes directions (player script on the left), so if this first if-Statement is executed vvalue should become Vektor2(1,0), if the second one is executed it should become Vektor2(-1,0). How can I achieve this?

unique ravine
#

So your bullets do change direction after being shot?

static wedge
#

their movement is calculated in the bullet script. So their velocity determines their direction, no matter wich way the player faces.

unique ravine
#

If it does not matter you just need to set the direction once, right?

static wedge
#

their direction should be changed when the player changes direction

mystic wharf
#

Create function something like player_changed_right in the bullet
after you instantiate the bullet, do changed_right.connect(bullet.player_changed_right)
then since you already emit the signal, you should be good

unique ravine
#

You just said the opposite

#

I give up

static wedge
#

when did I

unique ravine
#

their movement is calculated in the bullet script. So their velocity determines their direction, no matter wich way the player faces.

static wedge
#

yeah, that's how it is right now and I want to change that

unique ravine
#

So, AFTER the bullet was shot, you want it to change direction based on player’s scale.x?

static wedge
#

I want it to change direction when the player turns in said direction, not if it's shot

unique ravine
#

Wdym not if it’s shot?

#

Bullet would not exist then

static wedge
#

do you get what my problem is?

unique ravine
#

Yes. Exactly like I imagined and you don’t need signals

mystic wharf
#

you can just do bullet.vvalue = scale then, you dont need signals

unique ravine
#

Like I said up there

#

Set the vvalue in the line after you instantiate the bullet

#

bullet.vvalue = scale.x

#

It does not matter if it is a different script. You have access to it while you have the bullet variable

static wedge
#

like this?

unique ravine
#

Yep

static wedge
#

am I missing something? because that's not it.

unique ravine
#

Your scale is always -1

static wedge
#

see that's another strange thing:

unique ravine
#

You should set to 1 the second time

#

You are setting to -1 in both cases

static wedge
#

it seems that it actually somehow multipies by -1 or something... because if I logically set the second one to 1 the player doesn't scale correctly, if I set to -1 on both, it does

#

like you can see in the vid that the player scales correctly

unique ravine
#

That does not make sense

static wedge
#

if I set to 1:

unique ravine
#

Add a print to check the value of scale.x when you shoot

static wedge
#

doesn't change while shooting, also scales wrong if I don't shoot at all

unique ravine
#

Humm. Try to get rid of the scale logic maybe. I usually use flip_h on the Sprite2D.

#

And either have 2 bullet spawn points or add the spawn point to a node and flip just that node

static wedge
#

But I have to flip the whole player for its children and everything

unique ravine
#

Life is hard

static wedge
#

hmm

#

I might actually check the printed scale values

#

interesting... so the scale value itself outputs correctly

#

Although it doesn't flip the Player Object back at all

#

It's getting late... I should probably come back to it tomorrow

#

I hate this project now xD

#

hopefully someone will rescue me from this nightmare eventually

unique ravine
#

Zip your whole project. It’s easier

static wedge
#

right

unique ravine
#

I can check tomorrow. Going to bed now. You should too. You are gonna se clearly tomorrow

static wedge
#

we'll see

unique ravine
#

I don't know what is happening with the scale. So I did what I have suggested before.

#

might not be what you wanted, but works

static wedge
#

I deeply appreciate it. You didn't have to do this. Thank you very much

#

I might not know excatly what you did but I can proceed now

unique ravine
#

Instead of changing the scale of the character, I used flip_h for the Sprite.

#

And for the bullet spawn point I used scale.x, but I added a node as its parents to do so

static wedge
#

ah I see

#

So the spawn point isn't a child of the player, but to another node in the player scene