Swift 4 - How to have a function set a variable outside that function

Swift 4 - How to have a function set a variable outside that function

So I just started with Swift the other day. In messing around with some code, I tried to write a function that would have a target player attack and enemy and have that enemies HP reduced by the amount of attack power of the player. Everything works inside the function fine, but I can't seem to find a way to get it to effect the selected enemy outside the function. Maybe its something that will come in a later lesson, but in my head it should be something simple that I can't seem to figure out.

So I just started with Swift the other day. In messing around with some code, I tried to write a function that would have a target player attack and enemy and have that enemies HP reduced by the amount of attack power of the player. Everything works inside the function fine, but I can't seem to find a way to get it to effect the selected enemy outside the function. Maybe its something that will come in a later lesson, but in my head it should be something simple that I can't seem to figure out.

Here is the code that I am using.

struct Player {
    var maxHP: Int
    var currentHP: Int
    var maxMP: Int
    var currentMP: Int
    var currentLVL: Int
    var damageATT: Int
}

struct Enemy { var maxHP: Int var currentHP: Int var damageAtt: Int }

var alexRider = Player(maxHP: 100, currentHP: 100, maxMP: 100, currentMP: 100, currentLVL: 1, damageATT: 5) var enemyOne = Enemy(maxHP: 20, currentHP: 20, damageAtt: 2) var enemyTwo = Enemy(maxHP: 40, currentHP: 40, damageAtt: 4)

func attackTarget(attacker player: Player, enemy target: Enemy) { var attackTAR = target attackTAR.currentHP -= player.damageATT print("Dealt (player.damageATT) damage!") print("Enemy now has (attackTAR.currentHP) health remaining") }

attackTarget(attacker: alexRider, enemy: enemyTwo) print(enemyTwo.currentHP)

Obviously, when I print the currentHP at the end of the code, its the same as it started since the scope of the variables inside the function do not pass outside. The print commands inside the function work fine to show the damage amount and remaining HP of the instance inside.

Is there something simple I am missing here? Again I know this should be something simple, but just getting started with this I was hoping someone could point me in the right direction.

Thanks.

swift function

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Hire Dedicated Swift Developers

Want to create a native iOS application for your Startup? **[Hire Dedicated Swift Developers](https://hourlydeveloper.io/hire-dedicated-swift-developer/ "Hire Dedicated Swift Developers")** for end-to-end services like development, migration...

Variadic Functions in Swift

What is a variadic function? In computer programming, a variadic function is a function which accepts a variable number of arguments. The function arguments are ...

Create a class instance (without a Class) from a function in Swift

This short blog was inspired when I was watching an MIT/GNU Scheme tutorial on Youtube and thought to convert Scheme code example into

Functional Programming: Higher Order Functions

Functional Programming: Higher Order Functions. A Better Take on JavaScript’s Higher Order Functions. Functional Programming is awesome! It makes programming fun.

Higher-Order Functions Beginners Should Be Familiar With.

Higher order functions are functions that operates on other functions, either by taking them as arguments or by returning them.