Can I move an image between cards?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Jetboy
Posts: 7
Joined: Wed Sep 06, 2017 11:22 am

Can I move an image between cards?

Post by Jetboy » Wed Sep 06, 2017 11:53 am

I am making a game with an inventory system. Throughout the game you navigate N, S, E, W...etc between different cards (1st person, point and click adventure style).

When you click on the image of a collectable item, the visibility is set to 'false' and the item is entered into a database (1 of 6 inventory slots). When you open up your inventory (a data grid + 2 buttons, 'use' and 'drop'), you can see a list of all the items you are carrying (in a 'data grid' that pulls the inventory data from the database). If you click on the 'drop' button, the visibility of the image becomes 'true' and the entry for the item is deleted from the database.

My challenge is that if (for example) I pick up an item in card 1, I can't drop it in card 2 because there is no item with that name in card 2. Thus, my 'trick' of making the object visible/invisible doesn't work.

Is there a command I can attach to the 'drop' button that can find the object within the stack (i.e. search all cards) and then move it to the card that is being used, then make it visible?

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Can I move an image between cards?

Post by Klaus » Wed Sep 06, 2017 12:24 pm

Hi Jetboy,

1. welcome to the forum! :D

2. Not sure I understand your problem correctly, but can't you use a GROUP for your inventory stuff?
This way the SAME group could be placed on every card and thus all objects are available on each card.


Best

Klaus

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Can I move an image between cards?

Post by Klaus » Wed Sep 06, 2017 1:00 pm

Hi Jetboy,

even better idea:
Do not use the original image but a button with the ID of your image set as ICON! :D

This way you can also use any image, no matter where it is in your stack, when you
set the icon of a button to its ID, the engine will search the complete stack and substacks
to find an image with that ID!

Know what I mean?


Best

Klaus

Jetboy
Posts: 7
Joined: Wed Sep 06, 2017 11:22 am

Re: Can I move an image between cards?

Post by Jetboy » Wed Sep 06, 2017 1:41 pm

Klaus wrote:Hi Jetboy,

1. welcome to the forum! :D

2. Not sure I understand your problem correctly, but can't you use a GROUP for your inventory stuff?
This way the SAME group could be placed on every card and thus all objects are available on each card.


Best

Klaus
Thanks! I thought about this option... my concern is that the group will then exist on every single card if I do this. Therefore, if I 'drop' an item on 1 card, it will then be visible on every card.
Klaus wrote:Hi Jetboy,

even better idea:
Do not use the original image but a button with the ID of your image set as ICON! :D

This way you can also use any image, no matter where it is in your stack, when you
set the icon of a button to its ID, the engine will search the complete stack and substacks
to find an image with that ID!

Know what I mean?


Best

Klaus
I like your thinking but am trying to visualise how I'll do it.

So... every 'room' has multiple buttons. These buttons are not grouped, they are individual buttons.

If I click one of these buttons, the picture becomes blank (giving the illusion that the item is gone) and the item is still placed into my inventory. If I 'drop' an item, then the picture of that item is placed onto one of the buttons within the screen.

Uuuum... so say I have 3 buttons per card (all with the same 3 names for each card) then I have a 'drop' statement that searches the 3 buttons for one with no picture and then places it there. Rather than buttons becoming 'invisible'... they simply lose their picture (or gain one when an item is dropped).

Not sure if this is what you meant, but it sounds plausible to me :D I will try it out tomorrow and get back to you...

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

Re: Can I move an image between cards?

Post by bogs » Wed Sep 06, 2017 2:31 pm

Seems like this could be an ideal use of the copy command. From the dictionary:
Syntax:
copy [object [to {card | group | stack} ]] | [chunk of field]
So you might do something like you started with, user clicks, set the object to invisible (picked up, etc etc), when you get to the card you want to drop it, use the copy command to move it to the new card, set it to visible (dropped, etc etc).

For instance, this code copies an object and changes its visibility :

Code: Select all

   if the short name of this card = "one" then
      set the visible of me to "false"
      copy me to card "two"
   end if
Image

