LunaDll for Lua Tutorial/Reference



Events:

Events are called by Lunadll if they exsist in the lua code. Note: Not all parameters have to be set, you can just ignore them, if you want to. But the order is always the same. So if you need parameter 2 you have to write parameter 1 too!


Function Signature Info

function onLoad()

 

end

onLoad is called when the level editor or the game engine loads the level.

function onLoop()

 

end

On each frame while the game runs onLoop is called.

function onLoopSection#()

 

end

On each frame while the game runs in the specific section # onLoopSection# is called. Replace # with the sectionnumber 0-20.

function onLoadSection()

 

end

In the first frameloop of a new section onLoadSection is called.

function onLoadSection#()

 

end

In the first frameloop of section # onLoadSection# is called. Replace # with the sectionnumber 0-20.

function onJump([int] playerIndex)

 

end

When the player starts to jump, onJump is called.

playerIndex is the index of the player (Player 1: playerIndex = 1; Player 2: playerIndex = 2).

function onJumpEnd([int] playerIndex)

 

end

When the player hits the surface (jump ends), onJumpEnd is called.

playerIndex is the index of the player (Player 1: playerIndex = 1; Player 2: playerIndex = 2).

function onKeyDown([int] keycode, [int] playerIndex)

 

end

 

When a key is getting hit, onKeyDown is called. Keycode can be compared with the constants.

playerIndex is the index of the player (Player 1: playerIndex = 1; Player 2: playerIndex = 2).

function onKeyUp([int] keycode, [int] playerIndex)

 

end

When a key is getting released, onKeyUp is called. Keycode can be compared with the constants.

playerIndex is the index of the player (Player 1: playerIndex = 1; Player 2: playerIndex = 2).




Classes:

Classes are holding a bunch of functions, fields, ect... for a specific object type. They can be constructed or returned by a global function.

NOTE: The square brackets "[]" are used to indicate the return-type/param and are not part of the actual code. In lua, variables are defined without a variable-type.

The RECT Structure
Function Signature Info

myRect = RECT()

Constructs a new RECT structure.

[int] myRect.left

The "left" value of the RECT structure.

[int] myRect.top

The "top" value of the RECT structure.

[int] myRect.right

The "right" value of the RECT structure.

[int] myRect.bottom

The "bottom" value of the RECT structure.

This class provides fields to save "left", "top", "right", "bottom" as a int value. It is common used by other classes.

 

The RECTd Structure
Function Signature Info

myRect = RECTd()

Constructs a new RECT structure.

[double] myRect.left

The "left" value of the RECT structure.

[double] myRect.top

The "top" value of the RECT structure.

[double] myRect.right

The "right" value of the RECT structure.

[double] myRect.bottom

The "bottom" value of the RECT structure.

Same as RECT, just with double values.

 


The Section Class
Function Signature Info

mySection = Section([int] sectionnum)

Constructs a new section class by the sectionnumber.

[RECTd] mySection.boundary

Holds the values for the section boundaries.

This class provides fields and functions to modify the section.


The NPC Class
Function Signature Info

myNPC = NPC([int] arrayindex)

Constructs a new NPC via internal arrayindex.

NOTE: Do not construct directly. Use function "npcs()" or "findnpcs([int],[int])" instead!

[double] myNPC.x

The x value of the npc.

[double] myNPC.y

The y value of the npc.

[double] myNPC.speedX

The x speed value of the npc.

[double] myNPC.speedY

The y speed value of the npc.

[int] myNPC.id

The npc-id of the npc.

[int] myNPC.direction

The face direction of the npc.

NOTE: Setting the direction of a npc, resets the monumentum of the npc. You can modify the speedX value if you want to set your own monumentum.

myNPC:mem([hex/int] address-offset, [int] datatype, [any] value)

Sets a NPC offset-address to a specfic value. See constants for datatypes.

[any] myNPC:mem([hex/int] address, [int] datatype)

Gets a specfic value by NPC offset-address. See constants for datatypes.

myNPC:kill()

Kills the npc with the standard kill animation.

myNPC:kill([int] killanimation)

Kills the npc with the kill animation of your choice.

Known ids are:

1 - Standard kill animation

6 - Lava kill

8 - Spin jump kill

9 - dissapear (no animation)

10 - Link kill (like link would kill this npc)

This class provides fields and functions to modify the npc. It is not recommended to save this class for later, as the internal arrayindex changes when enemies get killed.



The Player Class
Function Signature Info

player = Player()

Constructs a new Player class. All constructed players will always point to the first player.

