How do I set this up correctly

Get help using Construct 2

Post » Sun Nov 23, 2014 4:34 am

Okay so I'm trying a different type of game where users will work together with others who use the app, while only playing alone actually.

So let me try to explain it,

A single person will click a button on their screen to add points. Once the person is ready to cash in those points, a plugin (after research I assume I'd be using something like AJAX or XML) will post the players cashed in points to a number stored which gathers all points collected by everyone else who has used the app. Basically making it a team effort to build this number up as high as possible.

So, is this possible? I would hope it would be, but then I think of all the errors that could come out of it, like people posting at the same time and maybe messing up the total number, I'm not sure or the system would wait until the first person's number is added before the next, it's very complicated which therefore is difficult for me to explain.

In summary, I am trying to figure out if it is possible to have a number stored somewhere (on a file of course that would be linked to a URL), that Construct 2 can access that number and add to it the points the player is cashing in, and making the total now higher, and everyone who contributed can now see the new point total.

I am stumped and didn't seem to find a tutorial that explained my situation, probably because my situation is rather complicated, any help is appreciated :)
B
3
Posts: 6
Reputation: 189

Post » Sun Nov 23, 2014 9:19 am

HI @Cgimler. Yes that's certainly possible. If you can't find a tutorial I'll see if I can put something together for you.
Image
B
9
S
3
G
1
Posts: 346
Reputation: 1,560

Post » Sun Nov 23, 2014 12:35 pm

OK, what I would do is this:
1) set up a hosted database somewhere that will track the points.
2) whenever a player cashes in some points you want the app to make an AJAX call to a url linked to the database, which will create a record to store those points.
3) during play each player's app makes regular AJAX calls to a similar url which triggers a database query to show the total of all of those records which were created in step 2.
Image
B
9
S
3
G
1
Posts: 346
Reputation: 1,560

Post » Sun Nov 23, 2014 6:11 pm

Thanks for the reply @hundredfold, so from what your saying is that I need to set up a mysql database right? I tried last night to set it up where AJAX calls a number from a php file which is located with the game's files on my webhost, but no avail. So I will try what you said and see if it works! I'm going to get started on it now trying to set it up, but I'm a beginner at mysql databases so I'm still trying to familiarize myself with the coding, if you have any example pages and coding I could work off of I would greatly appreciate it, if not I will begin the quest of trying myself :)

Thank you!
B
3
Posts: 6
Reputation: 189

Post » Sun Nov 23, 2014 7:15 pm

Hi @Cgimler. Yes php/mysql will certainly do the job. There's tons of tutorials around online for those and places you can get them hosted for free too. Also, if you're new to mysql you might find phpmyadmin useful as it gives an interface a little more like MS Access. You can get this set up in your localhost environment for testing offline without a host provider by using XAMPP.

Focus on how to:
- set up tables (to hold the records)
- insert records (to capture each score)
- use SUM queries with SQL

Let me know if you don't get it working using what's already available and I'll see if I can find some time to help you a little more with the detail.
Image
B
9
S
3
G
1
Posts: 346
Reputation: 1,560

Post » Sun Nov 23, 2014 7:42 pm

Thanks so much for the feedback and tips @hundredfold, I've managed to get somewhere but not to the finish line yet unfortunately. Once I get this down I'm pretty sure I'll have Mysql all figured out :P

I have some code already that I've put together based off of what I've read on tutorials around the web. (At point you might notice something I inputted incorrectly I'd appreciate it if you'd let me know :) )

Code #1: The creation of the table that stores all the scores
Code: Select all
CREATE TABLE ATotalPoints (
total_points DECIMAL(12,2) NOT NULL DEFAULT 0.00
) ENGINE = MYISAM DEFAULT CHARSET = utf8;


*I have a weird feeling that the line with "total_points DECIMAL..." is wrongly coded but I'm not entirely sure, based off of the knowledge I've received. And actually the rest of the creation code I'm skeptical about, but when I put that code in the myphpadmin, it successfully created a table, below is a picture of what the results :O

Image

So then I'm assuming the table creation process is complete, I went to my webhost to create a php file that posts the score that the player posts, which the code is below (for security I blocked out my server information :P )

Code: Select all
<?php
$score = $_GET['score'];
$con = mysql_connect("*****","*******","******");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
 
  mysql_select_db("ATotalPoints", $con);
 
  mysql_query("INSERT INTO ATotalPoints (score) VALUES ($score)");

mysql_close($con);
?>


So then I saved the php and placed it with my game files on my webhost, then went to construct and where the player can submit the score, I inputted the request for AJAX to use the php, below is what it looks like-

Image

Then from what I believe, I would then be able to export my game to my webhost, go the point where I'd post the score. I didn't get any errors from the game about the request for posting, but then once I went to my table to check if the score was posted, it says the same thing as the beginning, or would it even show me there? I know I still have the process of requesting the sum score of everyone combined, but right now I'm trying to figure out on just the posting part... HELP :(
B
3
Posts: 6
Reputation: 189

Post » Sun Nov 23, 2014 9:24 pm

Hi @Cgimler well done you're doing great.

Your current block could be caused by your host being cautious about cross-domain AJAX calls.

If you add the following line to the top of your php file it could well sort that for you:
(inside the php tags)

header('Access-Control-Allow-Origin: *');

Also, you might want to eventually upgrade the table to include some other fields if you'll ever want to see where specific scores are coming from, or if you want more than one group to play without affecting each other's scores.
Image
B
9
S
3
G
1
Posts: 346
Reputation: 1,560

Post » Sun Nov 23, 2014 11:10 pm

Thanks again @hundredfold :) I add that to my php file just to be safe and see if it works! I did plan on making different groups but I didn't think I would put the same scores in the same table, rather make two separate tables, but that might be a waste a time, I guess I'll just figure it out on the way through it all haha!

At this point I'm gonna try to continue figuring this out as I go along, I do appreciate all your help so far, if I have any other questions I'll give you a hollar, but hopefully I won't have to :)
B
3
Posts: 6
Reputation: 189

Post » Sun Nov 23, 2014 11:53 pm

So actually the php file I made isn't connecting because of a code error :(... I tested my code on a website called writecodeonline dot com slash php (can't put urls in posts yet :P) to it telling me this error...

Code: Select all
<br />
<b>Fatal error</b>:  Call to undefined function mysql_connect() on line <b>4</b><br />


so there might be issues with my webhost, so now, I'm going to try getting it to work with XXAMP on my local computer... I guess :/
B
3
Posts: 6
Reputation: 189

Post » Mon Nov 24, 2014 7:55 am

Hi - get all your connection stuff working on your host before trying the AJAX connection.

You should be able to test the scripts by just running the url directly in the browser.

Normally a host will provide example connection scripts for you.

It'll be the same with localhost - you'll still need to connect to the database.
Image
B
9
S
3
G
1
Posts: 346
Reputation: 1,560

Next

Return to How do I....?

Who is online

Users browsing this forum: Yahoo [Bot] and 19 guests