Jetboy
Posts: 7
Joined: Wed Sep 06, 2017 11:22 am

Re: Can I move an image between cards?

Post by Jetboy » Wed Sep 06, 2017 11:22 pm

bogs wrote:Seems like this could be an ideal use of the copy command. From the dictionary:
Syntax:
copy [object [to {card | group | stack} ]] | [chunk of field]
So you might do something like you started with, user clicks, set the object to invisible (picked up, etc etc), when you get to the card you want to drop it, use the copy command to move it to the new card, set it to visible (dropped, etc etc).

For instance, this code copies an object and changes its visibility :

Code: Select all

   if the short name of this card = "one" then
      set the visible of me to "false"
      copy me to card "two"
   end if
Awesome, yes I think that will definitely work. I'll give it a go tonight after work and report back.

Guess all I'll have to do on top of this is have some kind of a switch statement that knows where the original card for each object is, so that the copying can work properly. I might need some kind of clean-up code inside the switch too, so that 'rooms' don't get filled up with invisible objects. I was thinking about having a DB enry for every object, but I think that'd be too tedious.

PS - great to see such an active community behind all this!

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

Re: Can I move an image between cards?

Post by bogs » Thu Sep 07, 2017 12:26 am

Jetboy wrote:Guess all I'll have to do on top of this is have some kind of a switch statement that knows where the original card for each object is, so that the copying can work properly. I might need some kind of clean-up code inside the switch too, so that 'rooms' don't get filled up with invisible objects.
Not necessarily, in the dictionary, look up the 'cut' and 'paste' commands, which might apply more to what your trying to accomplish.

Glad to be of help :)
Image

Jetboy
Posts: 7
Joined: Wed Sep 06, 2017 11:22 am

Re: Can I move an image between cards?

Post by Jetboy » Thu Sep 07, 2017 5:39 am

bogs wrote:
Jetboy wrote:Guess all I'll have to do on top of this is have some kind of a switch statement that knows where the original card for each object is, so that the copying can work properly. I might need some kind of clean-up code inside the switch too, so that 'rooms' don't get filled up with invisible objects.
Not necessarily, in the dictionary, look up the 'cut' and 'paste' commands, which might apply more to what your trying to accomplish.

Glad to be of help :)
Cheers so then (to avoid a bulky switch statement) I could cut and paste objects into a 'storage' card so that I don't have to keep track of the items?

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

Re: Can I move an image between cards?

Post by bogs » Thu Sep 07, 2017 2:02 pm

Jetboy wrote: Cheers so then (to avoid a bulky switch statement) I could cut and paste objects into a 'storage' card so that I don't have to keep track of the items?
Well, there are at least 3 or 4 (or possibly more) ways to do what your talking about easily. Just to review, in the first post you said
My challenge is that if (for example) I pick up an item in card 1, I can't drop it in card 2 because there is no item with that name in card 2. Thus, my 'trick' of making the object visible/invisible doesn't work.
The number 1 easiest way to accomplish this that I can think of was already proposed by Klaus, here:
Klaus wrote:2. Not sure I understand your problem correctly, but can't you use a GROUP for your inventory stuff?
This way the SAME group could be placed on every card and thus all objects are available on each card.
To go a bit further in explaining, a 'group' is any number of objects, but in this case lets say 1 object. If you turn each of the objects in your game into an individual group, you gain the ability to share those groups on every card as a background object. All of this sharing can be done through the property inspector.
...What this gives you is the ability to make it look like your "picking up the object on one card, and dropping it on another" simply by setting the visibility of the group object, probably through a single if then statement way down on the stack level in a handler.

The 'copy' method I listed was based on a misunderstanding of your question on my part, I literally thought you wanted to copy the object, but on re-reading after your response, I understood you actually want it removed (or to appear removed) and placed.

The 'cut' method would certainly also accomplish your goal, when combined with 'paste' and 'remove'. You will write more lines of code than simply setting the visible of a group object to true or false, but otherwise it would work in a similar way.

