Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

[Resolved] Linking data

Status
Not open for further replies.
I was wondering if it was possible to link two variables/objects to the same value. not necessarily assign one variable the value of another variable, but link them so if you change the data of one, then you also change the data of the other.

Like so:

x = 4
y = x
y = 1
print x => 1
 

Mac

Member

Do you mean like this so that whatever the other variable is the other will be the same, if so..its done like this:-

Code:
x==y

Remember that...

= Assigns
== Equalivalent
 
You cannot assign the same variable to different values. You can assign them to an array, but a variable can only point to one object.

= : Assigns
== : Compares and returns true or false, depending upon a boolean.

(x == y) = ture or false, depending on x.

is_a_bird? = object == bird

Something like that.
 
However, it's different for initialized objects.

Let me show you :

Code:
class Blah
  attr_accessor :value
  def initialize
    @value = 0
  end
end

var1 = Blah.new
var2 = var1
var2.value = 20
print var1.value # returns 20
 
Status
Not open for further replies.

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top