Jump mechanics and Platypus

#1

Hello,
Today I am trying to create a jumping mechanic and I am a bit lost. I do not know exactly how I would do it. I have looked at many different posts and assets and I am still lost. Here is what I have…

local min_y = 30
local max_y = 680
local min_x = 30
local max_x = 1225


function init(self)
msg.post(".", "acquire_input_focus") 
self.vel = vmath.vector3()     

end

function update(self, dt)
local pos = go.get_position() 
pos = pos + self.vel * dt 

self.vel.x = 0
self.vel.y = -200

if pos.y < min_y then
	pos.y = min_y
elseif pos.y > max_y then
	pos.y = max_y
end

if pos.x < min_x then
	pos.x = min_x
elseif pos.x > max_x then
	pos.x = max_x
end
go.set_position(pos) 
self.vel.x=0

end
function on_message(self, message_id, message, sender)
    --Handle collision
    if message_id == hash("contact_point_response") then
    	--local newpos = go.get_position() + message.normal * message.distance
	--go.set_position(newpos)
	self.vel.y=0
end
end



function on_input(self, action_id, action)
if action_id == hash("up") then
	self.vel.y = 400
elseif action_id == hash("down") then
	self.vel.y = -400
elseif action_id == hash("left") then
	self.vel.x = -400
	sprite.set_hflip("#sprite", false)
elseif action_id == hash("right") then
	self.vel.x = 400
	sprite.set_hflip("#sprite", true)
elseif action_id == hash("down") then
	self.vel.y = -400
end
end

Now I don’t quite know how to implement a jump, and I have read over multiple posts and I still feel lost…
Eventually, I would like to be able to add double jumps and wall jumps, but I can’t even get a normal jump haha.
If anyone could take a look at my code and let me know what I can add and/or tweak with what I have it would be greatly appreciated.

Also, I have taken a look at platypus and it seems extremely convenient (and I would love to use it) but I’m not quite sure how it works. I have added the library dependancy and copied and pasted in the code that was listed under usage at this site. If anyone has insight on using platypus or creating my own code or directing me with platypus it would be very appreciated
(sorry for all the questions lately, I have just been a bit lost.)

0 Likes

#2

There’s a full example if you download the entire Platypus project and open it in Defold:

1 Like

#3

Thanks! I viewed it in Defold and I found in the player script that it was referring to “platypus.platypus,” “in.state,” and “in.mapper.”
Screenshot%20(53)

Would I have to add that, and anything else into my own game?
What things would I have to transfer to my game?
Thanks again for all the help!

0 Likes

#4

If you wish to base your game on the code from the Platypus example project then you need to make sure to add a dependency to Platypus plus whatever other depends that there are in game.project (like defold-input).

0 Likes

#5

So I added all the dependencies that are in the Platypus example project, as well as the Platypus dependency itself, into my own game.
Screenshot%20(55)

From there I added a basic movement script and attached it to my player game object.
Screenshot%20(60)

It returned these errors:

At this point, I decided to look at the Grottoescape example within the Platypus example and found that the player script was referring to a few different things:
Screenshot%20(62)

So I decided to add these to my player code, but it still returned the same errors…
I’m not quite sure what to do now. My goal is to just make platformer movement. I am very sorry for my little knowledge of the subject, and your assistance, guidance, and patience with me is very much appreciated.

0 Likes

#6

Zip your project and share it here so that I can take a look (exclude the .git , .internal and build folders from the archive).

1 Like

#7

Here is the project. The script and game object is under the “test level” folder. Thanks again so much for all your help.

archive_150716_HEAD.zip (201.0 KB)

0 Likes

#8

I opened the project and ran it. The console showed the following:

    INFO:DLIB: Log server started on port 61228
    INFO:ENGINE: Engine service started on port 61229
    INFO:ENGINE: Defold Engine 1.2.159 (f7d0a1b)
    INFO:ENGINE: Loading data from: build/default
    INFO:ENGINE: Initialised sound device 'default'

    ERROR:SCRIPT: /platypus/platypus.lua:58: You must provide a config
    stack traceback:
    	[C]: in function 'assert'
    	/platypus/platypus.lua:58: in function 'create'
    	/test level/character/platypus script.script:7: in function </test level/character/platypus script.script:6>

This is a pretty clear error message. The platypus library gives an error saying that “You must provide a config” and it even mentions in which file and line the error originated from:

