0 votes

I am trying to implement a system to split the items in my inventory to another slot. For example, let’s say that I have a stacked slot in which one I have 6 items, when splitting, I should have 3 on the initial slot, and 3 in a second slot.
The problem I’m facing is when the number is not an even number. If I split a slot that has 5 items, the initial one should have 3 items, and the second one 2.
How could I get the corresponding values for this?

in Engine by (97 points)

3 Answers

0 votes
Best answer

So, you're just trying to calculate the number of items to place into 2 separate columns based on the total number of items? And, with an even number of items, each column should have the same number of items and with an odd number of items, the left-hand column should have 1 more?

If so, something like this should work:

func _ready():
    var total_items = 11
    var left = ceil(float(total_items) / 2)
    var right = total_items - left
by (12,475 points)
selected by
0 votes

like so ... ?

var max_items_per_column:float = 3
var column_index = floor( item_index / max_items_per_column )
by (4,044 points)
0 votes
func split(val):
    var valInSlot1=(val/2) as int
    var valInSlot2=val-valInSlot1
by (54 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.