Discord
Login
Community
DARK THEME

Useful functions - Please help to build a list

Hello Community, Let's build together a great collection of simple but very useful functions. Ideally keep them small and flexible.

Please post your own code snippets that you find to be useful. I am thinking we need a more general collection of common game-related functions such as:

  • sprite.turnTo(targetSprite)
  • screen.shake(amount,millisecs)

Here are a few of my contributions for a kick off:

// return concatenated string from r,g, and b values 
RGBtoString=function(r,g,b) "rgb("+r+","+g+","+b+")" end
// a shortcut way of getting directional movements (-1,0,1) from keyboard inputs
xdir=-keyboard.A+keyboard.D
ydir=-keyboard.S+keyboard.W
// return the sign of a value  ** Update - TinkerSmith's minimalist version
sgn=function(v) (v>0)-(v<0) end
dist2d=function(x1,y1,x2,y2) sqrt((x2-x1)^2+(y2-y1)^2) end
lerp=function(v1,v2,amt) (1-amt)*v1+amt*v2 end
clamp=function(v,lo,hi) max(lo,min(v,hi)) end
wrap=function(v,lo,hi)
  local r=hi-lo
  return (lo+((((v-lo)%r)+r)%r))
end
remap=function(v,s1,e1,s2,e2) (v-s1)*(e2-s2)/(e1-s1)+s2 end
mouseInZone=function(x1,y1,x2,y2)
  (mouse.x>=x1 and mouse.x<=x2 and mouse.y>=y1 and mouse.y<=y2) 
end
rectsOverlap=function(x1,y1,w1,h1,x2,y2,w2,h2)
  not(x2>x1+w1 or x2+w2<x1 or y2>(y1+h1) or (y2+h2)<y1)
end
screen.drawText("X="+floor(touch.x)+"  Y="+floor(touch.y),0,92,8,"rgb(255,85,198)")

Great idea to keep them all in one place. My work day just started but will have a look tonight once I'm back home.

P.S. love the keyboard input :)

Added to the list --> dist2d, lerp, clamp, wrap, remap

I've got some of my own -

angle=function(x,y,x2,y2) // Angle between two points
  dir=atand((x2-x)/(y2-y1))
  if y>=y2 then
    dir+=180
  end
  return dir
end
dirtovect=function(dir) // Direction (angle) into vector form
  vectorx=sind(dir)
  vectory=cosd(dir)
  return [vectorx,vectory] // Returns in list format (haven't tested)
end
dot=function(x1,y1,x2,y2) // Dot product of two vectors. Not sure if this can be implemented by multiplying two lists
  return x1*x2+y1*y2
end
dot3d=function(x1,y1,z1,x2,y2,z2) // 3d dot product
  return x1*x2+y1*y2+z1*z2
end
cross=function(x1,y1,z1,x2,y2,z2) // Cross product of two vectors
  return [y1*z2-z1*y2,z1*x2-x1*z2,x1*y2-y1*x2]
end
normalise3d=function(x,y,z) // Returns normalised vector
  return [x/dist3d(x,y,z),y/dist3d(x,y,z),z/dist3d(x,y,z)]
end

I'll let you know if I think of anymore useful ones :). I'm not sure if returning a list works, by the way.

@JimB007 @tinkersmith Are either of you replying to the post at all? I'm noticing that the post says '5 hours ago' but isn't showing any new messages to me. Is it some kind of bug?

approx function returns true if a is approx to b by tol(number)

approx = function(a, b, tol)
  abs(a-b) <= tol
end

useful if you want to do something like

//if player.vx is approx 0 by difference of 10
if approx(player.vx,0,10) then
//some code
end

since 0.1 is still moving even thou player can't see it
that's just how engine works

//Cycles through fonts so you can test which one works best for your text


testFont = function()
    
    for f in fonts 
    screen.loadFont(f)
    end
    
    if keyboard.press.LEFT then
      if fontpos <= 0 then
      fontpos = 46
      else
      fontpos -= 1
      end
      print(fonts[fontpos])
    end
    if keyboard.press.RIGHT then
      if fontpos >= 46 then
        fontpos = 0
      else
      fontpos += 1
      end
      print(fonts[fontpos])
    end
    
    screen.setFont(fonts[fontpos])
    
  end

idk if anyone else has done this A tool i made so that you can easily test fonts easily for whatever text you have

testFont()
screen.drawText("title",0,0,20)

then just use the arrow keys

ConnorL_K_I, yes! I also did it with the ability to change the font size https://microstudio.dev/i/Romero/fontcheatsheet/

I like to write a simple logging system in JavaScript for my MicroScript projects:

//javascript

let debugging = "none";

global.debug = function(...data) {
  if (!data.length) {
    return;
  }
  
  switch(debugging) {
    case "print":
      if (data.length == 1) {
        global.print(data[0]);
      } else {
        global.print(data.join(" "));
      }
      break;
    
    case "console":
      console.log(...data);
      break;
  }
}

Here are a few functions that I have created. I wouldn't want to lose them again. 😅: They are all for JavaScript! If you use microScript, don't forget to add // javascript

// allows you to rotate the sprite/object towards the mouse, suitable for top down shooter
// example screen.setDrawRotation(angleView(player.x, player.y))
angleView = function(px,py){ 
  return (Math.atan2(mouse.x-px, mouse.y-py)*180/Math.PI)*(-1);
}
// simple collision detection of two objects AABB
collision = function(a,b){
  if(Math.abs(a.x-b.x)<(a.w+b.w)/2 &&
     Math.abs(a.y-b.y)<(a.h+b.h)/2){
     return true;
  }else{
     return false;
  }
}
// detecting the side of collisions between two objects
// returns the side of object B that object A touches
colSide = function(a,b){ 
  if (a.x == b.x-b.w && Math.abs(a.y-b.y) <= b.h/2){ return "left"
  }else if (a.x == b.x+b.w && Math.abs(a.y-b.y) <= b.h/2){ return "right"
  }else if (a.y == b.y+b.h && Math.abs(a.x-b.x) <= b.w/2){ return "up"
  }else if (a.y == b.y-b.h && Math.abs(a.x-b.x) <= b.w/2){ return "down"
  }else{return false}
}

example: https://microstudio.io/Romero/sideofcollisions/

// The mapValue() function proportionally converts values from one range to another
function mapValue(value, oldMin, oldMax, newMin, newMax) {
  return ((value - oldMin) * (newMax - newMin)) / (oldMax - oldMin) + newMin;
}

Post a reply

Progress

Status

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