#Help With My Combat Game [Global Position, Use of key twice]

61 messages · Page 1 of 1 (latest)

tame crater
#

Hello! I'm making a combat game and I'm stuck on 2 things.

**1. ** The player will have 2 different modes: Normal mode and Attack mode. When you press "E" you activate attack mode. How would I turn attack mode off when you press "E" again?

2. The game has 2 players and with attack mode, you're always facing the second player. How can I see a player's position from a different scene?
[For example: Access Player 2's position in Player 1's code]

Code:

        AttackMode = true

    if AttackMode == true:
        if self.position.x <= 640:
            $Image.set_flip_h(false)
            $Area2D/CollisionShape2D.position.x = 30

        if self.position.x >= 640:
            $Image.set_flip_h(true)
            $Area2D/CollisionShape2D.position.x = -26```

Thanks!
#

Help With My Comabt Game [Global Positon, Use of key twice]

#

Help With My Comabt Game [Global Position, Use of key twice]

noble blade
short zephyr
#
  1. you can do AttackMode = !AttackMode
  2. Get_node works, but depends on the hierarchy, if the second player is always the child of the same node as the first player, then get_node("../Player2sNodeName") works
#
  1. You can also do get_parent().get_node("Player2sNodeName")
#

replace Player2sNodeName with the name of player 2's node, obviously

tame crater
#

Thank You @noble blade and @short zephyr!

#

Help With My Combat Game [Global Position, Use of key twice]

#

@short zephyr Wait would this work between 2 scenes?

short zephyr
#

as in?

tame crater
#

Because I have Player 1 and Player 2 as separate scenes

short zephyr
#

as long as they're active at the same time and are the children of the same node then yea

#

being inside of a scene counts as being a child of the root node of that scene btw

tame crater
short zephyr
#

not the parent, the child

#

those are... basically the opposites

tame crater
#

I meant child, sorry

short zephyr
tame crater
short zephyr
# tame crater ?

like can you send me a screenshot of how the scene where the players are loaded in is set up?

#

the left "scene" tab

#

y'know where you can see the scene tree

tame crater
#

ah kk

short zephyr
#

and by basically i mean literally lol

tame crater
short zephyr
#

ah i see

#

just a common misconception wanted to get that out of the way just in case

tame crater
#

This the main scene

short zephyr
# tame crater This the main scene

they are the child of the same node though, the scene root ("Main" node) is a node, and you can access the player nodes as children of that node

tame crater
#

Because the code is written inside of the Player 1 script

short zephyr
#

if you'd were to run player 1 as a scene (so nothing is loaded except player 1) then yes it wouldn't work

short zephyr
#

if you right click on Player 1 and press "run this scene" then it won't work because if that part of the code is run and it tries to reference a node with the relative path of "../Player 2" it won't find a valid instance
if you right click on "Main" and press "run this scene" then it will work because at the time that that line of the code is run the reference is a valid instance

tame crater
#

Like this is the code

short zephyr
#

think about it this way: you're not setting player 2 to be a variable that always exists within the player 1 code, you're instead searching for the player 2 node during the gameplay

tame crater
#

But I need the code to run like this

short zephyr
#

also no need to do self. there

#

position.x will do the same thing as self.position.x

tame crater
tame crater
#

That's what I'm trying to get

short zephyr
short zephyr
#

you will know the position of player 2

#

you can't have it before runtime because player 2's position just doesn't exist at that point

#

ill try to explain

tame crater
#

Oh wait hold on

#

I think it works

#

One second

short zephyr
#

i mean yeah that's not the problem, i wanna explain it to you in a clear way

tame crater
#

It works!

short zephyr
#

Player 2's position is a variable that will change during runtime.
Player 2 as a node doesn't exist within the Player 1 "scene", therefore you don't know Player 2's position. HOWEVER, you don't need player 2's position BEFORE the game is run.
When the game is run, both Player 1 and Player 2 exist. You are able to find what player 2's position is by referencing player 2 as a node (for the sake of this being easier to understand - think of it as opening a file, but not literally) and grabbing it's position property (like reading the contents of said file).
At this point, you know player 2's position, and are able to use it

#

is this clear? or is there something that needs explaining?

tame crater
#

Thanks Mate!