Player (class)

From PGE Wiki
Revision as of 00:13, 5 February 2018 by Enjl (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

This class provides fields/functions for managing Players in the current level or world map. NOTE: The Player functionality is limited on the world map.

Static functions

Static Player Functions (Overworld and level)
Function Return values Description
Player.count() number How many players are in the level.
Player.get() table of Player Returns all players. You might want to consider using the constants player and player2 instead!
Player.getIntersecting() table of Player Returns all players which are intersecting with an area from x1/y1 to x2/y2.
Player.getTemplates() table of Player Returns all player templates. Player templates are the saved up player states for each character. They are saved, when the player hits a character-switch block. Modifing these templates and then hitting the character-switch blocks may give the result you expect.
Player() Player Returns the first player. You might want to use the constant player instead.
Player(number index) Player Returns the player object at the given index.

Static Player Settings functions (Level only)

Static Player Settings Functions
Function Return values Description
PlayerSettings.get(number character, number powerup) PlayerSettings Returns a PlayerSettings object. With this object you can modify the hitbox and offset of the player directly. For character and powerup variable see constants.

Constants

The player class provides constants for commonly used objects:

Player constants
Constant Used in Description
player Player class References the first player
player2 Player class References the second player

Instance functions

These fields and functions are part of Player Objects and have to be called for that object. Trying to call them from the static Player class will result in an error. Example (feel free to copy it into your lua file to see how it works):

function onStart()
    player.powerup = 2
end


Player class
Type Function/Field Return values/Value type Description
function Player:mem(int offset, int fieldtype, object value) nil Sets a value of the Player struct at a specific address-offset.
function Player:mem(int offset, int fieldtype) object Returns a value of the Player struct at a specific address-offset.
function Player:kill() nil Kills the player.
function Player:harm() nil Harms the player.
Field Player.x double The x coordinate of the player.
Field Player.y double The y coordinate of the player.
Field Player.speedX double The x-speed of the player.
Field Player.speedY double The y-speed of the player.
Field (ro) Player.screen RECT Returns the distances from the player's coordinates to the screen boundaries of player camera. Note that this means width is 'right' + 'left', and not 'right' - 'left', etc.
Field (ro) Player.section int Returns the current section number of the player.
Field (ro) Player.sectionObj Section Returns the current section object of the player.
Field Player.powerup int The current powerup state the player is in. See constants for the values you can put it.
Field Player.character int The current character of the player is in. See constants for the values you can put it. (LunaLua ≥ v0.7.2)
Field Player.reservePowerup int The npc-id in the powerup box of the player.
Field Player.width int The player width.
Field Player.height int The player height.
Field (ro) Player.holdingNPC NPC or nil The npc which the player is holding. If the player holds nothing then nil is returned.
Field Player.upKeyPressing boolean True, if the player is pressing the up key. Can be overwritten with the Event onInputUpdate, to lock or reverse controls (LunaLua ≥v0.7)
Field Player.downKeyPressing boolean True, if the player is pressing the down key. Can be overwritten with the Event onInputUpdate, to lock or reverse controls (LunaLua ≥v0.7)
Field Player.leftKeyPressing boolean True, if the player is pressing the left key. Can be overwritten with the Event onInputUpdate, to lock or reverse controls (LunaLua ≥v0.7)
Field Player.rightKeyPressing boolean True, if the player is pressing the right key. Can be overwritten with the Event onInputUpdate, to lock or reverse controls (LunaLua ≥v0.7)
Field Player.jumpKeyPressing boolean True, if the player is pressing the jump key. Can be overwritten with the Event onInputUpdate, to lock or reverse controls (LunaLua ≥v0.7)
Field Player.altJumpKeyPressing boolean True, if the player is pressing the alt jump key. Can be overwritten with the Event onInputUpdate, to lock or reverse controls (LunaLua ≥v0.7)
Field Player.runKeyPressing boolean True, if the player is pressing the run key. Can be overwritten with the Event onInputUpdate, to lock or reverse controls (LunaLua ≥v0.7)
Field Player.altRunKeyPressing boolean True, if the player is pressing the alt run key. Can be overwritten with the Event onInputUpdate, to lock or reverse controls (LunaLua ≥v0.7)
Field Player.dropItemKeyPressing boolean True, if the player is pressing the drop item key. Can be overwritten with the Event onInputUpdate, to lock or reverse controls (LunaLua ≥v0.7)
Field Player.pauseKeyPressing boolean True, if the player is pressing the pause key. Can be overwritten with the Event onInputUpdate, to lock or reverse controls (LunaLua ≥v0.7)
function Player:getCurrentPlayerSetting() PlayerSettings Returns a PlayerSettings object of the current character and powerup state of the Player. (LunaLua ≥ v0.7.2)
function Player:getCurrentSpriteIndex() number, number Returns the current indexX and indexY of the sprite on the spritesheet. Can be used with PlayerSettings. (LunaLua ≥ v0.7.2)
function Player:setCurrentSpriteIndex(number indexX, number indexY, boolean forceDirection) nil Sets the current sprite on the spritesheet. If forceDirection is true, then the direction of the player will be set. Caution: This may not work, depending on the players movement!. Can be used with PlayerSettings. (LunaLua ≥ v0.7.2)
Field (ro) Player.isValid boolean Checks if the player is acutally valid.

There are also raw fields. These fields are not maintained and not camel-case affected!