Can I move an object from one card to another?

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
ByronViggo
Posts: 1
Joined: Wed Apr 28, 2021 8:12 am

Can I move an object from one card to another?

Post by ByronViggo » Wed Apr 28, 2021 8:15 am

For a game I'm making, I've made an inventory system. When you touch an image, the visibility is set to 'false' and the name of the image is added to spare inventory 'slot' in an SQLite database.

When you select the item in a data grid and click the 'drop' button, the entry gets removed from the corresponding database 'slot' and the visibility of the image is set to 'visible' again. Probably makes me sound like an uber n00b to all the elite coders on here, but that's how I've done it.

QUESTION...
The obvious problem with my above methodology is that if you move between cards (aka 'rooms' in my game) then the image doesn't come with you. The database entry in the 'inventory' comes with you but you can't 'drop' items in different cards because they belong to a card.

Is there a better way to do this before it's too late? (I'm making a 'demo' game while I learn so that I don't get stuck with messy, fundamental mistakes that kill me later on)

Can I make the object move to the card you are in if you choose to drop it in another card?

An alternative option I've been thinking of is to 'group' all of the objects and drag them with you to every card. However, I think this would be really messy because you'd have heaps of invisible items floating around everywhere. Also, if you dropped an item, it would then be visible in every card (not just the one where you dropped it)... unless you had a 'storage room' card or something for dropping stuff. However, this would also come unstuck because if you 'stored' something then came back to the original room where it was kept, it'd be visible in both rooms.

I hope that there's a way in which I can code something like [not real code here]:
on mouseup [for the drop burron]
move [hilited object] to the current card
delete that object from the database slot
end mouseup

Can this be done?

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Can I move an object from one card to another?

Post by bogs » Wed Apr 28, 2021 8:53 am

ByronViggo wrote:
Wed Apr 28, 2021 8:15 am
An alternative option I've been thinking of is to 'group' all of the objects and drag them with you to every card. However, I think this would be really messy because you'd have heaps of invisible items floating around everywhere. Also, if you dropped an item, it would then be visible in every card (not just the one where you dropped it)... unless you had a 'storage room' card or something for dropping stuff. However, this would also come unstuck because if you 'stored' something then came back to the original room where it was kept, it'd be visible in both rooms.
I'd suggest that going with that second idea would probably work better, but instead of visible and not visible, using copy (or cut ) and paste from the storage card.

I'd tend to prefer copying and pasting for drops (which would only happen once per item) for 2 reasons...
1. your storage card would permanently have the full inventory at all times, so you couldn't accidentally loose an object due to power failure, lets say, which would permanently hose the game up.

2. it would make coding extremely simple, since you could just delete the object on the card it is picked up from, and copy an instance from the storage card to whatever card you are going to paste it on (drop it).

Modifying your psuedo code, it would look like :

Code: Select all

on mouseUp [for the pick up  button]
	if the selectedObject is among the lines of field "objects that can be picked up then cut the selectedObject
	   # I would not use the selected object for this without making sure that it was an acceptable object.  
	   # after all, it wouldn't do to have, say, a tree get picked up by the player (well, maybe it would heh)
end mouseUp

on mouseUp [for the drop button]
	copy button "myItem" of card "storage" of stack "storedStuff" to the current card 
	  # ...trigger whatever mechanism you choose to set the item's availability to be dropped...
end mouseUp
Image

elanorb
Livecode Staff Member
Livecode Staff Member
Posts: 516
Joined: Fri Feb 24, 2006 9:45 am

Re: Can I move an object from one card to another?

Post by elanorb » Wed Apr 28, 2021 11:49 am

One thing you might find useful is the concept of a shared group. This is a group of controls that can appear on more than one card. If you don't have too many items that can be picked up and dropped you could put them all in a group that is shared on each card and set the visibility of the items when you go to the card.

We have a lesson on this https://lessons.livecode.com/m/4071/l/1 ... iple-cards.

This might be a good method as you do not really need to 'move' controls and any changes you make to the shared group only need to be made once. for example adding another item etc.

I hope that is some help.

Elanor
Elanor Buchanan
Software Developer
LiveCode

Post Reply

Return to “Talking LiveCode”