Hi! So, I just started using Godot, and my idea for a game was a platformer where you play as a little water blob who can become either gas or ice. The problem is, I can't figure out how to make a code to make my character change. Anyone can help?
Help with making my character changing into another
I would just have 3 different sprites and switch their visibility using a state machine.
But I wouldn't worry about that now. I'd start with just learning to build a simple character controller for one of them, then for the other and so on until I had 1 completed individual character controller for each of these. Then I'd start thinking about how to integrate them.
I kinda already have a control scheme for each of them, all I need now is for the character to swap out. also I already tried this state thing but it said there was an error. I t said delta isn't declared in the current scope or something
The delta in the above video comes from the process function. If you are using this outside of process or physics_process then using delta makes no sense. To better help you we'd need to see your code.
- Edited
Here it is:
extends KinematicBody2D
enum {
WATER,
GAS,
ICE
}
var velocity = Vector2(0,0)
const speed = 300
const gravity = 30
const jump = -700
var state = WATER
func physicsprocess(delta):
if Input.isactionpressed("right"):
velocity.x = speed
$Sprite.play("Walk")
$Sprite.fliph = false
elif Input.isactionpressed("left"):
velocity.x = -speed
$Sprite.play("Walk")
$Sprite.fliph = true
else:
$Sprite.play("Idle")
velocity.y = velocity.y + gravity
if Input.is_action_just_pressed("Jump") and is_on_floor():
velocity.y = jump
$Sprite.play("Jump")
if not is_on_floor() and velocity.y < -200:
$Sprite.play("Fly")
if not is_on_floor() and velocity.y >= -200 and velocity.y <= 0:
$Sprite.play("Fall")
if not is_on_floor() and velocity.y > 0:
$Sprite.play("Fall1")
if not is_on_floor() and velocity.y > 100:
$Sprite.play("Fall2")
velocity = move_and_slide(velocity,Vector2.UP)
velocity.x = lerp(velocity.x,0,0.2)
func state(state):
match state:
WATER:
physicsprocess(delta)
GAS:
pass
ICE:
pass
- Edited
Line 15 should be func _physics_process(delta):
not func physicsprocess(delta)
_process()
and_physics_process()
are special engine functions that processed on each frame/physics-frame the engine produces the delta time variable for you to know via the time delta between previous and current frame how much time has passed and feeds into them for your use. If you misspell it then you won't get delta, so no wonder it doesn't work. ;)
Also the first if/elif chain uses .ifactionpressed()
instead of .if_action_pressed()
and you probably don't want to call _physics_process()
from another function since the engine already processes it on every frame.
It still gives the same error: "The identifier "delta" isn't declared in the current scope."
Fixed it! Dumbass me wrote the state(delta) wrong
Still need help changing the character tho...
Now within the state machine you would switch visibility of the sprite nodes and implement the input controls for each of the states.
Yeah, I think I figured it out. Just need to find a way to change the collision shape now. Do you know how can I do that?
You could have multiple CollisionShape nodes and then enable/disable them using the disabled property. I've done this a few times for changing collision shapes based on states and it's worked okay.
Worked like a charm! I just don't know why the gas one is invisible. Could it be the negative gravity?
FIgured it out
@"Adrian Monky" said: FIgured it out
Could you mention how you resolved the problem? This is helpful in case someone else stumbles upon this post using a search engine :)