checkpoint with signals

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By mister_why

Hi !

I’m trying to make a checkpoint for my platformer game,

so for that I have a Player and an Area2D.

My logic is : when the player “enter” in the Area2D, the variable checkpoint take the global position of the checkpoint.

I tried with signals :

Area2D’s script :

var player_node = get_node("/root/World_0/Player")
self.connect("body_entered", player_node ,"test")

Player’s script :

checkpoint = get_node("/root/World_0/Checkpoint").global_position

And, of course, it doesn’t work

Do you have any idea why ?

Thanks folks !

did you implement method test in your player script?

Bartosz | 2018-03-22 15:20

Yes I did sorry I copy and paste to quickly

mister_why | 2018-03-22 21:26

:bust_in_silhouette: Reply From: Bartosz

How to make checkpoint using signals

assumptions:

  • player_node is set in editor to correct value e.g "/root/World_0/Player"
  • player is of type of kinetic or rigid body
  • checkpoint_node is set in editor to correct value e.g "/root/World_0/Checkpoint"
  • checkpoint_area changes position of checkpoint
  • new checkpoints position is inside checkpoint_area
  • script will be attached to checkpoint_area

To easily create multiple checkpoints we attached script to checkpoint_area because thanks to that if we duplicate checkpoint_area everything will work without any additional steps

code

extends Area2D

export(NodePath) var player_node
export(NodePath) var checkpoint_node
var player
var checkpoint
func _ready():
	player = get_node(player_node)
	checkpoint = get_node(checkpoint_node)
	# connect signal body_entered
  # to this area's method named maybe_player_entered
	connect("body_entered",self,"maybe_player_entered")

func maybe_player_entered(maybe_player):
	# check if it is player who entere area
	if maybe_player == player:
		# set checkpoint position this area position
		checkpoint.global_position = global_position
		
	# now optionaly disable previous checkpoint areas if no backtracking

rewrite code by hand instead of copy-pasting gives your brain changes to learn something new for the future use, so choose wisely how you apply this code to your games

Thanks for the reply but I got a problem at the third line already …

export("/root/World_0/Player") var player_node

I got : “Expected type for export”

And I though that the function that call connection has to be in Player, everytime I saw an exemple, it was that way :confused:

mister_why | 2018-03-23 07:37

leave export variables unmodified in code, and use editor to set values: select checkpoint_area and in inspector on top you will see two properties representing player_node and checkpoint_node, set them accordingly. That what export is for (modifying values with source code intact) here is graphical illustration link

to understand why I put this script on area and not on player image those two
scenarios:

  • (script on player) - player have method that set current checkpoint, and is connected to all checkpoint_areas on the scene. Now it gets signal body_entered that carries one piece of information body parameter. Player check what that body is, and guess what` 99% of the time it is player. Player now knows that he entered some check_point area but he has no clue witch one so he check every area on the scene to find out which one is overlapping his position. When he find that he set checkpoint appropriately. Of course he can cheat and do not search for areas position and just used hes his own (after all it can’t be far from actual design checkpoint right?)

  • (script on area) - area has script that modifies checkpoint. It gets signal body_entered that something entered its area. It checks if that thing is a player if it is area set checkpoint position to it(areas) position place where you gamedev intended it to be.

There is way to make script work in player in simple manner but it involves creating custom signals that will contain all necessary information. If you want to pursue that implementation I will happily show you, but that is to far away from main topic so ask another question to keep things organize.

Bartosz | 2018-03-23 18:14

Hi !

Sorry I’m late :confused:

I did everything you said but I can’t get it :confused:

Your gif confuse me, you use a Node2D (checkpoint_area) but there is no … area ?

And why can’t we attached the checkpoint on the World, but we do on the player ? :confused:

Very sorry I’m sure it’s logical for you but still not for me

Regards

mister_why | 2018-04-10 20:39