• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

How to generalize this function?

Status
Not open for further replies.
Level 15
Joined
Aug 7, 2013
Messages
1,338
Hi,

Suppose I've got several functions which all have the same locals and outer loop, but each function has a completely different inner loop.

Consider these two functions:

JASS:
function foo takes nothing returns boolean
  local integer i = 0
  local integer maxIterations
  local integer x
  local integer y
  local integer z
  loop
     exitwhen i == maxIterations
     //specific code here
     set i = i + 1
  endloop
endfunction

function bar takes nothing returns boolean
  local integer i = 0
  local integer maxIterations
  local integer x
  local integer y
  local integer z
  loop
     exitwhen i == maxIterations
     //specific code here
     set i = i + 1
  endloop
endfunction

So basically all the locals and outer structure is the same, but the inner calls of the loop change between functions. Is there anyway I can generalize this at all to make the code more elegant (e.g. a textmacro?).
 
Level 14
Joined
Jun 27, 2008
Messages
1,325
Depends on where the values for the locals come from, what happens inside the loop and whether you need the values of those locals in the rest of the function. Sure, you can use a textmacro, but textmacros are a horrible horrible thing, so why not just a function?

JASS:
function inner takes integer i, integer x, integer y, integer z returns nothing
     //specific code here, if you dont dont need i here remove it from the arguments
endfunction

endfunction

function foo takes nothing returns boolean
  local integer i = 0
  local integer maxIterations
  local integer x
  local integer y
  local integer z
  loop
     exitwhen i == maxIterations
         call inner(i,x,y,z)
     set i = i + 1
  endloop
endfunction

function bar takes nothing returns boolean
  local integer i = 0
  local integer maxIterations
  local integer x
  local integer y
  local integer z
  loop
     exitwhen i == maxIterations
         call inner(i,x,y,z)
     set i = i + 1
  endloop
endfunction

But since this solution is so obvious im not sure if I understood your question.
 
Level 15
Joined
Aug 7, 2013
Messages
1,338
Thanks Muzzel I think what you wrote is what I'm looking for. My question is vague because like you said

Depends on where the values for the locals come from, what happens inside the loop and whether you need the values of those locals in the rest of the function.

which I don't know 100% yet.

That's the solution I started using and I think I'll combine it with textmacros.

And why are textmacros horrible? They save a lot of screen real estate, which is why I asked this question.
 
Status
Not open for further replies.
Top