Any way to have Function save its Args for later use?

Discuss any general programming issues here
Bugala
Posts: 1168
Joined: Sun Feb 14, 2010 7:11 pm

Any way to have Function save its Args for later use?

Post by Bugala »

Here is a code example that does not work the way I would want to:

Code: Select all

n = {}

prt = "hello"

n[1] = Function() DebugPrint(prt) EndFunction

prt = "world"

n[2] = Function() DebugPrint(prt) EndFunction
	

n[1]()
n[2]()
Idea would be that I would want this code to debugprint first "Hello" and then "World". But is there any way to achieve this?
SamuraiCrow
Posts: 475
Joined: Fri May 15, 2015 5:15 pm
Location: Waterville, Minnesota USA

Re: Any way to have Function save its Args for later use?

Post by SamuraiCrow »

Your prt variable is global so you aren't actually passing a parameter. If you stuff your parameters in a global table instead of garbage collecting them, then it will work.
I'm on registered MorphOS using FlowStudio.
Bugala
Posts: 1168
Joined: Sun Feb 14, 2010 7:11 pm

Re: Any way to have Function save its Args for later use?

Post by Bugala »

hmm.. This is interesting. I didnt expect this to work just by changing them to local, but it does:

Code: Select all

n = {}

Local prt = "hello"

n[1] = Function() DebugPrint(prt) EndFunction

Local prt = "world"

n[2] = Function() DebugPrint(prt) EndFunction
	

n[1]()
n[2]()
Is this really a safe way to do it, or is there a chance that garbage collecting will remove n[1] prt state away?

For it seems strange to me that putting them both to local makes it work, but using only one local declaration doesn't make it work.
Bugala
Posts: 1168
Joined: Sun Feb 14, 2010 7:11 pm

Re: Any way to have Function save its Args for later use?

Post by Bugala »

By the way, explaining bit more what I am after with this.

I am having this movement where it is moving through a certain path. However, that path is done step by step. That while it is moving 5 steps, it is actually moving 5 times, 1 step.

As I was thinking what to be the best way to get it done in this case, I came to bit different kind of solution than I have done before.

Idea is that each step is going to be OOP.

And basically these OOPs have so called EndFunc on them.

This means that when OOPs task is complete, it will execute its EndFunc.

This EndFunc in practice is going to be starting the next step, except for the last one.


So basically there is this for-next loop that does something like:

Code: Select all

for n = 5 to 1 step-1
	if n=5
		endfunc = function() OneToUseAfterLastStep endfunction
	else
	        endfunc = function() CreateNextStep(n, n+1, EndFunc) endfunction
	endif
next
	CreateNextStep(n, n+1, EndFunc)
And idea is that when step 1 ends, it will start step 2, when step 2 ends, it will start step 3 and so on, until step 5 ends them all.

Problem now comes that all these steps are created in this same spot. Therefore what needs to happen is that there will be 5 different CreateNextStep() function with different args:

Code: Select all