And there may be a lot of other ways to do this simply. I suspect that 'move' probably would do it, but this post is already longer than it should be.
...Again, since you have multiple cards in play, probably the best way to handle any and all of these methods will be in the stack level script, using the mouseUp or mouseDown event to trigger your specific handler/function.

As to your question about switch statements, at some point for what your doing you will either have to use an if/then or switch structure, if only to determine when to move the object or set its visible status.

Hope that helps, but if I've only confused you more, feel free to post back :D
Image

Jetboy
Posts: 7
Joined: Wed Sep 06, 2017 11:22 am

Re: Can I move an image between cards?

Post by Jetboy » Fri Sep 08, 2017 12:25 am

Thank you for the detailed response, I appreciate being able to throw ideas around (as it seems, there are many ways to fry a fish).

With groups, the only issue I see is that the object will be visible (or invisible) on every card and there will be no way of tracking where items have been 'dropped'. Am I right about this?

For example my inventory buttons and N, S, E, W...etc controls are all part of a group because they are the same on every card. However, I have these stray object that I want to be able to put anywhere... but not everwhere.

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

Re: Can I move an image between cards?

Post by bogs » Fri Sep 08, 2017 12:47 pm

Yes, fish frying has a lot of ways to go about getting it done :)

You can easily track where something *should* be visible by using either a variable or, more likely, a custom property of the stack in this case. Let us say you have 3 cards in one stack for simplicity. You might create a custom property of the stack that is a string saying if the item is in inventory, or not (your handler deciding whether it is in inventory will set the property).
... you have a second custom prop that stores whether or not the item has been dropped. A third custom prop would tell you the card name in a string. All of this should be in the handler for dropping items.

On cards where the item should be invisible (or all cards when you have the item in inventory) you retrieve the third custom props string and set the visible based on that. Remember we were talking about if/then and switch statements coming at some point? Well you've arrived at that point :mrgreen:
Pseudo-code (really long hand, this could all be much much shorter)

Code: Select all

on openCard
/* stop any updates till we know what should be on the screen ... */
    lock screen
/* beginning of the game, the item would not be in inventory, so the visible would be true when you initialize the game */
    if the item is in field "inventory" then
       set the visible of group "item1" to "false"
    else
       set the visible of group "item1" to "true"
   end if
/* the item has been dropped somewhere... */ 
    if the item is not in field "inventory" then
        set the custPropDropped to the (long / short / abbreviated) name of this card
/* your still on this card after dropping, so .. */
        set the visible of group "item1" to "true"
   end if
/* on any other card ... */ 
   if the name of this card is not the custPropDropped of this stack then
       set the visible of group "item1" to "false"
   end if
/* remember to unlock the screen again ... */
unlock screen
end openCard
Now, I just simply put all the code above into the open card event, you will likely not do anything that poorly designed, break things down into functions and handlers that you can call from everywhere else and you will be much happier maintaining this later.

The above lets you make it "look" like the object is in one place, after all, you can only have one card of one stack open at any given time (OR CAN YOU?@? :twisted: ) and so the object will be there or not based on the card you are on.

The other way (cut, paste) you would again have to write out the handler to be called based on definitive events. This would be more along the lines of what would happen in the physical world. The upsides would be that might be an easier model for you to follow, but you would have to paste the object after each pickup, then have to cut/paste it again for each drop. For instance, what happens if the player picks up a 2nd object ? :wink:
Last edited by bogs on Fri Sep 08, 2017 7:21 pm, edited 1 time in total.
Image

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7237
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Can I move an image between cards?

Post by jacque » Fri Sep 08, 2017 5:29 pm

Here's how I'd do it. I'd use Klaus' suggestion to set button icons rather than copy whole images, it has a number of benefits. You don't need to have existing buttons on the card, they can be created on the fly and assigned an icon in script.

I would keep a list (probably an array, but a plain text list would work too at the expense of a little more scripting) that keeps track of what objects should be on each card. If using an array, the keys would be the card names and the element contents would be the objects to display, their location coordinates, and their icon ID.

On preopencard (not opencard) I'd look up the card name in the array and create the buttons and assign their icons.

