Jump to content


Photo

getting variables from objects without instances


  • Please log in to reply
6 replies to this topic

#1 _175023

_175023

    GMC Member

  • New Member
  • 46 posts

Posted 25 February 2012 - 10:49 PM

Hi, I want to draw the sprites for all the spaceships a player has selected to use in the game. These sprites belong to objects which do not have a current instance in the game. Is there a way to address these objects anyway? My other options as I see it is to create a system to keep track of which sprites go to which spaceships, or maybe create a temporary instance of the object, get the information I need, and then destroy it. Any help much appreciated!
  • 0

#2 greep

greep

    Menaces with Spikes

  • GMC Member
  • 2297 posts
  • Version:GM7

Posted 25 February 2012 - 11:13 PM

Justm, upon selection, add sprite_indexes to a ds_list, then destroy it as needed. If you need to know which object the sprite_indexes correspond to, make it a 2d one. Or a global array, it's not like a lot of memory is involved. For really small things like this don't worry about optimization, just do what works.

Edited by greep, 25 February 2012 - 11:14 PM.

  • 0

#3 RangerX

RangerX

    GMC Member

  • GMC Member
  • 920 posts
  • Version:GM8.1

Posted 26 February 2012 - 03:11 PM

And if you really need to talk to an object that isn't the room well... you can't. Place those objects outside the view or somewhere in the room where they will be invisible to the player and call them/work with them whenever its needed.
  • 0

#4 PetzI

PetzI

    GMC Member

  • GMC Member
  • 1026 posts
  • Version:GM8.1

Posted 26 February 2012 - 04:33 PM

You can use object_get_sprite(ind). "ind" is the object's name (it's actually an index number than objects have, but you can just use the name and Game Maker changes it to the number. Make sure you don't have any resources with the same names, though.)

You can also get other variables from other resources. In the Help file, all these functions are under GML > Resources.

Edited by PetzI, 26 February 2012 - 04:34 PM.

  • 0

#5 TheouAegis

TheouAegis

    GMC Member

  • GMC Member
  • 4714 posts
  • Version:GM8

Posted 26 February 2012 - 08:26 PM

You will still need to keep track of if the ship has been unlocked, though.

Suppose you have 4 unlockable ships and one starting ship. Create a variable like

globalvar myships;
myships=0; //"0" will denote the starting ship

Now assign in your head a value for each other ship as follows:

Starting Ship: 0
First Unlockable: 1
Second Unlockable: 2
Third Unlockable: 4
Fourth Unlockable: 8

When the player unlocks a ship, add its specific value variable

myships+=1; //The player has just unlocked the first unlockable
myships+=4; //The player has just unlocked the third unlockable

Notice the player hasn't unlocked the 2nd and 4th unlockable ships yet. So now the variable is set to 5 (0+1+4). Now when you want to display a ship, compare the variable to the value it is supposed to be. Since the starting ship is guaranteed, you don't need to do any checks for it.

if (myships & 1) { show first unlockable }
if (myships & 2) { show 2nd unlockable }
if (myships & 4) { show 3rd unlockable }
if (myships & 8) { show 4th unlockable }

Note you use a single ampersand because this is a bitwise operation. In the example with the two ships unlocked, myships will have the binary value of 0101. The above conditionals will do the following:

myships & 1
0101 &
0001
-----
0001 = 1

myships & 2
0101 &
0010
-----
0000 = 0

myships & 4
0101 &
0100
-----
0100 = 4

myships & 8
0101 &
1000
-----
0000 = 0

Since ANDing 1 or 4 will yield a result greater than 0, the conditional will pass, but ANDing 2 or 8 yields 0 so their conditionals will fail, preventing the ship from being drawn.
  • 0

#6 greep

greep

    Menaces with Spikes

  • GMC Member
  • 2297 posts
  • Version:GM7

Posted 27 February 2012 - 12:10 AM

Heh, I know you like bitwise operators, but keep in mind the audience here (41 posts)
  • 0

#7 _175023

_175023

    GMC Member

  • New Member
  • 46 posts

Posted 27 February 2012 - 02:43 AM

Hey thanks all, this has been a big help!

@TheouAegis, I got you; it's just like setting file permissions.
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users