In this Lua Lesson, we will look at how to go about creating just enough code to get a game working. I find the best way to get started is to breakdown things into smaller parts so we will be using existing cards/skills/etc. to get a custom game up and running. This will be the launching point for more complicated topics to come in later lessons.

Make sure as a prerequisite , that you have read the post I made introducing the Lua development process and the list of resources you will need/want to have saved somewhere you can pull up quickly.
The link to that article is here

Most of this lesson will be going over all the comments I made to a Lua file. I will try to add screenshots along the way that show how the code affects the game. The file on todays topic will be available for you to save and use yourself. The objective is to keep the learning curve as small as possible by only using cards/skills/abilities/etc. that are already in the game.

Setting Up Player Data

When Hero Realms loads a game it will import all of your player data (ie your name) as well as the character data associated with your chosen character (ie class/character name/level/etc.) This all handled by the game engine and you probably could have inferred that yourself. When making Lua scripts, you have control over what each player enters the game with because you have the option to alter the character data on game start.

This first lua script file sets up a normal game for two players. There is nothing special going on game wise and the game will play like any other game you have played before.

  • Player 1 uses the engine method by importing the character data from the character you chose to use and would change if you started a new game with a different character.
  • Player 2 does not import anything from your character but all of the character data is manually coded in the script and is the same every time.

The best way to proceed now would be to download the file and open it in a code/text editor that understands the Lua file format. As I suggested in the resource article, I recommend Visual Studios Code (VSCode). Then walk through the file reading all the comments (lines starting with –)

The file is hidden here to save scrolling. You can read it here but the the formatting is not great.

The file name is bare_minimum.lua

--These "require" are imports to other HR code needed to run the game.

--You should be able to just reuse these same ones every time

require 'herorealms'

require 'decks'

require 'stdlib'

require 'timeoutai'

require 'hardai_2'

require 'aggressiveai'

--====================================================================================

-- This lua file file contains the minimum amount of parts to run a game with both options for player setup used.

-- You probably will only use one of the methods each script.

--Player 1 is using the data form the class/character they chose.

--Player 2 is using a custom defined character/class data

--This game will play out like a normal game.

--====================================================================================

function setupGame(g)

registerCards(g, {

--You only need to register new cards here (so nothing to see here for now)

})



standardSetup(g, {

description = "Bare Minimum: Default Player Data<br>Created by WardenSlayer.",

--This defines the play order, only 2 players is supported at this time

playerOrder = { plid1, plid2 },

--AI is optional but I always include it to have the option to test with an AI player

--

ai = ai.CreateKillSwitchAi(createAggressiveAI(), createHardAi2()),

timeoutAi = createTimeoutAi(),

opponents = { { plid1, plid2 } },

--This player object contains/creates all the information about each player.

--In this example the data is imported from the player/character (ie the class, health, and starting cards)

players = {

{

id = plid1,

--Uncomment the line below to make this player an AI. One note about AI, it doesnt work in online games so make sure to comment it back out before uploading

--isAi = true,

--This sets how many cards a player has on thier first turn (player 1 normally has only 3)

startDraw = 3,

init = {

--This is the part where the character data is imported

fromEnv = plid1

},

cards = {

--Since the starting deck cards/skills are predetermined, only buffs need to be here

buffs = {

--This sets how many cards a player draws at the end of the turn, normally 5

drawCardsCountAtTurnEndDef(5),

--This handles the case where a player needs to discard a card at the start of their turn (discard effects/skills)

discardCardsAtTurnStartDef(),

--This is used to track the turn counter so the "Enrage" syste, triggers to try and end the game after the set number of turns.

fatigueCount(40, 1, "FatigueP1"),

}

}

},

--In this example the player/character data is defined manually (good for making fresh starts or custom classes)

{

id = plid2,

--Uncomment the line below to make this player an AI.

isAi = true,

--This sets how many cards a player has on thier first turn.

startDraw = 5,

--This populates the name of the character in the game

name = "Custom Name 5000",

--This changes the icon image for the player (normally it is based on your class/gender but can use anything from the LUA doc avatar list)

avatar="assassin",

--This sets the starting health and by extension the player's health cap

health = 50,

--You can use custom cards below but for this example I am starting with the easiest way to learn

--by using already existing cards. You can find a list in the Lua doc.

cards = {

--This defines the starting deck composition

deck = {

--2 daggers and 8 gold

{ qty=2, card=dagger_carddef() },

{ qty=8, card=gold_carddef() },

},

--This defines the skills/abilities/armor the player has

skills = {

--Skill: Lift

{qty = 1, card = thief_lift_carddef() },

--Ability: Devastating Blow

{qty = 1, card = fighter_devastating_blow_carddef()},

--This an armor card but it works here too (ranger cloak)

{qty = 1, card = ranger_hunters_cloak_carddef()},

},

buffs = {

--These are the same as above for player 1 with one exception, make sure player 2 has their

--own fatigue counter (ie "FatigueP2") or it will trigger early

drawCardsCountAtTurnEndDef(5),

discardCardsAtTurnStartDef(),

fatigueCount(40, 1, "FatigueP2")

}

}

},

}

})

end



--====================================================================================

--This triggers when the game ends

function endGame(g)

end



--====================================================================================

function setupMeta(meta)

meta.name = "bare_minimum"

meta.minLevel = 0

meta.maxLevel = 0

meta.introbackground = ""

meta.introheader = ""

meta.introdescription = ""

--This is the filepath used to track updates. Set this once and then dont update it or move the file

meta.path = "C:/Users/xTheC/Desktop/Git Repositories/Hero-Realms-Lua/Tutorials/bare_minimum.lua"

meta.features = {

}

end

Conclusion

You can load the file onto your Steam Hero Realms client and try it out. You will see that your player always matches your character but the AI player 2 always looks like this

Now that you know how to load a file and get it into a playable state, the next lesson will focus on how to make your first new action card. You have to do the card graphics as well case code how it actually functions.

Leave a comment

Trending

A WordPress.com Website.