/test level/character/platypus script.script:7: in function </test level/character/platypus script.script:6>

Line #6 in platypus script.script is this:

1. local platypus = require "platypus.platypus"
2. local input = require "in.state"
3. local input_mapper = require "in.mapper"
4.
5. function init(self)
6. 	self.platypus = platypus.create(config)
7. end

And the problem is that config is undefined. You must provide a configuration table. This is done in the example:

0 Likes

#9

Thank you so much! That really helped! A few more things then I will be out of your hair:

The collisions seem a bit out of wack, when the gameobject lands on tiles it does not collide when it should, and created a product that looks like this:

Here is the current version of the game if needed…
archive_150716_HEAD (1).zip (201.4 KB)

The next thing, would it be possible to tweak some of the input for mobile gameplay? How might I go about doing that?

And finally, I believe to keep dependency changes permanent I would have to copy all of the scripts and files from the dependencies into the game itself? I remember having an issue with it once.
Thank you so much again for your time and help!

0 Likes

#10

It is likely that the collision object shapes are wrong or that you haven’t configured Platypus properly (check the config table that you passed to Platypus!). Start by enabling physics debug so that you see the shapes:

There is also a debug setting on Platypus that you can enable.

What do you mean specifically?

If you make changes to files that belong to a dependency then you need to make a copy of the library. But you really shouldn’t change any files belonging to your dependencies.

0 Likes

#11

Thanks very much for the debug help, I was able to figure out the problem!
When I was talking about mobile game play, I was wondering if, using the platypus dependency, would it be possible to create a mobile game? Would input (like arrow keys or wasd) somehow be able to be altered for use on a mobile device. I was thinking something like buttons on a mobile screen. Would it be possible to somehow use buttons on a mobile screen rather than arrow keys or wasd?

0 Likes

#12

Yes, this is of course possible. Platypus doesn’t require input to come from a specific source. In fact Platypus doesn’t care at all. You call functions such as platypus.left(), platypus.right() and platypus.jump() to control the player. You can use keyboard, gamepad or on screen buttons to detect interaction and call the appropriate platypus functions.

0 Likes

#13

Hey that’s great! So if I were to have an onscreen button and every time it was clicked it was to call platypus.left() or platypus.right() and send that message to the player.script, it would move the character? If that’s the case this is great! Thanks so much for all your help! Sorry if I have been driving you a bit nuts but you have really helped me!
Have a good one!

0 Likes

#14

Yes, that’s how it would work. The on-screen buttons will exist in a gui component and when pressed they will send messages to your player script which in turn will call platypus functions.

0 Likes

#15

Thanks so much!

0 Likes

#16

Hello,

I have recently returned to this topic as my Apple Developers Program’s info is pending and I have run into a bit of an issue…
I have managed to make both the left and the right buttons work. All I had to do was move them from the on_input function over to the on_message function and change action_id to message_id, but I could not figure out how to do the same thing with the platypus jump command since it uses action.pressed and action.released.

Screenshot%20(71)
This is my script. It works just fine with the left and right functions but I cannot figure out how to make the jump function work. (These lines of code are inside the on_message function)

Thanks so much for your help!

0 Likes

#17

You’d have to send if the GUI button was pressed or released along with the message. Something like msg.post("/player", "jump", {pressed = true, released = false}). So you might put

function on_input(self, action_id, action)
   if action_id == hash("touch") then
      if gui.pick_node(jump_button, action.x, action.y) then
         if action.pressed then
            msg.post("/player", "jump", {pressed = true, released = false})
         elseif action.released then
            msg.post("/player", "jump", {pressed = false, released = true})
         end
      end
   end
end

in your GUI script and replace action.pressed and action.released in the player script with message.pressed and message.released. Of course, you’d have to replace some of the above code to fit with your existing scripts.

1 Like

#18

On paper, this seemed like a great idea and I was very excited to give it a go!
Here is the script:

The problem is though, I keep getting this error:
Screenshot%20(74)

Not sure what I did wrong here. :grimacing:

0 Likes

#19

You’re missing an end right above print("jump!") and you need to remove the action. in the msg.post's like so:

msg.post("/character#platypus script", "jump", {pressed = true, released = false})
...
msg.post("/character#platypus script", "jump", {pressed = false, released = true})
1 Like

#20

Did you check the example that comes bundles with Platypus? There’s a player script in there that you can take a look at:

1 Like