CreateNextStep(1, 2, EndFunc(with step 2 args)
CreateNextSTep(2, 3, EndFunc(with step 3 args)
CreateNextStep(3, 4, EndFunc(with step 4 args)
CreateNextStep(4, 5, EndFunc(with last step args)
And then at a later point, these will be simply called using:

Code: Select all

If taskfinished=true
self.EndFunc()
I can use a different solution for this, but this would seem like the current best idea I have to solve the problem.
jalih
Posts: 276
Joined: Fri Jun 18, 2010 8:08 pm
Location: Finland

Re: Any way to have Function save its Args for later use?

Post by jalih »

Bugala wrote: Mon Dec 21, 2020 5:34 pm I can use a different solution for this, but this would seem like the current best idea I have to solve the problem.
Sounds horribly complex! Why not just manage simple move list for object?
Bugala
Posts: 1168
Joined: Sun Feb 14, 2010 7:11 pm

Re: Any way to have Function save its Args for later use?

Post by Bugala »

Movelist kind of thing is basically my plan B.

This is bit complex way of doing it, but it also has its benefits, especially the point that it lives quite independently from everything else and is somewhat flexible.
Bugala
Posts: 1168
Joined: Sun Feb 14, 2010 7:11 pm

Re: Any way to have Function save its Args for later use?

Post by Bugala »

Bugala wrote: Mon Dec 21, 2020 5:24 pm hmm.. This is interesting. I didnt expect this to work just by changing them to local, but it does:

Code: Select all

n = {}

Local prt = "hello"

n[1] = Function() DebugPrint(prt) EndFunction

Local prt = "world"

n[2] = Function() DebugPrint(prt) EndFunction
	

n[1]()
n[2]()
Is this really a safe way to do it, or is there a chance that garbage collecting will remove n[1] prt state away?

For it seems strange to me that putting them both to local makes it work, but using only one local declaration doesn't make it work.
@Andreas

Can you confirm if this is actually a safe way of doing something like this, or if program is long is it possible that after for example couple of minutes the garbage collection comes and removes n[1] prt state away?
jalih
Posts: 276
Joined: Fri Jun 18, 2010 8:08 pm
Location: Finland

Re: Any way to have Function save its Args for later use?

Post by jalih »

Bugala wrote: Mon Dec 21, 2020 7:48 pm Movelist kind of thing is basically my plan B.

This is bit complex way of doing it, but it also has its benefits, especially the point that it lives quite independently from everything else and is somewhat flexible.
Trust me, it's a nightmare! If you want to use OOP, why not just make move function for a single move step? If your unit moves five times, you just call it five times? Plain and simple, easy to follow and debug, If needed.
Bugala
Posts: 1168
Joined: Sun Feb 14, 2010 7:11 pm

Re: Any way to have Function save its Args for later use?

Post by Bugala »

well, actually this is the one step approach. This is about how to execute that move step 5 times.

What I am currently looking at is that basically the moving is initiated very simply. I am simply telling which unit is moving, and sending its pathway, which is simply numbers like 102, 103, 123, 124, 144.

as example "MoveUnit(unit, path)"

Then this function initiates those steps which in simple is:

MoveOneStep(102, 103)
MoveOneStep(103, 123)
MoveOneStep(123, 124)...

Easy so far, but problem comes that I cant initiate each step at same time, or even one after another, but things happen first.

Like when that one movement is happening, there is moving animation. And problem with that is that once again the moving animation is not happening alone, but all other things need to be happening at same time too. Like other animations in that game need to be working at the same time.

Therefore what really happens is that the first MoveOneStep(102, 103) is initiated, and when it finishes its movement in multitasking, then it needs to start the next MoveStep(103, 123), and so on.

And one extra hassle comes from the need that while the first four MoveSteps are going to be initiating the next MoveOneStep, the 5th MoveOneStep needs to initiate something else, the EndMovement procedure so to say.

Therefore I came to conclusion that so far the easiest solution seems like MoveOneStep OOP which has EndFunc variable, which would contain the next MoveOneSteps initiation, which in turn would have EndFunc variable that contains MoveOneStep initiation all the way until the last one would instead of having MoveOneStep have the FinishMovement function call.

by otherwords there is MoveOOP that basically has UpDate function which keeps doing the animation and checks if the move animation have completed, and if it did, then it starts the EndFunc.

The flexibility this gives is that I can also have different kind of units, for example maybe I want a bomber unit that first moves 3 steps, then drops a bomb to next 3 steps, and then moves 3 more steps again.

Now I can construct that by putting those endfunc variables accordingly.

First three and 7 and 8 would have:
MoveOneStep(a, b)

while 4 - 6 would have:
DropBomb()
MoveOneStep(a, b)

and 9 would have:
FinishMovement()

So far this looks like the best approach to me given all the things I need to take into consideration, despite it being bit complex.
jalih
Posts: 276
Joined: Fri Jun 18, 2010 8:08 pm
Location: Finland

Re: Any way to have Function save its Args for later use?

Post by jalih »

Bugala wrote: Sun Dec 27, 2020 4:27 pm Easy so far, but problem comes that I cant initiate each step at same time, or even one after another, but things happen first.

Like when that one movement is happening, there is moving animation. And problem with that is that once again the moving animation is not happening alone, but all other things need to be happening at same time too. Like other animations in that game need to be working at the same time.
Handle movement one step at a time, you can animate during render loop! Keep it simple!

Here I handle movements during the update loop but units slide into tiles during the render loop.
Post Reply