Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > CopperCube > Programming and Scripting
How can I get multiple variables from one TXT file?

dekon_17
Registered User
Quote
2021-10-28 20:06:49

So, I tried creating a "multiplayer"... thing (with some tips from there: https://www.ambiera.com/forum.ph...). And so, I need to make a few variables in one file. The thing is that I know how to write multiple of variables in one file (like ccbWriteFileContent(file, var1 + " " + var2 +...) or kinda like that), but how do I make it read this file as a multiple variables? Is there a way to do this?


Guest
Guest
Quote
2021-10-28 21:19:52

I answered this already here:

https://www.ambiera.com/forum.ph...

But I will write an example for your specific question.

Assume I have a txt file named data.txt with this content:

ccbSetCopperCubeVariable("A", "Apple");
ccbSetCopperCubeVariable("B", "Banana");
ccbSetCopperCubeVariable("C", "Cherry");

Then I would write a function like this one below:


function assignVarsFromTxt(src) {

var content = ccbReadFileContent(src);

var lineArray = content.split("\n");

var arrayLength = lineArray.length;

for (var i = 0; i < arrayLength; i++) {

function myInstructions() {

var F = new Function (lineArray[i])

return(F());
}

lineArray.forEach(myInstructions);
}
}


You would then invoke the function in a Before first drawing do something > Execute Java Script behavior and action like so:


assignVarsFromTxt("data.txt");


This is just how I would do it. Others may have a better way. If you have any questions about the code, I will answer them. Hope this helped.


sven
Registered User
Quote
2021-10-28 21:27:48

dekon_17 wrote:
So, I tried creating a "multiplayer"... thing (with some tips from there: https://www.ambiera.com/forum.ph...). And so, I need to make a few variables in one file. The thing is that I know how to write multiple of variables in one file (like ccbWriteFileContent(file, var1 + " " + var2 +...) or kinda like that), but how do I make it read this file as a multiple variables? Is there a way to do this?


I made script that saves and loads variables for game saveing.

https://www.ambiera.com/forum.ph...

Just take a look into script and you see how i made it.


Guest
Guest
Quote
2021-10-30 01:13:30

Noticed some issues with my code. Here's the corrected version of the function:


function assignVarsFromTxt(src) {

var content = ccbReadFileContent(src);

var lineArray = content.split("\n");

for (var i = 0; i < lineArray.length; i++) {

function myInstructions() {

var F = new Function (lineArray[i])

return(F());
}

myInstructions();
}
}



dekon_17
Registered User
Quote
2021-11-01 10:34:21

Thanks guys! Also, sorry for a bit late answer. If I won't be able to do a multiplayer (due to my -5 IQ, lol), I still can use this in order to create a save system (like sven just shown) or something.


sven
Guest
Quote
2021-11-01 11:30:49

wrote:
Thanks guys! Also, sorry for a bit late answer. If I won't be able to do a multiplayer (due to my -5 IQ, lol), I still can use this in order to create a save system (like sven just shown) or something.


you can use my multiplayer example that i made..its working example.. search udp lan multiplayer by sven. its ok enaught for start.


DouweDabbe
Guest
Quote
2021-11-01 22:04:48

better use JSON

create an object with properties for all player positions
and their inventory

then convert this object to a JSON text

write json text to file

then all read that file and

read that json text into gameobject
and update all positions except your own.


Create reply:


Posted by: (you are not logged in)


Enter the missing letter in: "I?ternational" (you are not logged in)


Text:

 

  

Possible Codes


Feature Code
Link [url] www.example.com [/url]
Bold [b]bold text[/b]
Image [img]http://www.example.com/image.jpg[/img]
Quote [quote]quoted text[/quote]
Code [code]source code[/code]

Emoticons


   






Copyright© Ambiera e.U. all rights reserved.
Privacy Policy | Terms and Conditions | Imprint | Contact