Jump to content

Leadwerks 4.6 -- Lua Script for a Health Pack Item to replenish player's health


Recommended Posts

Good afternoon all,

I have recently been playing around with the Leadwerks engine 4.6 and working with the AI & Events.map that is a FPS demo game. I have since bought all the asset packs from Leadwerks on Steam and downloaded a medical kit and tried to look around for a lua script that will add/replenish the player's health after he has taken damage from the crawler enemies in the demo level. I couldn't find any script like that available in the workshop and so far here in the forums unless I haven't looked good enough.. :-p

 

I had tried the ChatGPT to see what it could come up with and got the following for this below.. If this doesn't look right, or it will not work well, or needs corrected, could someone kind enough show the proper way this should be written or better yet.. If someone already has a lua script for this exact thing that would be appreciated too!

 

The Lua Script from ChatGPT for this:

 

 

-- Define variables (assuming these are already defined)
local player = {
    x = 100,
    y = 100,
    speed = 100,
    health = 100,
    maxHealth = 100
}

local healthPickup = {
    x = 50,
    y = 50,
    radius = 10,
    active = true
}

-- Function to check if player collides with health pickup
local function checkCollision(px, py, pr, hx, hy, hr)
    local dx = px - hx
    local dy = py - hy
    local distance = math.sqrt(dx * dx + dy * dy)
    return distance < pr + hr
end

-- Function to handle player picking up health
local function pickupHealth()
    if healthPickup.active and checkCollision(player.x, player.y, 5, healthPickup.x, healthPickup.y, healthPickup.radius) then
        if player.health < player.maxHealth then
            player.health = player.health + 25  -- Increase health by 25 (adjust as needed)
            if player.health > player.maxHealth then
                player.health = player.maxHealth  -- Cap health at maxHealth
            end
            healthPickup.active = false  -- Deactivate the health pickup after it's been used
        end
    end
end

-- Update function (called every frame)
function love.update(dt)
    pickupHealth()  -- Check for health pickup

    -- Example movement for player (replace with your own player movement logic)
    if love.keyboard.isDown("right") then
        player.x = player.x + player.speed * dt
    elseif love.keyboard.isDown("left") then
        player.x = player.x - player.speed * dt
    end

    -- Example code for checking player's health
    if player.health <= 0 then
        -- Game over or respawn logic can go here
        player.health = player.maxHealth
    end
end

-- Drawing function (called every frame)
function love.draw()
    -- Draw player
    love.graphics.circle("fill", player.x, player.y, 5)

    -- Draw health pickup if it's active
    if healthPickup.active then
        love.graphics.circle("line", healthPickup.x, healthPickup.y, healthPickup.radius)
    end

    -- Display player's health
    love.graphics.print("Health: " .. player.health, 10, 10)
end
 

 

Link to comment
Share on other sites

 

I use something like this:

 

 

Script.healthBonus = 25 -- int "Health Bonus"
Script.height = 1.1 -- float "Hover Height"
Script.speed = 2.0 -- float "Rotate Speed"

function Script:Start()

	--Load a sound
    self.sound = Sound:Load("Sound/Interaction/switch12.wav")

	self.entity:SetMass(0)
	self.entity:SetCollisionType(Collision.Trigger)
	local pos = self.entity:GetPosition()
	self.entity:SetPosition(pos.x,pos.y+self.height,pos.z)
end

function Script:UpdateWorld()

	self.entity:Turn(0,Time:GetSpeed() * self.speed,0,true)
end

function Script:Collision(entity, position, normal, speed)

	--if collision with key value type is "player"
	if entity:GetKeyValue("type")=="player" then

		--add health
		if entity.script.health < entity.script.maxHealth then
			entity.script.health = entity.script.health + self.healthBonus
		end

		--cap max health
		if entity.script.health > entity.script.maxHealth then
			entity.script.health = entity.script.maxHealth
		end

		--play sound
		if self.sound then
			self.sound:Play()
		end

		--despawn item
		self:DespawnItem()
	end
end

function Script:DespawnItem()

	self.entity:Hide()
	self.entity:Release()
	self.entity = nil
end

 

 

A couple of things for this to work:

1. You want to make sure this line is in the Start function of your player's script:

    self.entity:SetKeyValue("type","player")

2. Also your health kit model has to have physics on it for the collision to  work. You can just double click the model and the page should pop up. I just use basic box physics.

 

Link to comment
Share on other sites

Thank you gentlemen for your very kind assistance here. I have tried one of those scripts and the Medkit is working just fine! I am still learning the Lua language and see it have similarities to that of C which I use to code in a very long time ago! I'm a little rusty mind you.. But in time I'm sure I'll catch on to it and know my way around better. I sincerely appreciate your very kind help and quick response. God bless! ;)

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...