Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs

ct.js

Create cool 2D games with a free game editor! · By Cosmo Myzrail Gorynych

Accessing constants and variables from copies/rooms

A topic by JB007 created 40 days ago Views: 68 Replies: 3
Viewing posts 1 to 4
(1 edit)

I just can't figure out how to get a value from a set constant or variable between rooms and copies, or copies to other copies. Does not matter what I try the results are always undefined values.

ROOM (within Create)

CONST X_OFFS=123;

THE COPY (within Create)

this.x=ct.room.X_OFFS+64 // <-- Does not work. Undefined

Even if I make CONST X_OFFS into this.X_OFFS the copy still does not pick up the value.

Can anyone make any sense please how best to share fixed constants between:

ROOM <> COPY

COPY <> ANOTHER_COPY

Developer (1 edit)

Variables and constants created with const, let and var exist on a quite small life span and don’t get stored inside rooms or copies. They are for temporary values. (See Introduction to JavaScript)

To create a property that is stored inside a room, you should write in this syntax: this.X_OFFS = 123;

Then you can read this property with ct.room.X_OFFS. It works the other way as well (reading/writing the properties of a copy from within a room).

(1 edit)

Hello Cosmo,
Does not seem to work for me. Here is a simple bit of test code. I have one room and one copy placed into it via the editor.
The COPY1 copy is not reading the value of ROOM_VAR.

ROOM1  (Code in CREATE)

this.ROOM_VAR="TestValue";
console.log("from room -> " + this.ROOM_VAR);

COPY1  (Code in CREATE) 

console.log("reading room value -> " + ct.room.ROOM_VAR);

The console log results when code is run:

reading room value -> undefined
 from room -> TestValue

So, it seems to me the COPY1 is created first, then the ROOM1, according to the order of the console log.
This would contradict the ct documents:

  1. room's oncreate event, which is emitted when a user starts a game or navigates to a new room;
  2. oncreate is applied for each copy;
  3.  ...


I am running the Linux version 1.21 64 bit if it helps.

(3 edits)

Cosmo,

Would you kindly test this for me? --> VarsTest

It is a simple test program with a this.XXX_VAR in a script, room, and copy.
Just to see what access results come from where.

My console log reports the following showing that copy does not read the room or script variable, and room does not read the script variable.

from script1 -> script1 value -> 123
from copy1 -> room1 value -> undefined
from copy1 -> script1 value -> undefined
from room1 -> room1 value -> TestValue
from room1 -> script1 value -> undefined