Log In  

Hello everyone,

I am working on some test code for a game idea and it involved picking up one object at a time. Now my code functions properly for the most part but for some reason the p.detect won't trigger for the key but it will for the gun. And the key can't be placed on top of the gun (correct) but the gun can be placed on top of the key (not meant to happen).

If someone wouldn't mind taking a look for me, most of the code I'm currently working with in is the objects functions and the player functions.

The boolean values are:

p.detect
p.held
key.detect
key.held
gun.detect
gun.held

Cart #yonezijiko-0 | 2022-01-09 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA

P#104678 2022-01-09 19:43

I'm posting a fairly minimally changed version.

  • I've reworked the _draw feedback to be a bit more informative.
  • I've set p.detect to false in player_update(), so that each frame it is detecting everything afresh. This is just my preference; I'm not sure it would always be necessary - if this was kept accurate at every pick-up and drop and allowed to run from frame to frame, as I think it was, it could work - but this way helps me to reason and debug.
  • I've done detection and grabbing separately, they're now called from within _update(), so all the detection is done before anything is attempted to be grabbed.
  • I've removed a line in obj_detect() that set p.detect to false - it's now done in every frame in player_update() as mentioned above, which because it runs before obj_detect() is called, means that if anything is detected, p.detect becomes true, and because we're doing all the detecting before the grabbing, p.detect will be true when it needs to be.
  • I've commented out object_update() because it is no longer being used.
  • I've uncommented blue to test it with three objects.

What I've done is to follow a pattern that goes (in very informal pseudo-code):

collision_flag=false --player_update()

for all items of interest
  --obj_detect()
  if collision() then collision_flag=true end
end

-- collision_flag will be true if we collided-with/detected any item of interest

for all items of interest
 --obj_grab(), unchanged
 do something based on collision_flag and other conditions
end

There is a difference in how it plays compares to how it was playing, which is that you now can't put down an item and immediately - as a swap - pick up the other item (which was happening with the key being put down on the gun). You have to put an item down slightly away from whatever you want to pick up next. By sorting out the bug (at least the way I've sorted it) it naturally leads to this change. That's not to say it couldn't be made to swap an item for an item on the ground, but now the bug is removed the logic from the original code in obj_grab() prevents it - that logic would need to be changed if you did want to swap an item you were holding for one detected on the ground; I'm not going to speculate on the logic for that.

P#104694 2022-01-09 23:39

@remcode Thank you so much for the assistance! Glad to know it was just me not putting p.detect in the players logic rather than something else wrong with my system. Still learning about all this tableception type stuff lol

P#104700 2022-01-10 01:15 ( Edited 2022-01-10 01:17)

[Please log in to post a comment]