• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

How to generalize this function?

Status
Not open for further replies.
Level 15
Joined
Aug 7, 2013
Messages
1,337
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,337
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