On closecard I'd update the array and then delete all the temporary buttons. This keeps the playing field clear and you don't have to worry about forgetting to remove stray objects.

The inventory content could also be an entry in the array. Since standalones can't save themselves, you'd need to store the array on disk if you want the user to be able to pick up where they left off if they restart the game later. It isn't hard to do.

The group that was mentioned here was for the inventory, not the object buttons, BTW.

If you decide to implement this and you get stuck, just ask.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

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

Re: Can I move an image between cards?

Post by bogs » Fri Sep 08, 2017 7:20 pm

Wow, just wow. I (somehow) completely missed Klaus's 2nd post :shock: :oops: Jacque is also right about the preOpenCard being the better place for the checking, keep in mind my pseudo-code was just to lay out my initial thoughts on it, not explicit instructions on how it had to be done :)
Image

Jetboy
Posts: 7
Joined: Wed Sep 06, 2017 11:22 am

Re: Can I move an image between cards?

Post by Jetboy » Mon Sep 11, 2017 10:54 am

jacque wrote:Here's how I'd do it. I'd use Klaus' suggestion to set button icons rather than copy whole images, it has a number of benefits. You don't need to have existing buttons on the card, they can be created on the fly and assigned an icon in script.

I would keep a list (probably an array, but a plain text list would work too at the expense of a little more scripting) that keeps track of what objects should be on each card. If using an array, the keys would be the card names and the element contents would be the objects to display, their location coordinates, and their icon ID.

On preopencard (not opencard) I'd look up the card name in the array and create the buttons and assign their icons.

On closecard I'd update the array and then delete all the temporary buttons. This keeps the playing field clear and you don't have to worry about forgetting to remove stray objects.

The inventory content could also be an entry in the array. Since standalones can't save themselves, you'd need to store the array on disk if you want the user to be able to pick up where they left off if they restart the game later. It isn't hard to do.

The group that was mentioned here was for the inventory, not the object buttons, BTW.

If you decide to implement this and you get stuck, just ask.
I'm back :D

This sounds good. My only challenge now will be making is 'saving' the location (i.e. having a 'saved game' where all the objects...etc are where they were when I left them). It's bulky but seems like what I'm going to have to do is make a column in my database for each room, store the array in it and then reference the database on preopencard.

Sorry that this is all planning and not 'doing'... however, I figure that if I make a good plan, I'll save stress later down the track. I can think of many instances (don't say the work pygames!!!) where I've just jumped in as an over-confident n00b, smashed out 10,000 lines of code and then gone 'oooooh faaaaark!!! My underpinning framework for EVERYTHING is so frigging stupid and never should have been done that way EVER!!' Best to ask experts and make a plan first. So the plan:
1. Preopencard checks the database and sucks in the array
2. A preopencard script then crunches the array and modifies the card accordingly (i.e. adds the objects...etc)
3. While the card's open, everything works off the array
4. When you close the card, the array is copied into the database
5. On top of all this, there's an extra column in the database that lets the game know which room you're in... so every time you open the game, you

This has some flaws:
1. It relies on a database and a lot of arrays rather than 'livecode' to save the current state (I feel there's probably a more 'native' way of doing this that I don't quite get)
2. Multiple saved games will be difficult to implement if I have like 50-100 columns (i.e. 1 for each room). I guess what I could do is make a massive array with a key for each room instead, then save this array into a single column? This would allow multiple saved games and would make the database a heck of a lot easier to navigate. Uuuum... anybody know what livecode's 'sqlite_max_length' is? Guessing that would quickly become an issue if (again) I'm thinking 50-100 cards in my game.

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

Re: Can I move an image between cards?

Post by bogs » Mon Sep 11, 2017 1:12 pm

Um. I'd suggest a slightly different way to your goal with saved games. I would make one separate card with a simple table object to hold all the things you are planning to put into a database, and I'd use a launcher stack to launch the game.

All your db info would be put and managed into the card with the simple table and due to the nature of using the launcher setup, you could save the state of all the cards that make up the actual game so you could eliminate having to keep track of that.
Image

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”