Hey! I want to write a code that tracks how many gold coins has every single player on the match and make the player with the highest number of coins win the match. Is it possible to code that in Verse?
You can do that by using a conditional_button_device
set to have Gold as a price item.
In Verse you can query how much of a resource a particular agent has via this button.
@editable
ResourceChecker:conditional_button_device = conditional_button_device {}
# This will return how much Gold that Agent has
GetGoldAmount(Agent:agent):int=
# 0 is the Index of the cost item, make sure it's set to Gold
ResourceChecker.GetItemCount(Agent, 0)
Thanks for the help!
I’m new to verse and I don’t understand everything yet, can you explain me a bit more how I can implement this in the map?
Thank you!
Yeah no worries! You’ll need to create a new Verse file and define your own device.
Go to your Verse Explorer window and right click
Name the file gold_tracking_device.verse
and paste that in:
using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
gold_tracking_device := class(creative_device):
@editable
ResourceChecker:conditional_button_device = conditional_button_device {}
GetGoldAmount(Agent:agent):int=
ResourceChecker.GetItemCount(Agent, 0)
Now you can find your device in your Content folder, just drag it in your scene and make sure it’s not visible in game
Assign the conditional button I’ve mentioned earlier into ResourceChecker
and you’re set.
Now, somewhere else in your code you’ll need to reference that device and call the GetGoldAmount()
function to check your player, or you can use that device to do so.
I’m assuming you want to calculate the winner of the match at a certain point and trigger whatever device you have to make that player win, so you’d want a function like that
CheckWinningPlayer():void=
var MaxGold:int = 0
var MaybeWinningPlayer:?player = false
# Going through all active players in the game
for(Player:GetPlayspace().GetPlayers(), Player.IsActive[]):
# Identify the player that has the most Gold
GoldAmount := GetGoldAmount(Player)
if(GoldAmount > MaxGold):
set MaxGold = GoldAmount
set MaybeWinningPlayer = option {Player}
# This will verify that you have a winner
if(WinningPlayer := MaybeWinningPlayer?):
# Do whatever you need to make the player win the game
I would advise looking into Verse a bit more to understand how to connect devices together (using @editable
), and then you can trigger anything you want in the code above.
Hope that helps!
You need to indent the whole function so it’s part of your device. Just select it all and press Tab
Hi again! Sorry to ask for help again, but I can´t find a way to trigger the "GetGoldAmount(Agent:agent):int="
function. Can you tell me please how to trigger it with a trigger device? Thank you!
You can’t call it directly from a trigger device, but you can hook a trigger_device
into it and listen to its Triggered event.
You can add this code on your device and that should work.
@editable
Trigger:trigger_device = trigger_device {}
OnBegin<override>()<suspends>:void=
Trigger.TriggeredEvent.Subscribe(OnTriggered)
OnTriggered(Agent:agent):void=
CheckWinningPlayer()
Again, I’d advise to learn how to do this by following tutorials (there are plenty on this platform)
I got this error when I added the code.
I’ve been watching some tutorials but I couldn’t find anyone that does something similar to what I want.
Well I’ve answered your original question because there was a round-about way of achieving what you wanted to achieve, but now I feel like I’m just writing your game for you
The current error you’re encountering is because my code wasn’t accurate. The trigger_device
’s event TriggeredEvent
is expecting to call back on a funtion that has a certain signature (i.e. parameters/return value combo).
In this case it expects a “maybe agent”, or ?agent
- whereas the function I wrote and you copy/pasted has an agent
as parameter.
Just change the parameter to the correct type and that will fix the code.
If you are interested in understanding the language itself and its capabilities, this is a good start:
It won’t tell you how to do specific functionalities, but being able to look at some code, and understand how to manipulate it will help you in the long run.
Okay, thank you for the help!
Its really frustrating when I want to do something but I just can’t find the way to do it, Verse documentation is a good place to start but some things aren’t really that clear. I was able to fix the error I was facing earlier, so I think with all of this I can start creating my game.
Again, thank you for helping, it means a lot