Discord
Login
Community
DARK THEME

What am I doing wrong trying to update lists of coordinates at specific time interval ??

So I'm trying to make ghosting effect and in order to do that I need to store previous coordinates. I tried various ways to make timers I even copied one from tutorial on site tried to make use of Schedule function built in. tried after, every, do, sleep but that just frizzes the game like the whole game is sleeping not just function.

I'm calling this function from updateplayer witch is in turn called by update... Tell me what am I doing wrong

Here is function:

updateGhostPositions = function(obj, ghostTimer)
  if ghostTimer != 0 then
  ghostTimer -= 0.5 // do not tick lower than 0.5
  else if newGhostTimer == 0 then
      print(newGhostTimer)
      obj.gx.insert(obj.x)
      obj.gy.insert(obj.y)
      if obj.gx.length > 21 then
        obj.gx.pop()
        obj.gy.pop()
      end
    ghostTimer = time
    print(ghostTimer)
    end
  end
end

Solved if you are calling sleep from update function it will be called 60 times per second so the sleep will never end. You need to call it only once but in update function that is impossible at least to my knowledge. Advise do not use math for time measurement it will end up badly.

So what did I do:
initialized global variable to stop function from infinite loop and put my code in every x do statement

UGPstarted = 0
updateGhostPositions = function(obj, ghostTimer)
  if UGPstarted == 1 then
    return
  end
  if UGPstarted == 0 then
    UGPstarted = 1
    every 100 milliseconds do
      obj.gx.insert(obj.x)
      obj.gy.insert(obj.y)
      if obj.gx.length > 5 then
        obj.gx.pop()
        obj.gy.pop()
      end
    end
  end
end

Thanks for help and have a nice one. P.S. I really do hope that this will be helpful to someone.

As mentioned for other help requests, it is really hard to guess what is going on without seeing the rest of the code. It would be best to publish the project (unlisted!) and post the link here.

@TinkerSmith here is the link to the project.

Thank you in advance.

At work and on my phone at the moment, so can't do much. So what is the ghost supposed to do? To follow the player, or is the idea to have a ghost trail behind the player?

If you want a trail, then maybe have a look at the particle system in 'Space Bur-Bur' as a start.

Other thing I noticed in your decAlpha() function:
In the first line you define local alpha = 0.9 , so it will be reset to 0.9 every time you call the function.
As in ... you always get a return value of 0.8 ... maybe use a global alpha instead? As mentioned, it depends on what the ghost is supposed to do (or are there multiple ghosts?)

I might have a look tonight when I'm home. But maybe you're lucky and someone else has a go at it :)

Cheers

Live Long and Tinker

P.S. I think the same happens with your ghostTimer, since you use it locally in your function it will always stay at 10 with every call.

@TinkerSmith Thank you for your time.

So what is the ghost supposed to do? To follow the player, or is the idea to have a ghost trail behind the player?

idea is well yeah more like a ghost trail behind the player and enemies so that's why I'm trying to make more flexible function but I'm C developer and not that good with OOP (objects and constructors). Particle system in "Space Bur-Bur" is good example thanks for pointing that out.

Other thing I noticed in your decAlpha() function: In the first line you define local alpha = 0.9 , so it will be reset to 0.9 every time you call the function. As in ... you always get a return value of 0.8 ... maybe use a global alpha instead?

decAlpha() function isn't in use I figured out better way of handling alpha but yeah you are right that is what is happening in that function
but not with timer I will further describe issue with timer in continuation of my reply.

As mentioned, it depends on what the ghost is supposed to do (or are there multiple ghosts?)

Yeah I want multiple ghosts and depending on players velocity aka is player holding run or not trail to get longer or shorter that's why I need something like timer so if player isn't holding run wait smaller amount of time before each save of coordinates(updates in list) and if player is holding run wait longer before each save of coordinates in that way I will have control in trail shall I say length.

I think the same happens with your ghostTimer, since you use it locally in your function it will always stay at 10 with every call.

Well no I tried using global variable it didn't help, the code is a mess I know but when you are trying to do something so seemingly simple for 7h straight that could happen. I've also tried making while loop for timer something like:

  while ghostTimer != 0 
    ghostTimer -= 0.1 

to assure that timer must go down but that just oscillates between 0 and whatever number is defined for wait time
(maybe in while loop it decreases too quickly I will try and see if that's the problem)

also I tried simplest solution sleep but that makes whole game "fall asleep" like it's affecting update function itself.
(maybe to make whole func looping/safe looping, like if its already active to just return and inside of that while true loop to set sleep)

P.S. If u can help, please and if not thanks anyways your response was really helpful cause I was stuck now at least I have new ideas for solving this.

To anyone with same or similar issue if I fix this I will update this tread and let you know the solution.

Haven't had time to look further into it, was coming home late and ready to drop into bed. I got some ideas, will try tomorrow. ( I work at a hospital and work times can be unpredictable...)

You add the current item to the list 30 times.

 while ghostTimer > 0
   ghostTimer -= 1/60
   if round(ghostTimer) == 0 then

it doesn't make sense because you count from 10 to zero in 1/60 steps. The condition will be executed anyway when the value drops below 0.5.

Since the list will only have 21 items, all data from the list "gx" and "gy" will have the same value. You move the sprite in the same place.

updateGhostPositions = function(obj, ghostTimer)
//  while ghostTimer > 0
//   ghostTimer -= 1/60
//   if round(ghostTimer) == 0 then
      //print('yeaaaaaaaaaaaah')
      obj.gx.insert(obj.x)
      obj.gy.insert(obj.y)
      if obj.gx.length > 21 then
        obj.gx.pop()
        obj.gy.pop()
      end
//    end
//  end
end
drawGhosts = function(obj, color)
  // Combine color and alpha
  for i = 0 to 20
    Alpha = decAlpha()
    local alphaTime = (sin(system.time() * 0.002) + 1) / 2
    local adjustedAlpha = clamp(glowTime,0.3,0.7)
    local adjustedColor = 'rgba('+color+', '+adjustedAlpha+')'
    local fullAlpha = 'rgba('+color+',1)'
    print( "gx["+i+"]="+obj.gx[i]+"  gy["+i+"]="+obj.gy[i] )
  
    screen.drawColored(obj.sprite, obj.gx[i], obj.gy[i], obj.w, obj.h, adjustedColor)
  //  screen.drawColored(obj.sprite, obj.gx[10], obj. gy[10], obj.w, obj.h, adjustedColor)
   // screen.drawColored(obj.sprite, obj.gx[15], obj.gy[15], obj.w, obj.h, adjustedColor)
    //screen.drawColored(obj.sprite, obj.gx[20], obj.gy[20], obj.w, obj.h, adjustedColor)
  end
end
  

do this test - you will see the sprite

    screen.drawColored(obj.sprite, obj.gx[i]-i, obj.gy[i]-i, obj.w, obj.h, adjustedColor)

@Loginus I have different issue so the thing that I'm struggling with is time delay.

You add the current item to the list 30 times. it doesn't make sense because you count from 10 to zero in 1/60 steps. The condition will be executed anyway when the value drops below 0.5.

Your mathematics is correct I understand that, but when you print numbers you will see that rounded(ghostTimer) is equal to zero 3/5 times max and idk why is that also that was testing from few hours ago I will try now do while true sleep method as noted in my previous response any ideas are welcome. It is not problem that I don't see ghost sprite.
Ghost sprites are present but they are too close one to each other.
So what is solution: add time delay in update of coords list. That way I will have sprites more far apart.

Please read response that I wrote to @TinkerSmith I think that I explained issue there pretty well.

solved

I've updated the thread description.

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community