NOTE: It is recommended to use the constant "player" and/or "player2" (for second player) instead of constructing an own one.

player = Player([int] playerindex)

Constructs a player with the specific playerindex. To check if valid use if(player.isValid)! Player 1 has playerindex = 1 and Player 2 has playerindex = 2

player:kill()

Kills the player.

player:harm()

Harms the player (get a fake hit)

[double] player.x

The x value of the player.

[double] player.y

The y value of the player.

[double] player.speedX

The x speed value of the player.

[double] player.speedY

The y speed value of the player.

[int] player.powerup

The powerup value of the player.

[int] player.reservePowerup

The npc-id of the reserve powerup box.

[int(readonly)] player.section

The current section of the player.

[Section(readonly)] player.sectionObj

Returns the current section in a section object.

[class RECT(readonly)] player.screen

The player RECT to the screen.

[class NPC(readonly) or nil] player.holdingNPC

If the player holds the npc, it returns the NPC

player:mem([hex/int] address-offset, [int] datatype, [any] value)

Sets a player offset-address to a specfic value. See constants for datatypes.

[any] player:mem([hex/int] address, [int] datatype)

Gets a specfic value by player offset-address. See constants for datatypes.

[boolean] player.isValid

If this instance is valid. Good for checking if a second player exsist.

View raw fields

Raw fields can be accessed like normal fields with read/write access, but there is no support (no constants, read-limiting, ect...) given.

They are not camel-case formatted!

This class provides fields and functions to modify the player/character.

NOTE: It is recommended to use the constant "player" instead of constructing an own one.



Global functions:

Functions which can be globally accessed. They don't need any class to run.


Function Signature Info

windowDebug([string] text)

Displays a message box with the text provided.

printText([string] text, [int] x, [int] y)

Displays text at x,y with the font-type 3.

printText([string] text, [int] font-type, [int] x, [int y]

Displays text at x,y with the font-type of your choice.

[int] totalNPC()

Returns the number of NPCs in this level.

[NPC (array)] npcs()

Returns an array with all NPCs in this level.

[NPC (array)] findnpcs([int] npc-id, [int] section)

Returns an array filtered to npc-id and/or section. Use -1 or FIND_ANY to skip one of either filter.

mem([hex/int] address, [int] datatype, [any] value)

Sets a global address to a specfic value. See constants for datatypes.

[any] mem([hex/int] address, [int] datatype)

Gets a specfic value by address. See constants for datatypes.

playSFX([int] soundid)

Plays the given sound by sound-id.

playSFX([string] soundfile)

Plays the given sound by soundfile.

triggerEvent([string] eventname)

Runs a event by eventname.

playMusic([int] section)

Plays the music for section.

loadImage([string] filename, [int] resourceNumber, [hex/int] transperentColor)

Loads a image by filename into resourceNumber.

 

placeSprite([int] type, [int] imgResource, [int] xPos, [int] yPos)

Places a image/sprite by resource-number imgResource at xPos, yPos.

placeSprite([int] type, [int] imgResource, [int] xPos, [int] yPos, [string] extra)

Places a image/sprite by resource-number imgResource at xPos, yPos. Extra string data field provided. Leave a empty string if not used.

placeSprite([int] type, [int] imgResource, [int] xPos, [int] yPos, [string] extra, [int] time)

Places a image/sprite by resource-number imgResource at xPos, yPos. Extra string data field provided. Leave a empty string if not used. You can set the time if needed.

[int] gravity()

Returns the gravity in this level. 12 = Default

gravity([int] value)

Sets the gravity in this level. 12 = Default

[int] earthquake()

Returns the earthquake-effect in this level. 0 = No earth quake. Resets to 0 after a time. Higher number = Higher earthquake-effect.

earthquake([int] value)

Sets the earthquake-effect in this level. 0 = No earth quake. Resets to 0 after a time. Higher number = Higher earthquake-effect.

[int] jumpheight()

Returns the jumpheight-factor of every character. 20 = Default

jumpheight([int] value)

Sets the jumpheight-factor of every character. 20 = Default

[int] jumpheightBounce()

Returns the jumpheight-factor (when bounced off from a npc) of every character. 20 = Default

jumpheightBounce([int] value)

Sets the jumpheight-factor (when bounced off from a npc) of every character. 20 = Default

 

Group: UserData
Function Signature Info

UserData.setValue([string] key, [double] value)

Sets a double value by the given key.

[double/nil] UserData.getValue([string] key)

Returns the value by the given key. If the value doesn't exsist, nil is returned.

[table] UserData.values()

Returns a copy of all values with the keys.

[boolean] UserData.isValueSet([string] key)

Returns true if the value exsist.

UserData.save()

Saves the variable bank into the file.

This funtion group provides functions to load/save [double] values into the variable bank. You can then save it for later use, even after the game restarted.

NOTE: Don't forget to save! It doesn't save automatically! Call UserData.save() for saving into the file.

 

 

Constants:

Constants are variables which hold values for specifc functions.


Constant Recommended use Info

player

---

An instance of player 1

player2

---

An instance of player 2. (Only if 2-player mode) You can check with 'if(player2)then' to be sure if a second player exsist.

FIND_ANY

function npcread

Ignores the filter of npcread

DIR_RIGHT

myNPC.direction

Direction right

DIR_RANDOM

myNPC.direction

Direction random

DIR_LEFT

myNPC.direction

Direction left

PLAYER_SMALL

player.powerup

No powerup/Small

PLAYER_BIG

player.powerup

Mushroom powerup/Big

PLAYER_FIREFLOWER

player.powerup

Fireflower powerup

PLAYER_LEAF

player.powerup

Leaf powerup

PLAYER_TANOOKIE

player.powerup

Tanookie powerup

PLAYER_HAMMER

player.powerup

Hammer powerup

PLAYER_ICE

player.powerup

Ice powerup

FIELD_BYTE

function mem

player:mem

myNPC:mem

Byte field data type

FIELD_WORD

function mem

player:mem

myNPC:mem

Word field data type

FIELD_DWORD

function mem

player:mem

myNPC:mem

DWord field data type

FIELD_FLOAT

function mem

player:mem

myNPC:mem

Float field data type

FIELD_DFLOAT

function mem

player:mem

myNPC:mem

DFloat field data type

KEY_UP

keycode

Up key

KEY_DOWN

keycode

Down key

KEY_LEFT

keycode

Left key

KEY_RIGHT

keycode

Right key

KEY_JUMP

keycode

Jump key

KEY_SPINJUMP

keycode

Spinjump key

KEY_X

keycode

x key

KEY_RUN

keycode

Run key

KEY_SEL

keycode

Sel key

KEY_STR

keycode

Str key

lapi.lua - Helper class written in lua

 

This is just a helper class. Even without it Lunadll+Lua can run, but may break codes which require this class to run!


Global functions/features:

Functions/Features which can be globally accessed. They don't need any class to run.


Function Signature Info

uservar[[string]key] = [double] value

A fast way to set a variable in the variable bank.

Example:

uservar["Jumpcounter"] = uservar["Jumpcounter"] + 1

 

[double] value = uservar[[string]key]

A fast way to get a variable in the variable bank. If value doesn't exsist 0 is returned, not nil!

Example:

uservar["Jumpcounter"] = uservar["Jumpcounter"] + 1


 

Examples:

function onLoop()
  allNPCs = findnpcs(20,player.section)
  for k,v in pairs(allNPCs) do
    if(v.direction == DIR_RIGHT)then
      v.speedX = 5
    else
      v.speedX = -5

    end
  end
end

Info: Makes every blue Shy-Guy running super fast.


i = 0

function onLoad()
  i = 0
end

function onLoop()
  i = i + 1
  if(i % 100 == 0)then
    allNPCs = findnpcs(20,player.section)
    for k,v in pairs(allNPCs) do
      v.speedY = -8
    end
  end
end

Info: Makes every blue Shy-Guy jump every 100 frames.


function onLoop()
  if(player.y > -200320)then
    player.speedY = -10
  end
end

Info: Let the player jump up when it reaches a specific y-value.


i = 0

function onLoad()
  i = 0
end

function onLoop()
  printText("RUNNING "..tostring(i).." FRAMES", 30, 30)
  printText("PLAYER COOR "..tostring(player.x).." "..tostring(player.y), 30, 60)
end

Info: Some debug info.


i = 0

function onLoad()
  i = 0
end

function onLoop()
  i = i + 1
  if(i % 200 == 0)then
    player.powerup = math.random(1,7)
  end
end

Info: A new powerup every 200 frames (powerup roulett)

 

 


function onLoop()
  if(UserData.getValue("jump-counter"))then
    printText("Jumps: "..tostring(UserData.values()["jump-counter"]), 30, 160) -- values()[value-Name] can be used too, to get the a value
  end
end

function onJump(plIndex)
  if(UserData.getValue("jump-counter"))then
    UserData.setValue("jump-counter", UserData.getValue("jump-counter")+1)
  else
    UserData.setValue("jump-counter", 1)
  end
  UserData.save()
end


Info: A jump-counter with the new variable bank!